如何通过 python-telegram-bot 在 for 循环中获取下一条消息?

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

我的问题是如何在 for 循环中从用户的下一条消息中获取文本,下面是我的代码示例:


async def end(update: Update, contex: ContextTypes.DEFAULT_TYPE):
    await update.callback_query.edit_message_reply_markup(reply_markup=None)

    callback_data = update.callback_query.data.split(sep=',')
    subsystem = callback_data[0]
    env = callback_data[1]

    gc = GoogleSheetsUpdater().connect()

    sheet_url = 'https://docs.google.com/spreadsheets/d/' + config['DEFAULT']['spreadsheetId']

    sheet = gc.open_by_url(sheet_url)

    ws = sheet.get_worksheet(0)

    df = pandas.DataFrame(ws.get_all_records())

    apps_list = df['app'][df['subsystem'] == subsystem]

    for app in apps_list:

        tag_text = f"Please, send me a tag fro {app} in {subsystem}:"

        await update.callback_query.edit_message_text(text=tag_text)

        time.sleep(10)

        # I cant get a tag from next user message here
        async def tag_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):

            tag = update.message.text

            print(tag)

            return

而且我不知道为什么这不能正常工作...... 我的建议是像这样放置一个新的 MessageHandler:

        @Application.add_handler(handler=MessageHandler(filters=filters.Text, callback=Update))
        async def tag_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):

            tag = update.message.text

            print(tag)

            return

但它也不起作用。 谁能帮我解决这个问题?

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

python-telegram-bot
中,处理程序回调函数总是一次处理一个更新。不多也不少。无法从处理程序回调函数中等待下一次更新。 为此,PTB 附带了
ConversationHandler
.

此外,

@Application.add_handler
不是
python-telegram-bot
支持的语法。


免责声明:我目前是

python-telegram-bot
.

的维护者
© www.soinside.com 2019 - 2024. All rights reserved.