首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ConversationHandler没有移动到下一个状态

ConversationHandler没有移动到下一个状态
EN

Stack Overflow用户
提问于 2022-09-24 16:10:23
回答 1查看 48关注 0票数 0

我有下面的ConversationHandler

代码语言:javascript
复制
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)

输入功能:

代码语言:javascript
复制
async def entry(update: Update, context: ContextTypes) -> int:
    await update.message.reply_text("bla")
    return ONE

一项职能:

代码语言:javascript
复制
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

两项职能:

代码语言:javascript
复制
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

  • python-telegram-bot = "==20.0a1"

编辑:

我固定了函数的两个键盘,并删除了callback_query的使用

代码语言:javascript
复制
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

不过,一到二之间的转换不起作用,没有错误,但我确实收到了警告:

代码语言:javascript
复制
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

最起码的例子:

代码语言:javascript
复制
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()
EN

Stack Overflow用户

发布于 2022-09-24 19:11:44

使用您的代码片段,我可以从state ONE获得状态TWO,但是函数two被窃听了。

首先,您应该看到一个错误消息AttributeError: 'str' object has no attribute 'to_dict',其中的跟踪指向您的行。

代码语言:javascript
复制
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作为expected
  • type、任何文本、->、bot、yes. bla.、expected
  • type、任何文本、->、bot、two_bla。附加键盘的第二条消息不会发送,因为InlineKeyboardButtons被错误地初始化了--这正是必须传递的可选参数之一。因此,该示例被困在状态TWO中,并将two_bla回复到任何文本消息

免责声明:我目前是python-telegram-bot的维护者

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73838709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档