|
1 | 1 | import telegram |
2 | 2 | import os |
3 | | -from telegram.ext import Application, CommandHandler, CallbackQueryHandler |
4 | | -from bot_logic import COMMANDS, button # Assuming 'button' is now in bot_logic |
| 3 | +import logging |
| 4 | +from telegram.ext import Application, CommandHandler, CallbackQueryHandler, MessageHandler, filters |
| 5 | + |
| 6 | +from bot_logic import COMMANDS, button |
5 | 7 | from models import init_db |
6 | 8 |
|
| 9 | +async def log_update(update, context): |
| 10 | + """Log every update received by the bot.""" |
| 11 | + logger = logging.getLogger("DCUBABOT") |
| 12 | + user = update.effective_user |
| 13 | + chat = update.effective_chat |
| 14 | + |
| 15 | + log_message = f"Update received. User: {user.id} ({user.username}) in chat: {chat.id} ({chat.title})." |
| 16 | + if update.message and update.message.text: |
| 17 | + log_message += f" Message: {update.message.text}" |
| 18 | + elif update.callback_query: |
| 19 | + log_message += f" Callback Query: {update.callback_query.data}" |
| 20 | + |
| 21 | + logger.info(log_message) |
| 22 | + |
| 23 | + |
7 | 24 | def main(): |
8 | 25 | """Start the bot.""" |
| 26 | + logging.basicConfig( |
| 27 | + level=logging.INFO, |
| 28 | + format='[%(asctime)s] - [%(name)s] - [%(levelname)s] - %(message)s', |
| 29 | + filename="bots.log" |
| 30 | + ) |
| 31 | + |
| 32 | + init_db() |
9 | 33 |
|
10 | 34 | application = Application.builder().token(os.environ["TELEGRAM_BOT_TOKEN"]).build() |
11 | 35 |
|
| 36 | + # Add the logging middleware handler with a high priority |
| 37 | + application.add_handler(MessageHandler(filters.ALL, log_update), group=-1) |
12 | 38 |
|
13 | 39 | for command_name, command_info in COMMANDS.items(): |
14 | | - application.add_handler(CommandHandler(command_name, command_info['handler'])) |
| 40 | + application.add_handler(CommandHandler(command_name, command_info['handler'])) |
15 | 41 |
|
16 | | - # Register the callback query handler for buttons |
17 | 42 | application.add_handler(CallbackQueryHandler(button)) |
18 | 43 |
|
19 | | - |
20 | | - init_db() |
21 | | - |
22 | | - # The WEBHOOK_URL is now guaranteed to be set by the CI/CD pipeline. |
23 | 44 | webhook_url = os.environ["WEBHOOK_URL"] |
24 | 45 |
|
25 | 46 | # Start the Bot |
|
0 commit comments