Hey everyone, I’m stuck with a problem in my Telegram bot. I made it using Pyrogram to help users get video lecture links. The /lecture command works fine and shows inline buttons. But when I click them, nothing happens.
I’ve tried using print statements to debug, but they don’t show up in the logs. There are no errors either. I’m wondering if my regex is wrong or if there’s another issue.
Here’s a simplified version of my code:
@Bot.on_message(filters.command('lecture'))
async def lectures_command(client, message):
keyboard = [
[InlineKeyboardButton("Math", callback_data="subject_math"),
InlineKeyboardButton("Science", callback_data="subject_science")]
]
reply_markup = InlineKeyboardMarkup(keyboard)
await message.reply_text("Pick a subject:", reply_markup=reply_markup)
@Bot.on_callback_query(filters.regex(r'^subject_.*'))
async def subject_callback(_, query):
print('Callback received')
subject = query.data.split("_")[1]
await query.message.edit_text(f"You picked {subject}")
Any ideas on what might be causing this? Thanks for your help!
Have you considered checking your bot’s permissions? Sometimes, unresponsive inline buttons can be due to the bot lacking the necessary permissions to edit messages or send callbacks. Ensure your bot has the ‘Can Edit Messages’ permission enabled in the group or channel where you’re testing it.
Also, it’s worth verifying that your bot token is correct and up-to-date. If you’ve regenerated the token recently, make sure you’ve updated it in your code.
Lastly, try adding some logging instead of print statements. Pyrogram has built-in logging that might give you more insight:
import logging
logging.basicConfig(level=logging.INFO)
Add this at the start of your script. It might reveal issues that aren’t apparent with print statements alone.
hey man, have u tried clearing ur bot’s webhook? sometimes that can mess with callbacks. also, make sure ur using the latest pyrogram version. older ones had some callback issues.
try running this before starting ur bot:
await bot.delete_webhook()
if that doesnt help, maybe post ur full code somewhere so we can take a closer look. good luck!
I’ve encountered a similar issue with Pyrogram callbacks before. The problem might be that you haven’t registered the callback query handler properly. Make sure you’ve added the necessary decorator for callback queries.
Try modifying your code like this:
@Bot.on_callback_query()
async def callback_query_handler(client, query):
print('Callback received')
if query.data.startswith('subject_'):
subject = query.data.split('_')[1]
await query.answer(f'You picked {subject}')
await query.message.edit_text(f'You picked {subject}')
This should catch all callback queries and handle the ones starting with ‘subject_’. Also, don’t forget to call query.answer() to acknowledge the callback to Telegram.
If this doesn’t work, double-check that you’ve enabled inline mode for your bot via BotFather. Sometimes that can be the culprit for unresponsive buttons.
Let me know if this helps or if you need further assistance!