Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple callback query handlers?

Is it possible with the python-telegram-bot wrapper to use more than one callback query handler?

I would like to have multiple unique handlers but as far as I can tell, there can only be one. This means I'd have to base which inline keyboards I show on the initiating message text.

Is there something I'm missing?

like image 582
George knife Avatar asked Dec 17 '16 06:12

George knife


2 Answers

You can use CallbackQueryHandler pattern argument. Regex pattern to test telegram.CallbackQuery.data against.

def motd(bot, update):
    motd_keyboard = [[InlineKeyboardButton('I agree',
        callback_data='motd_callback_button')]]
    motd_markup = InlineKeyboardMarkup(motd_keyboard)
    update.message.reply_text('Message of the day',
        reply_markup=motd_markup)

def motd_callback_button(bot, update):
    pass

def main():
    dp = DjangoTelegramBot.dispatcher
    dp.add_handler(CallbackQueryHandler(motd_callback_button, 
        pattern='^motd_callback_button$'))
like image 39
Yuri P.D. Avatar answered Jan 05 '23 01:01

Yuri P.D.


You can use ConversationHandler wrapper for that. Check code bellow:

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, ConversationHandler

TELEGRAM_HTTP_API_TOKEN = 'PASTE_TELEGRAM_HTTP_API_TOKEN'

FIRST, SECOND = range(2)

def start(bot, update):
    keyboard = [
        [InlineKeyboardButton(u"Next", callback_data=str(FIRST))]
    ]
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text(
        u"Start handler, Press next",
        reply_markup=reply_markup
    )
    return FIRST

def first(bot, update):
    query = update.callback_query
    keyboard = [
        [InlineKeyboardButton(u"Next", callback_data=str(SECOND))]
    ]
    reply_markup = InlineKeyboardMarkup(keyboard)
    bot.edit_message_text(
        chat_id=query.message.chat_id,
        message_id=query.message.message_id,
        text=u"First CallbackQueryHandler, Press next"
    )

    reply_markup = InlineKeyboardMarkup(keyboard)

    bot.edit_message_reply_markup(
        chat_id=query.message.chat_id,
        message_id=query.message.message_id,
        reply_markup=reply_markup
    )
    return SECOND

def second(bot, update):
    query = update.callback_query
    bot.edit_message_text(
        chat_id=query.message.chat_id,
        message_id=query.message.message_id,
        text=u"Second CallbackQueryHandler"
    )
    return

updater = Updater(TELEGRAM_HTTP_API_TOKEN)

conv_handler = ConversationHandler(
    entry_points=[CommandHandler('start', start)],
    states={
        FIRST: [CallbackQueryHandler(first)],
        SECOND: [CallbackQueryHandler(second)]
    },
    fallbacks=[CommandHandler('start', start)]
)

updater.dispatcher.add_handler(conv_handler)

updater.start_polling()

updater.idle()
like image 167
ip0000h Avatar answered Jan 05 '23 02:01

ip0000h