我有下面的ConversationHandler
conv_handler = ConversationHandler(
entry_points=[CommandHandler('entry' entry)],
states={
ONE: [MessageHandler(filters.TEXT, one)],
TWO: [MessageHandler(filters.TEXT, two)],
THREE: [CallbackQueryHandler(three)],
FOUR: [CallbackQueryHandler(four)]
},
fallbacks=[CommandHandler(Commands.CANCEL.value, cancel)],
)我将状态键定义如下:ONE, TWO, THREE, FOUR = range(4)
输入功能:
async def entry(update: Update, context: ContextTypes) -> int:
await update.message.reply_text("bla")
return ONE一项职能:
async def one(update: Update, context: ContextTypes) -> int:
x = update.message.text
await update.message.reply_text("yes. bla.")
context.user_data["x"] = x
return TWO两项职能:
async def two(update: Update, context: ContextTypes) -> int:
await update.message.reply_text("bla")
keyboard = [
['7', '8', '9'],
['4', '5', '6'],
['1', '2', '3'],
['0']
]
await update.message.reply_text("bla", reply_markup=InlineKeyboardMarkup(keyboard))
query = update.callback_query
await query.answer()
context.user_data["x"] = query.data
return THREE入口函数到一个函数之间的转换是有效的,但是第一个和第二个条目之间的转换不起作用。
在调试过程中,当我要返回一个时,对象self.map_to_parent为None,所以我想有一些解析错误(?)在ConversationHandler但是我找不到它。
更多信息:
displayed
编辑:
我固定了函数的两个键盘,并删除了callback_query的使用
async def two(update: Update, context: ContextTypes) -> int:
await update.message.reply_text("bla")
keyboard = [
[InlineKeyboardButton('7'), InlineKeyboardButton('8'), InlineKeyboardButton('9')],
[InlineKeyboardButton('4'), InlineKeyboardButton('5'), InlineKeyboardButton('6')],
[InlineKeyboardButton('1'), InlineKeyboardButton('2'), InlineKeyboardButton('3')],
[InlineKeyboardButton('0')],
]
await update.message.reply_text("bla", reply_markup=InlineKeyboardMarkup(keyboard))
return THREE不过,一到二之间的转换不起作用,没有错误,但我确实收到了警告:
PTBUserWarning: If 'per_message=False', 'CallbackQueryHandler' will not be tracked for every message. Read this FAQ entry to learn more about the per_* settings: https://github.com/python-telegram-bot/python-telegram-bot/wiki/Frequently-Asked-Questions#what-do-the-per_-settings-in-conversationhandler-do.
conv_handler = ConversationHandler(编辑2
最起码的例子:
import os
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
CommandHandler,
Application,
ContextTypes,
MessageHandler,
filters,
ConversationHandler,
)
BOT_TOKEN = os.getenv("BOT_TOKEN")
ONE, TWO, THREE, FOUR = range(4)
async def entry(update: Update, context: ContextTypes) -> int:
await update.message.reply_text("entry_bla")
return ONE
async def one(update: Update, context: ContextTypes) -> int:
x = update.message.text
await update.message.reply_text("yes. bla.")
context.user_data["x"] = x
return TWO
async def two(update: Update, context: ContextTypes) -> int:
await update.message.reply_text("two_bla")
keyboard = [
[InlineKeyboardButton('7'), InlineKeyboardButton('8'), InlineKeyboardButton('9')],
[InlineKeyboardButton('4'), InlineKeyboardButton('5'), InlineKeyboardButton('6')],
[InlineKeyboardButton('1'), InlineKeyboardButton('2'), InlineKeyboardButton('3')],
[InlineKeyboardButton('0')],
]
await update.message.reply_text("bla", reply_markup=InlineKeyboardMarkup(keyboard))
return THREE
async def cancel(update: Update, context: ContextTypes):
pass
if __name__ == '__main__':
application = Application.builder().token(BOT_TOKEN).build()
conv_handler = ConversationHandler(
entry_points=[CommandHandler('entry', entry)],
states={
ONE: [MessageHandler(filters.TEXT, one)],
TWO: [MessageHandler(filters.TEXT, two)]
},
fallbacks=[CommandHandler('cancel', cancel)]
)
application.add_handler(conv_handler)
application.run_polling()发布于 2022-09-24 19:11:44
使用您的代码片段,我可以从state ONE获得状态TWO,但是函数two被窃听了。
首先,您应该看到一个错误消息AttributeError: 'str' object has no attribute 'to_dict',其中的跟踪指向您的行。
await update.message.reply_text("In two", reply_markup=InlineKeyboardMarkup(keyboard))这是因为InlineKeyboardMarkup需要一个InlineKyeboardButton对象列表,而不是字符串。此外,在two中,您试图调用update.callback_query.answer(),但是由于two是对MessageHandler的回调,所以update.callback_query永远是None。
编辑
使用更新的MWE,我得到以下行为:
/entry、->、bot、entry_bla作为expectedyes. bla.、expectedtwo_bla。附加键盘的第二条消息不会发送,因为InlineKeyboardButtons被错误地初始化了--这正是必须传递的可选参数之一。因此,该示例被困在状态TWO中,并将two_bla回复到任何文本消息免责声明:我目前是python-telegram-bot的维护者
https://stackoverflow.com/questions/73838709
复制相似问题