File size: 3,207 Bytes
08531fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16386f2
08531fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16386f2
08531fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
from utils import clean_text
from os import environ
from app import generate_response, detect_language


# Configure logging
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)
logger = logging.getLogger(__name__)

# Get token from environment variable
TOKEN = environ.get("TELEGRAM_BOT_TOKEN")
if not TOKEN:
    raise ValueError("No TELEGRAM_BOT_TOKEN found in environment variables!")


async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    """Handler for the /start command."""
    if not update.message or not update.message.text:
        return
    await update.message.reply_text("Hello! Tell me your decision-making issue, and I'll try to help.")
    logger.info("Start command received.")


# -------------------------
# Message handler for incoming text messages
# -------------------------
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
    """
    Handler for incoming messages.
    Sends the user's message to the API and replies with the response.
    """
    try:
        if not update.message or not update.message.text:
            return
        
        user_text = clean_text(update.message.text)
        logger.info(f"User message: {user_text}")
    
        response = generate_response(user_text) 
        await update.message.reply_text(response)
    except Exception as e:
        logger.error(f"Error processing message: {e}")


  


# async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
#     """Handle regular messages."""
#     if not update.message or not update.message.text:
#         return

#     text = clean_text(update.message.text)
#     if '?' in text:
#         response = generate_response(text)
#     else:
#         user_lang = detect_language(text)
#         response = get_response('no_question', user_lang)

#     await update.message.reply_text(response)


# async def error_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
#     """Handle errors."""
#     logger.error(f"Error occurred: {context.error}")
#     try:
#         if update and update.message:
#             user_lang = detect_language(update.message.from_user.language_code)
#             await update.message.reply_text(
#                 get_response('error', user_lang)
#             )
#     except Exception as e:
#         logger.error(f"Error in error handler: {e}")


def main():
    """Start the bot."""
   
    # Create the application
    application = Application.builder().token(TOKEN).build()

    # Add handlers
    application.add_handler(CommandHandler("start", start))
    # application.add_handler(CommandHandler("help", help_command))
    # application.add_handler(CommandHandler("decide", decide))
    application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))

    # Add error handler
    # application.add_error_handler(error_handler)

    # Start the bot
    application.run_polling(allowed_updates=Update.ALL_TYPES)

if __name__ == '__main__':
    main()