电报机器人的按钮选择和文本回复的混合
Mixture of button selection and text replies from telegram bot
我正在尝试创建简单的 TG 机器人,其中第一次交互是通过单击按钮,然后是输入文本。在 ConversationHandler
中,我正在关注多个可用示例并使用两者,CallbackQueryHandler
处理按钮点击,MessageHandler
处理文本响应。
但是,单击按钮不会保存性别。相反,当打印 user_data
时,它会显示类似 <telegram.callbackquery.CallbackQuery object at 0x7fc2d82c8190>
的内容。
如果有人能指出下面的清单有什么问题,我们将不胜感激。
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler)
from telegram import (Update, InlineKeyboardButton, InlineKeyboardMarkup)
GENDER, NAME, AGE = 0, 1, 2
user_data = {}
def start_handler(update: Update, context):
keyboard = [
[
InlineKeyboardButton("M", callback_data=str(GENDER)),
InlineKeyboardButton("F", callback_data=str(GENDER)),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text("Male / Female", reply_markup=reply_markup)
return GENDER
def gender_handler(update: Update, context):
user_data[GENDER] = update.callback_query
query = update.callback_query
query.answer()
query.edit_message_text("enter name")
return NAME
def name_handler (update: Update, context):
user_data[NAME] = update.message.text
update.message.reply_text("enter age")
return AGE
def finish_handler(update: Update, context):
user_data[AGE] = update.message.text
print(user_data)
update.message.reply_text(f"G: {user_data[GENDER]}, name: {user_data[NAME]}, age: {user_data[AGE]}")
return ConversationHandler.END
def cancel_handler(update: Update, context):
print("bye")
def main():
updater = Updater(
token="TOKEN",
use_context=True,
)
dp = updater.dispatcher
conv_handler = ConversationHandler(
entry_points=[
CommandHandler("start", start_handler),
],
states={
GENDER: [CallbackQueryHandler(gender_handler)],
NAME: [MessageHandler(Filters.all, name_handler)],
AGE: [MessageHandler(Filters.all, finish_handler)]
},
fallbacks=[CommandHandler("cancel", cancel_handler)],
)
dp.add_handler(conv_handler)
updater.start_polling()
updater.idle()
main()
问题出在这一行:
user_data[GENDER] = update.callback_query
Update.callback_query
is an instance of the class CallbackQuery
。因此,您将完整的对象保存到 user_data[GENDER]
,而您想知道的只是所选的性别。为此,您应该
- 更新键盘,使每个按钮都有唯一的
callback_data
- 访问
update.callback_query.data
以检查按下了哪个按钮
免责声明:我目前是 python-telegram-bot
.
的维护者
我正在尝试创建简单的 TG 机器人,其中第一次交互是通过单击按钮,然后是输入文本。在 ConversationHandler
中,我正在关注多个可用示例并使用两者,CallbackQueryHandler
处理按钮点击,MessageHandler
处理文本响应。
但是,单击按钮不会保存性别。相反,当打印 user_data
时,它会显示类似 <telegram.callbackquery.CallbackQuery object at 0x7fc2d82c8190>
的内容。
如果有人能指出下面的清单有什么问题,我们将不胜感激。
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler)
from telegram import (Update, InlineKeyboardButton, InlineKeyboardMarkup)
GENDER, NAME, AGE = 0, 1, 2
user_data = {}
def start_handler(update: Update, context):
keyboard = [
[
InlineKeyboardButton("M", callback_data=str(GENDER)),
InlineKeyboardButton("F", callback_data=str(GENDER)),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text("Male / Female", reply_markup=reply_markup)
return GENDER
def gender_handler(update: Update, context):
user_data[GENDER] = update.callback_query
query = update.callback_query
query.answer()
query.edit_message_text("enter name")
return NAME
def name_handler (update: Update, context):
user_data[NAME] = update.message.text
update.message.reply_text("enter age")
return AGE
def finish_handler(update: Update, context):
user_data[AGE] = update.message.text
print(user_data)
update.message.reply_text(f"G: {user_data[GENDER]}, name: {user_data[NAME]}, age: {user_data[AGE]}")
return ConversationHandler.END
def cancel_handler(update: Update, context):
print("bye")
def main():
updater = Updater(
token="TOKEN",
use_context=True,
)
dp = updater.dispatcher
conv_handler = ConversationHandler(
entry_points=[
CommandHandler("start", start_handler),
],
states={
GENDER: [CallbackQueryHandler(gender_handler)],
NAME: [MessageHandler(Filters.all, name_handler)],
AGE: [MessageHandler(Filters.all, finish_handler)]
},
fallbacks=[CommandHandler("cancel", cancel_handler)],
)
dp.add_handler(conv_handler)
updater.start_polling()
updater.idle()
main()
问题出在这一行:
user_data[GENDER] = update.callback_query
Update.callback_query
is an instance of the class CallbackQuery
。因此,您将完整的对象保存到 user_data[GENDER]
,而您想知道的只是所选的性别。为此,您应该
- 更新键盘,使每个按钮都有唯一的
callback_data
- 访问
update.callback_query.data
以检查按下了哪个按钮
免责声明:我目前是 python-telegram-bot
.