对话处理程序之间的python电报bot Pass参数

对话处理程序之间的python电报bot Pass参数,python,python-telegram-bot,Python,Python Telegram Bot,我正在尝试编写一个bot,其中用户单击命令,将链接作为消息发送,然后bot将链接添加到某个数据库。下面是它的外观: 所以我想我应该使用一个会话处理程序。下面是我写的,bot.py: 从telegram.ext导入(更新程序、CommandHandler、MessageHandler、过滤器、, 会话处理程序) 从设置导入BOT\u令牌 导入命令 def main(): updater=updater(BOT\u令牌,use\u context=True) dispatcher=updater.

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

所以我想我应该使用一个
会话处理程序
。下面是我写的,
bot.py

从telegram.ext导入(更新程序、CommandHandler、MessageHandler、过滤器、,
会话处理程序)
从设置导入BOT\u令牌
导入命令
def main():
updater=updater(BOT\u令牌,use\u context=True)
dispatcher=updater.dispatcher
会话=会话处理程序(
入境点=[
消息处理程序(
(Filters.command&Filters.regex(“al_u(.*)),
commands.add_链接
)
],
州={
commands.ADD_链接:[
MessageHandler(Filters.entity(“url”)、commands.receive\u链接)
]
},
回退=[]
)
dispatcher.add_处理程序(CommandHandler(“search”,commands.search))
dispatcher.add_处理程序(对话)
updater.start_polling()
updater.idle()
如果名称=“\uuuuu main\uuuuuuuu”:
main()
和命令位于另一个名为
commands.py的文件中:

从telegram.ext导入ConversationHandler
添加链接=范围(1)
def接收链接(更新,上下文):
bot=context.bot
url=update.message.text
chat\u id=update.message.chat.id
bot.send_消息(
chat\u id=chat\u id,
text=“已添加链接。”
)
返回ConversationHandler.END
def添加链接(更新,上下文):
bot=context.bot
uuid=update.message.text.replace(“/al_u3;”,”)
chat\u id=update.message.chat.id
bot.send_消息(
chat\u id=chat\u id,
text=“将链接作为消息发送。”
)
返回添加链接
现在的问题是,我需要能够在我的
receive\u link
函数中使用
uuid
变量(在
add\u link
中生成)。但我不知道如何传递这个变量。我该怎么做呢?

有了这个,我就这样解决了

通过在任何处理程序回调中使用
context.user\u data
,您可以访问特定于用户的dict

因此,我的代码将更改如下:

从telegram.ext导入ConversationHandler
添加链接=范围(1)
def接收链接(更新,上下文):
bot=context.bot
url=update.message.text
chat\u id=update.message.chat.id
uuid=context.user\u数据[“uuid”]
bot.send_消息(
chat\u id=chat\u id,
text=f“链接已添加到{uuid}.”
)
返回ConversationHandler.END
def添加链接(更新,上下文):
bot=context.bot
uuid=update.message.text.replace(“/al_u3;”,”)
context.user_data[“uuid”]=uuid
chat\u id=update.message.chat.id
bot.send_消息(
chat\u id=chat\u id,
text=f“将链接作为消息发送。”
)
返回添加链接
我将
uuid
变量存储如下:

context.user_data["uuid"] = uuid
uuid = context.user_data["uuid"]
然后像这样使用它:

context.user_data["uuid"] = uuid
uuid = context.user_data["uuid"]
非常简单和直观。以下是输出: