【问题标题】:python-telegram-bot Pass argument between conversation handlerspython-telegram-bot 在对话处理程序之间传递参数
【发布时间】:2020-04-06 05:44:18
【问题描述】:

我正在尝试编写一个机器人,其中用户单击命令,将链接作为消息发送,然后机器人将链接添加到某个数据库。下面是它的外观:

所以我想我应该使用ConversationHandler。这是我写的,bot.py:

from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters,
        ConversationHandler)
from settings import BOT_TOKEN
import commands

def main():
    updater = Updater(BOT_TOKEN, use_context=True)
    dispatcher = updater.dispatcher

    conversation = ConversationHandler(
            entry_points=[
                MessageHandler(
                    (Filters.command & Filters.regex("al_(.*)")),
                    commands.add_link
                )
            ],
            states={
                commands.ADD_LINK: [
                    MessageHandler(Filters.entity("url"), commands.receive_link)
                ]
            },
            fallbacks=[]
    )

    dispatcher.add_handler(CommandHandler("search", commands.search))
    dispatcher.add_handler(conversation)

    updater.start_polling()
    updater.idle()

if __name__ == "__main__":
    main()

命令在另一个名为commands.py的文件中:

from telegram.ext import ConversationHandler

ADD_LINK = range(1)

def receive_link(update, context):
    bot = context.bot
    url = update.message.text
    chat_id = update.message.chat.id

    bot.send_message(
            chat_id=chat_id,
            text="The link has been added."
    )
    return ConversationHandler.END

def add_link(update, context):
    bot = context.bot
    uuid = update.message.text.replace("/al_", "")
    chat_id = update.message.chat.id
    bot.send_message(
            chat_id=chat_id,
            text="Send the link as a message."
    )

    return ADD_LINK

现在的问题是我需要能够在我的receive_link 函数中使用uuid 变量(在add_link 中生成)。但我不知道如何传递这个变量。我该怎么做?

【问题讨论】:

    标签: python python-telegram-bot


    【解决方案1】:

    在这个article的帮助下,我这样解决了。

    通过在任何处理程序回调中使用context.user_data,您可以访问特定于用户的字典。

    所以我的代码会改变如下:

    from telegram.ext import ConversationHandler
    
    ADD_LINK = range(1)
    
    def receive_link(update, context):
        bot = context.bot
        url = update.message.text
        chat_id = update.message.chat.id
        uuid = context.user_data["uuid"]
    
        bot.send_message(
                chat_id=chat_id,
                text=f"The link has been added to '{uuid}'."
        )
        return ConversationHandler.END
    
    def add_link(update, context):
        bot = context.bot
        uuid = update.message.text.replace("/al_", "")
        context.user_data["uuid"] = uuid
        chat_id = update.message.chat.id
        bot.send_message(
                chat_id=chat_id,
                text=f"Send the link as a message."
        )
    
        return ADD_LINK
    

    我这样存储uuid 变量:

    context.user_data["uuid"] = uuid
    

    并像这样使用它:

    uuid = context.user_data["uuid"]
    

    非常简单直观。这是输出:

    【讨论】:

      猜你喜欢
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 2020-06-20
      • 2016-05-05
      • 2021-12-06
      相关资源
      最近更新 更多