-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (48 loc) · 1.82 KB
/
main.py
File metadata and controls
64 lines (48 loc) · 1.82 KB
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
from dotenv import load_dotenv
import os
from aiogram import Bot, Dispatcher, executor, types
import openai
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY") # Fixed typo
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
class Reference:
"""Stores previous chat responses"""
def __init__(self):
self.reference = "" # Fixed typo
reference = Reference()
model_name = "gpt-3.5-turbo"
bot = Bot(token=TELEGRAM_BOT_TOKEN)
dispatcher = Dispatcher(bot)
def clear_past():
"""Clears past conversation history"""
reference.reference = ""
@dispatcher.message_handler(commands=['clear'])
async def clear(message: types.Message):
clear_past()
await message.reply("I've cleared the past conversation and context.")
@dispatcher.message_handler(commands=['start'])
async def welcome(message: types.Message):
await message.reply("Hi! I am Tele Bot! Created by me. How can I assist you?")
@dispatcher.message_handler(commands=['help'])
async def helper(message: types.Message):
help_command = """
Hi There, I'm a Telegram bot! Here are my commands:
/start - Start the conversation
/clear - Clear the past conversation and context
/help - Show this help menu
"""
await message.reply(help_command)
chat_history = []
@dispatcher.message_handler()
async def chatgpt(message: types.Message):
global chat_history
chat_history.append({"role": "user", "content": message.text})
response = openai.ChatCompletion.create(
model=model_name,
messages=chat_history
)
bot_response = response['choices'][0]['message']['content']
chat_history.append({"role": "assistant", "content": bot_response})
await bot.send_message(chat_id=message.chat.id, text=bot_response)
if __name__ == "__main__":
executor.start_polling(dispatcher, skip_updates=False)