无法从另一个文件返回状态

问题描述 投票:0回答:1

我有一个文件 main.py ,其中包含以下代码:

MENU = range(0)

class Main():
    
    def __init__(self):
        self.app = Application.builder().token(token=config['TOKEN']).build()
        self.start_handler = Start(MENU).handler()
        self.menu_handler = Menu().handler()

    def run(self) -> None:
        conversation = ConversationHandler(
            entry_points=[self.start_handler],
            states={
                MENU: [self.menu_handler]
            },
            fallbacks=[]
        )
        self.app.add_handler(conversation)
        self.app.run_polling(allowed_updates=Update.ALL_TYPES)

Main().run()

然后我有另一个文件,我想从那里返回“菜单”状态,但我根本无法让它工作。机器人对我说:

'start' returned state range(0, 0) which is unknown to the ConversationHandler.

class Start():
    def __init__(self, MENU):
        self.MENU = MENU
        pass

    async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
        user_info = await get_user_info(update)

        keyboard = [
                [
                    InlineKeyboardButton('Decline', callback_data='STOP'),
                    InlineKeyboardButton('Accept', callback_data='CHOOSE_NETWORK')
                ]
            ]
        markup = InlineKeyboardMarkup(keyboard)
        get_user_id = await mySQL().select('users', 'user_id', 'WHERE user_id = %s', user_info['user_id'])

        if get_user_id is None:
            await update.message.reply_text('To continue using this bot you need to accept: \n\nThis and this', reply_markup=markup)
            return CHOOSE_NETWORK
        else:
            return self.MENU

我已经尝试了一切。

python bots python-telegram-bot
1个回答
0
投票

这是因为

MENU
状态被定义为
range(0)
,这是一个空范围。当您在
self.MENU
类中返回
Start
时,它会返回这个空范围,而
ConversationHandler
无法将其识别为有效状态,因此会出现错误消息。

您应该将

MENU
定义为可以被
ConversationHandler
识别为状态的特定整数或字符串。例如,您可以将
MENU
定义为 1 或“MENU”。

MENU = 1  # or 'MENU'

class Main():
    
    def __init__(self):
        self.app = Application.builder().token(token=config['TOKEN']).build()
        self.start_handler = Start(MENU).handler()
        self.menu_handler = Menu().handler()

    def run(self) -> None:
        conversation = ConversationHandler(
            entry_points=[self.start_handler],
            states={
                MENU: [self.menu_handler]
            },
            fallbacks=[]
        )
        self.app.add_handler(conversation)
        self.app.run_polling(allowed_updates=Update.ALL_TYPES)

Main().run()

在你的

Start
班上:

class Start():
    def __init__(self, MENU):
        self.MENU = MENU
        pass

    async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
        user_info = await get_user_info(update)

        keyboard = [
                [
                    InlineKeyboardButton('Decline', callback_data='STOP'),
                    InlineKeyboardButton('Accept', callback_data='CHOOSE_NETWORK')
                ]
            ]
        markup = InlineKeyboardMarkup(keyboard)
        get_user_id = await mySQL().select('users', 'user_id', 'WHERE user_id = %s', user_info['user_id'])

        if get_user_id is None:
            await update.message.reply_text('To continue using this bot you need to accept: \n\nThis and this', reply_markup=markup)
            return CHOOSE_NETWORK
        else:
            return self.MENU

这样,当

self.MENU
返回时,就会是
ConversationHandler
可以识别的状态。

© www.soinside.com 2019 - 2024. All rights reserved.