File size: 3,407 Bytes
4ffd549
 
 
43e4c76
 
 
 
cd182cf
 
4ffd549
 
 
43e4c76
4ffd549
 
 
 
 
 
 
 
 
 
cd182cf
 
 
4ffd549
 
 
 
 
 
 
 
 
 
 
cd182cf
4ffd549
 
 
 
 
 
 
 
 
 
cd182cf
 
 
 
4ffd549
 
 
 
 
 
 
 
 
 
cd182cf
 
4ffd549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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