基于热解图的电报机器人的内联按钮没有响应

问题描述 投票:0回答:1

所以我正在用pyrogram制作一个电报机器人,它将帮助用户在rest api的帮助下提供视频讲座链接。当我使用 /lecture 与此代码时,机器人通过内联按钮完美发送消息。但是当我点击按钮时,它没有响应。

import re
import requests
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup

from bot import Bot

@Bot.on_message(filters.command('lecture'))
async def lectures_command(client, message):
    # Create the inline keyboard with subjects
    keyboard = [
        [
            InlineKeyboardButton("Physics", callback_data="subject_physics"),
            InlineKeyboardButton("Maths", callback_data="subject_maths"),
        ],
        [
            InlineKeyboardButton("Organic Chemistry", callback_data="subject_organic"),
            InlineKeyboardButton("Inorganic Chemistry", callback_data="subject_inorganic"),
        ],
        [
            InlineKeyboardButton("Physical Chemistry", callback_data="subject_physical"),
        ]
    ]
    # Create the InlineKeyboardMarkup object
    reply_markup = InlineKeyboardMarkup(keyboard)
    # Send the message with the inline keyboard
    await message.reply_text("Choose a subject from below please:", reply_markup=reply_markup)

@Bot.on_callback_query(filters.regex(r'^subject_.*'))
async def subject_callback(_, query):
    print('ok')
    subject = query.data.split("_")[1]
    print('subject:', subject)
    # Fetch data from the API
    response = requests.get(f"https://MY-REST-API/teachers?subject={subject}")
    if response.status_code == 200:
        teachers_data = response.json()
        # Create inline buttons for each teacher
        buttons = [InlineKeyboardButton(teacher, callback_data=f"teacher_{subject}_{teacher}") for teacher in teachers_data[0]["teachers"]]
        # Add a button for each subject
        buttons.append(InlineKeyboardButton("Choose a subject", callback_data="subject_choice"))
        # Create InlineKeyboardMarkup object with buttons
        reply_markup = InlineKeyboardMarkup([buttons])
        # Edit the callback message to choose a teacher
        await query.message.edit_text(f"Choose a teacher for {subject}:", reply_markup=reply_markup)
    else:
        await query.message.edit_text("Failed to fetch data from the API. Please try again later.")

@Bot.on_callback_query(filters.regex(r'^teacher_.*'))
async def teacher_callback(_, query):
    data_parts = re.split(r'_', query.data)
    subject = data_parts[1]
    teacher_name = data_parts[2]
    # Fetch data from the API for chapters
    response = requests.get(f"https://MY-REST-API/chapters?subject={subject}&teacher={teacher_name}")
    if response.status_code == 200:
        chapters_data = response.json()
        # Create inline buttons for each chapter
        buttons = [InlineKeyboardButton(chapter, callback_data=f"chapter_{subject}_{teacher_name}_{chapter}") for chapter in chapters_data["chapters"]]
        # Create InlineKeyboardMarkup object with buttons
        reply_markup = InlineKeyboardMarkup([buttons])
        # Edit the callback message to choose a chapter
        await query.message.edit_text("Please choose a chapter from below buttons:", reply_markup=reply_markup)
    else:
        await query.message.edit_text("Failed to fetch data from the API. Please try again later.")

@Bot.on_callback_query(filters.regex(r'^chapter_.*'))
async def chapter_callback(_, query):
    data_parts = re.split(r'_', query.data)
    subject = data_parts[1]
    teacher_name = data_parts[2]
    chapter_name = data_parts[3]
    # Fetch data from the API for the lecture link
    response = requests.get(f"https://MY-REST-API/lecture?subject={subject}&teacher={teacher_name}&ch={chapter_name}")
    if response.status_code == 200 and response.json()["success"]:
        lecture_link = response.json()["link"]
        # Send the lecture link to the user
        await query.message.reply_text(f"Here's the lecture link for {chapter_name} by {teacher_name}: {lecture_link}")
    else:
        await query.message.reply_text("Failed to fetch lecture link. Please try again later.")

为了检查问题出在哪里,我尝试了打印,但没有成功,并且在日志中它没有打印任何内容。即使他们在记录器中也没有错误。我想也许我使用了错误的正则表达式!

我也尝试过startswith方法,但问题还是一样。

import re
import requests
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup

