File size: 3,285 Bytes
5889992
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9111274
 
 
 
 
 
 
5889992
 
 
 
 
 
 
 
 
 
 
 
 
9111274
5889992
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#TODO: Collate the necessary features for the Telegram bot, including parts like buttons, other services, Web Search Feature (*Might be nice to try this out.*)

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

# Configure logging
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"
            )
        )

        # Get or create session
        session_data = context.user_data.get('session_data')
        
        # Process query
        response = await conversation_agent.process_async(
            query=text,
            session_data=session_data
        )
        
        # Update session data
        context.user_data['session_data'] = response.session_data
        
        # Send response with typing indicator
        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()

    # Add handlers
    application.add_handler(CommandHandler("start", start))
    application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
    
    # Add error handler
    application.add_error_handler(error_handler)

    # Start polling
    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()