-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoGPT_Telegram-bot.py
50 lines (39 loc) · 1.42 KB
/
autoGPT_Telegram-bot.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
import pickle
import openai
import telebot
openai.api_key = "OPENAI_API_KEY"
telegram_key = "TELEGRAM_API_KEY"
bot = telebot.TeleBot(telegram_key)
def Generate_Respose(prompt):
try:
completions = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=150
)
except:
return "Openai server encountered a real time problem.\nPlease try again in some moments"
message = completions.choices[0].text
message = message.lstrip()
return message
@bot.message_handler(content_types=['voice'])
def voice_processing(message):
bot.reply_to(message, "Sorry, but I can't handle voice messages yet. I'm working on it.")
@bot.message_handler(func=lambda msg: True)
def echo_all(prompt):
try:
prompt_history = pickle.load(open(f'prompt_history/{prompt.from_user.username}', 'rb'))
except:
prompt_history = " "
temp = prompt_history + "\n" + prompt.text
response = Generate_Respose(temp)
prompt_history += f'\nUser: {prompt.text}'
if len(response) > 0:
response = response.lstrip("Ai: ")
response = response.replace("Ai: ", "")
bot.reply_to(prompt, response)
prompt_history += f'\nAi: {response}'
f = open(f'prompt_history/{(prompt.from_user).username}', "wb")
pickle.dump(prompt_history, f)
f.close()
bot.infinity_polling()