-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram_bot.py
More file actions
29 lines (20 loc) · 1.04 KB
/
telegram_bot.py
File metadata and controls
29 lines (20 loc) · 1.04 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
import os
from open_ai_client import OpenAiClient
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler
from models.chat import Chat
class TelegramBot:
def __init__(self, openai: OpenAiClient):
self.openai = openai
self.application = ApplicationBuilder().token(os.environ['TELEGRAM_TOKEN']).build()
self.application.add_handler(CommandHandler('ask', self._ask_command_async))
self.application.add_error_handler(self.error_handler_context)
def run(self):
self.application.run_polling()
async def _ask_command_async(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
chat = Chat(update.effective_chat.id, context.bot, self.openai)
return await chat.send_message_async(update.message.text)
async def error_handler_context(self, update, context):
print(update.error)
await context.bot.send_message(chat_id=update.effective_chat.id,
text="Something went wrong. Please, try again")