如何处理电报机器人的 InlineKeyboardButtons 回调
How to handle callbacks for InlineKeyboardButtons for telegram bots
如何处理内联键盘的回调?我试图在此处查看有关此主题的文档,但没有找到任何可以帮助我解决此问题的内容。
这是我想要实现的,如果用户按下任何按钮,机器人就会知道按下了哪个按钮并对其进行处理。到目前为止,这是我的代码片段。
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 结果处理更新的处理器正在调用 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))
Here you can find an example of bot that works with InlineKeyboardButton
and utilize django-tgbot
图书馆。
如何处理内联键盘的回调?我试图在此处查看有关此主题的文档,但没有找到任何可以帮助我解决此问题的内容。
这是我想要实现的,如果用户按下任何按钮,机器人就会知道按下了哪个按钮并对其进行处理。到目前为止,这是我的代码片段。
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 结果处理更新的处理器正在调用 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))
Here you can find an example of bot that works with InlineKeyboardButton
and utilize django-tgbot
图书馆。