-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
106 lines (90 loc) · 4.04 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# SQLkiss Cat's Horoscope
import random
import telebot
import sys
import os
import datetime
import openai
from phase_moon import calculate_moon_phase
from Zodiac_Signs import funny_forecasts
from loguru import logger
# ID of groups
log_chat_id = '-1001945815831'
group_chat_id = '-1001760424253' #
# CHAT GROUP ID: how to find this:
# https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id
# Choose the answer with 58's rating ('the simplest way i found using only telegram-web :')
#
# BOT Token
TOKEN = os.environ.get('BOT_TELEGRAM_TOKEN_FOR_CODE', 'nothing')
bot = telebot.TeleBot(TOKEN)
if TOKEN == 'nothing':
bot.send_message(log_chat_id, 'TOKEN not accept')
sys.exit("TOKEN not accept")
# API KEY
openai.api_key = os.environ.get('OPENAI_API_KEY', 'notapi')
if openai.api_key == 'notapi':
bot.send_message(log_chat_id, 'TOKEN not accept')
sys.exit("API Key not accept")
# Question for the chatGPT *prompt* and choose random funny forecast
random_value = random.choice(list(funny_forecasts.values()))
random_key = next(key for key, value in funny_forecasts.items() if value == random_value)
prompt = f"Rephrase the following sentence, making it sound different but retaining the same meaning and add emoji:\n{random_value}"
response = openai.Completion.create(
engine="davinci", # Model
prompt=prompt,
max_tokens=100
)
chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": f"{prompt}"}]) #creating discussion
# # text promt to DALL-E
# image_discription = f'Generate the picture of {random_key} in cat\'s style'
# # request to AI DALL-E
# response = openai.Image.create(
# model="image-alpha-001",
# n=1,
# prompts=image_discription
# )
# url_send_image = response.data[0]['url']
# image_send = openai.Image.create(model="image-alpha-001", prompt=f"{image_discription}")
# current Moon phase
current_date = datetime.datetime.now()
current_month = current_date.month
current_day = current_date.day
current_year = current_date.year
current_moon_phase = calculate_moon_phase(current_year,current_month, current_day)
# fun for checking and updating work of bot into privat messages *this is just for self-debugging*
@bot.message_handler(commands=['start'])
def start(message):
chat_id = message.chat.id
keyboard = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True)
forecast_button = telebot.types.KeyboardButton("Forecasts")
keyboard.add(forecast_button)
bot.send_message(chat_id, f'Hi, I\'m a bot that can provide you with funny forecasts!')
bot.send_message(chat_id, f'Here are some things you can do:')
bot.send_message(chat_id, f'- Press the "Forecasts" button below to get a random forecast.', reply_markup=keyboard)
# fun for getting forecasts
@bot.message_handler(func=lambda message: message.text.lower() == "forecasts")
def send_forecast(message):
chat_id = group_chat_id
sendforecastvaruable = (f"Purrs and whiskers! It looks like we're in the midst of {current_moon_phase}, so it's time for some serious rub-a-dub forecast, my feline friends!" + f"\n{chat_completion.choices[0].message.content}")
bot.send_message(chat_id, sendforecastvaruable)
return f"Thank you for reading our horoscope"
# fun for posting the forecasts to group
def post_forecast():
chat_id = group_chat_id
send_forecast(chat_id)
# send logs to another chat with logs
@bot.message_handler(func=lambda message: message.text.lower() == "logs")
def send_logs():
log_content = ""
log_content += f"Debug logs🐱: {logger.debug}\n"
log_content += f"\nInfo logs🐈: {logger.info}\n"
log_content += f"\nWarning logs😾: {logger.warning}\n"
log_content += f"\nError logs🙀: {logger.error}\n"
log_content += f"\nException logs💣: {logger.exception}\n"
chat_id = log_chat_id
bot.send_message(chat_id, log_content)
# running script
if __name__ == "__main__":
post_forecast()
send_logs()