| from fastapi import Request, Response | |
| import telebot | |
| from handlers import ( | |
| handle_message, handle_model_selection, | |
| handle_model_page_navigation, handle_reset_db_verification, | |
| ) | |
| from commands import ( | |
| handle_mute_command, handle_set_command, handle_clear_command, handle_reset_db_command, | |
| handle_json_command, handle_id_command, handle_unmute_command | |
| ) | |
| import logging | |
| async def root(): | |
| return {"message": "BOT 正在运行"} | |
| async def telegram_webhook(request: Request, token: str, bot, thread_local): | |
| if token != bot.token: | |
| return Response(status_code=403) | |
| try: | |
| body = await request.json() | |
| update = telebot.types.Update.de_json(body) | |
| bot_username = bot.get_me().username | |
| if update.callback_query: | |
| callback_query = update.callback_query | |
| if callback_query.data.startswith('resetdb_verify_'): | |
| handle_reset_db_verification(callback_query, bot, thread_local) | |
| elif callback_query.data.startswith('set_model_'): | |
| handle_model_selection(callback_query, bot, thread_local) | |
| elif callback_query.data.startswith('model_page_'): | |
| handle_model_page_navigation(callback_query, bot, thread_local) | |
| elif update.message: | |
| message = update.message | |
| if should_process_message(message, bot, bot_username): | |
| if message.text and message.text.startswith('.set'): | |
| handle_set_command(message, bot, thread_local) | |
| elif message.text and message.text.startswith('.clear'): | |
| handle_clear_command(message, bot, thread_local) | |
| elif message.text and message.text.startswith('.resetdb'): | |
| handle_reset_db_command(message, bot, thread_local) | |
| elif message.text and message.text.startswith('.json'): | |
| handle_json_command(message, bot) | |
| elif message.text and message.text.startswith('.id'): | |
| handle_id_command(message, bot) | |
| elif message.text and message.text.startswith('.mute'): | |
| handle_mute_command(message, bot, thread_local) | |
| elif message.text and message.text.startswith('.unmute'): | |
| handle_unmute_command(message, bot, thread_local) | |
| else: | |
| handle_message(message, bot, thread_local) | |
| logging.info("Webhook 已处理") | |
| return Response(status_code=200) | |
| except Exception as e: | |
| logging.error(f"Webhook 处理失败: {e}", exc_info=True) | |
| return Response(status_code=500) | |
| def should_process_message(message: telebot.types.Message, bot, bot_username): | |
| direct_commands = [".set", ".clear", ".resetdb", ".json", ".id", ".mute", ".unmute"] | |
| if message.chat.type == 'private': | |
| return True | |
| elif message.chat.type in ['group', 'supergroup']: | |
| if message.reply_to_message and message.reply_to_message.from_user.id == bot.get_me().id: | |
| return True | |
| elif message.text: | |
| at_mention = f"@{bot_username}" | |
| if at_mention in message.text: | |
| return True | |
| for command in direct_commands: | |
| if message.text.startswith(command): | |
| return True | |
| elif message.photo: | |
| return True | |
| return False |