如何处理嵌入式键盘的回调?我试图在此处查看有关此主题的文档,但是没有找到任何可以帮助我解决此问题的文档。
这是我想要实现的目标,如果用户按下任何按钮,则机器人会知道按下了哪个按钮并对其进行了处理。到目前为止,这是我的代码段。
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')
]
]
)
)
应该使用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
并加以利用的机器人示例django-tgbot
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句