如何为电报机器人处理InlineKeyboardButtons的回调

时间:2020-09-10 15:21:10

标签: python telegram-bot

如何处理嵌入式键盘的回调?我试图在此处查看有关此主题的文档,但是没有找到任何可以帮助我解决此问题的文件。

这是我想要实现的目标,如果用户按下任何按钮,则机器人会知道按下了哪个按钮并对其进行了处理。到目前为止,这是我的代码段。

from django_tgbot.decorators import processor
from django_tgbot.state_manager import message_types, update_types, state_types
from django_tgbot.types.update import Update
from ..bot import state_manager
from ..models import TelegramState
from ..bot import TelegramBot


from django_tgbot.types.inlinekeyboardbutton import InlineKeyboardButton
from django_tgbot.types.inlinekeyboardmarkup import InlineKeyboardMarkup
from django_tgbot.types.keyboardbutton import KeyboardButton
from django_tgbot.types.replykeyboardremove import ReplyKeyboardRemove

from django_tgbot.types.replykeyboardmarkup import ReplyKeyboardMarkup



@processor(state_manager,success='asked_to_select_main_menu_options')
def main_manu_options(bot: TelegramBot, update: Update, state: TelegramState):
    chat_id = update.get_chat().get_id()

    bot.sendMessage(
        chat_id,
        text='What would you like to do?',
        reply_markup=InlineKeyboardMarkup.a(
            inline_keyboard=[
                [
                    InlineKeyboardButton.a('Parking',callback_data='PK'),
                ],
                [
                    InlineKeyboardButton.a('SBP', callback_data='SBP')
                ],
                [
                    InlineKeyboardButton.a('Land Rates',callback_data='LR')
                ],
                [
                    InlineKeyboardButton.a('Rent',callback_data='R')
                ],
                [
                    InlineKeyboardButton.a('Bill Payment',callback_data='B')
                ]
            ]
        )
    )

1 个答案:

答案 0 :(得分:1)

应使用InlineKeyboardButton结果处理Update的处理器正在调用CallbackQuery,并且应将其添加到update_type中作为参数,请参见示例:

@processor(state_manager, from_states=state_types.All, update_types=[update_types.CallbackQuery])
def handle_callback_query(bot: TelegramBot, update, state):
    callback_data = update.get_callback_query().get_data()
    bot.answerCallbackQuery(update.get_callback_query().get_id(), text='Callback data received: {}'.format(callback_data))
InlineKeyboardButton一起使用并利用Here you can find an example库的漫游器的

django-tgbot