|
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 |
|
|
|
|
|
|
|
logging.basicConfig( |
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
|
level=logging.INFO |
|
) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
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.") |
|
|
|
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(): |
|
"""Start the bot.""" |
|
|
|
|
|
application = Application.builder().token(TOKEN).build() |
|
|
|
|
|
application.add_handler(CommandHandler("start", start)) |
|
|
|
|
|
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) |
|
|
|
|
|
|
|
|
|
|
|
application.run_polling(allowed_updates=Update.ALL_TYPES) |
|
|
|
if __name__ == '__main__': |
|
main() |
|
|