5

I'm trying to use CallbackQueryHandler() with my bot, but i can't make it work for my case . The code of the bot that i tried is the same as this sample :

def start(bot, update):
    keyboard = [[InlineKeyboardButton("Option 1", callback_data='1'),
                 InlineKeyboardButton("Option 2", callback_data='2')],
                [InlineKeyboardButton("Option 3", callback_data='3')]]

    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text('Please choose:', reply_markup=reply_markup)

def button(bot, update):
    query = update.callback_query
    bot.edit_message_text(text="Selected option: %s" % query.data,
                          chat_id=query.message.chat_id,
                          message_id=query.message.message_id)
def help(bot, update):
    update.message.reply_text("Use /start to test this bot.")

def error(bot, update, error):
    logging.warning('Update "%s" caused error "%s"' % (update, error))

# Create the Updater and pass it your bot's token.
updater = Updater(TOKEN)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.dispatcher.add_handler(CommandHandler('help', help))
updater.dispatcher.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT
updater.idle()
Saad
  • 1,047
  • 2
  • 19
  • 32
atareao
  • 151
  • 3

2 Answers2

0

Looks like you used this inlinekeyboard.py example. It works good for me.

Probably you have some problem with TOKEN, internet connection or something else. Code is ok.

To be sure, please configure logging to DEBUG level, so you'll see more verbose logs:

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.DEBUG)
wowkin2
  • 5,895
  • 5
  • 23
  • 66
0

Using a CallbackQueryHandler implies using query.answer(), even if not is answered. Try like this:

def button(bot, update):
    query = update.callback_query
    query.answer()
    bot.edit_message_text(text="Selected option: %s" % query.data,
                          chat_id=query.message.chat_id,
                          message_id=query.message.message_id)
Rafael Colombo
  • 339
  • 1
  • 2
  • 8