|
|
|
|
|
import asyncio |
|
import logging |
|
from telegram import Update, ReplyKeyboardMarkup |
|
from telegram.ext import ( |
|
Application, |
|
CommandHandler, |
|
MessageHandler, |
|
filters, |
|
ContextTypes, |
|
CallbackContext |
|
) |
|
|
|
from src.llm.agents.conversation_agent import ConversationAgent |
|
from src.llm.models.schemas import SessionData |
|
from src.llm.core.config import settings |
|
|
|
|
|
logging.basicConfig( |
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
|
level=logging.INFO |
|
) |
|
logger = logging.getLogger(__name__) |
|
|
|
conversation_agent = ConversationAgent() |
|
|
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): |
|
"""Handle /start command with interactive keyboard""" |
|
keyboard = [ |
|
["💬 Start Chatting"], |
|
["ℹ️ About", "🛠 Help"] |
|
] |
|
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True) |
|
|
|
welcome_msg = ( |
|
"🌟 Welcome to Thery AI! 🌟\n\n" |
|
"I'm here to provide compassionate mental health support. " |
|
"How can I help you today?" |
|
) |
|
|
|
await update.message.reply_text(welcome_msg, reply_markup=reply_markup) |
|
|
|
async def handle_message(update: Update, context: CallbackContext): |
|
"""Process user messages with conversation context""" |
|
try: |
|
user = update.effective_user |
|
text = update.message.text |
|
|
|
typing_task = asyncio.create_task( |
|
context.bot.send_chat_action( |
|
chat_id=update.effective_chat.id, |
|
action="typing" |
|
) |
|
) |
|
|
|
|
|
session_data = context.user_data.get('session_data') |
|
|
|
|
|
response = await conversation_agent.process_async( |
|
query=text, |
|
session_data=session_data |
|
) |
|
|
|
|
|
context.user_data['session_data'] = response.session_data |
|
|
|
|
|
await typing_task |
|
await update.message.reply_text(response.response) |
|
|
|
except Exception as e: |
|
logger.error(f"Error processing message: {str(e)}") |
|
await update.message.reply_text("I'm having trouble understanding. Let's try that again.") |
|
|
|
|
|
async def error_handler(update: Update, context: CallbackContext): |
|
"""Handle errors in the bot""" |
|
logger.error(f"Update {update} caused error: {context.error}") |
|
await update.message.reply_text("Oops! Something went wrong. Please try again.") |
|
|
|
|
|
def main(): |
|
"""Configure and start the bot""" |
|
application = Application.builder().token(settings.TELEGRAM_BOT_TOKEN).build() |
|
|
|
|
|
application.add_handler(CommandHandler("start", start)) |
|
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) |
|
|
|
|
|
application.add_error_handler(error_handler) |
|
|
|
|
|
logger.info("Starting Thery AI Telegram bot...") |
|
application.run_polling( |
|
poll_interval=1, |
|
allowed_updates=Update.ALL_TYPES, |
|
drop_pending_updates=True |
|
) |
|
|
|
if __name__ == "__main__": |
|
main() |