from bot import Bot

@Bot.on_message(filters.command('lecture'))
async def lectures_command(client, message):
    # Create the inline keyboard with subjects
    keyboard = [
        [
            InlineKeyboardButton("Physics", callback_data="subject_physics"),
            InlineKeyboardButton("Maths", callback_data="subject_maths"),
        ],
        [
            InlineKeyboardButton("Organic Chemistry", callback_data="subject_organic"),
            InlineKeyboardButton("Inorganic Chemistry", callback_data="subject_inorganic"),
        ],
        [
            InlineKeyboardButton("Physical Chemistry", callback_data="subject_physical"),
        ]
    ]
    # Create the InlineKeyboardMarkup object
    reply_markup = InlineKeyboardMarkup(keyboard)
    # Send the message with the inline keyboard
    await message.reply_text("Choose a subject from below please:", reply_markup=reply_markup)

@Bot.on_callback_query()
async def handle_callback(_, query):
    if query.data.startswith("subject_"):
        print('ok')
        subject = query.data.split("_")[1]
        # Fetch data from the API
        response = requests.get(f"https://MY-REST-API/teachers?subject={subject}")
        if response.status_code == 200:
            teachers_data = response.json()
            # Create inline buttons for each teacher
            buttons = [InlineKeyboardButton(teacher, callback_data=f"teacher_{subject}_{teacher}") for teacher in teachers_data[0]["teachers"]]
            # Add a button for each subject
            buttons.append(InlineKeyboardButton("Choose a subject", callback_data="subject_choice"))
            # Create InlineKeyboardMarkup object with buttons
            reply_markup = InlineKeyboardMarkup([buttons])
            # Edit the callback message to choose a teacher
            await query.message.edit_text(f"Choose a teacher for {subject}:", reply_markup=reply_markup)
        else:
            await query.message.edit_text("Failed to fetch data from the API. Please try again later.")
    elif query.data.startswith("teacher_"):
        data_parts = query.data.split("_")
        subject = data_parts[1]
        teacher_name = data_parts[2]
        # Fetch data from the API for chapters
        response = requests.get(f"https://MY-REST-API/chapters?subject={subject}&teacher={teacher_name}")
        if response.status_code == 200:
            chapters_data = response.json()
            # Create inline buttons for each chapter
            buttons = [InlineKeyboardButton(chapter, callback_data=f"chapter_{subject}_{teacher_name}_{chapter}") for chapter in chapters_data["chapters"]]
            # Create InlineKeyboardMarkup object with buttons
            reply_markup = InlineKeyboardMarkup([buttons])
            # Edit the callback message to choose a chapter
            await query.message.edit_text("Please choose a chapter from below buttons:", reply_markup=reply_markup)
        else:
            await query.message.edit_text("Failed to fetch data from the API. Please try again later.")
    elif query.data.startswith("chapter_"):
        data_parts = query.data.split("_")
        subject = data_parts[1]
        teacher_name = data_parts[2]
        chapter_name = data_parts[3]
        # Fetch data from the API for the lecture link
        response = requests.get(f"https://MY-REST-API/lecture?subject={subject}&teacher={teacher_name}&ch={chapter_name}")
        if response.status_code == 200 and response.json()["success"]:
            lecture_link = response.json()["link"]
            # Send the lecture link to the user
            await query.message.reply_text(f"Here's the lecture link for {chapter_name} by {teacher_name}: {lecture_link}")
        else:
            await query.message.reply_text("Failed to fetch lecture link. Please try again later.")
    else:
        print('no ok')

python regex telegram pyrogram
1个回答
0
投票

我尝试了您提供的第一段代码,该代码按预期工作。在这种情况下,如果没有错误,我通常使用 RawUpdateHandler 或在您的情况下使用不带任何过滤器的 CallbackQueryHandler 处理程序。然后将处理程序

group=-1
设置为优先级并打印更新以检查机器人是否捕获任何更新。为了更好地调试,请添加以下代码并运行您的机器人以查看它捕获了哪些更新。

@Bot.on_raw_update(group=-1)
async def log_updates(client, update, users, chats):
    print(update)

或仅用于回调查询更新:

@Bot.on_callback_query(group=-1)
async def log_callback_queries(client, update):
    print(update.data)
© www.soinside.com 2019 - 2024. All rights reserved.