인라인 키보드에 대한 콜백을 어떻게 처리합니까? 여기에서이 주제에 관한 문서를 확인하려고했지만이 문제를 해결하는 데 도움이되는 내용을 찾지 못했습니다.
사용자가 아무 버튼이나 누르면 봇이 어떤 버튼이 눌 렸는지 알고 그로 뭔가를 수행하는 것입니다. 지금까지 내 코드 스 니펫이 있습니다.
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] 삭제
몇 마디 만하겠습니다