python-telegram-bot 在会话处理程序之间传递参数
python-telegram-bot Pass argument between conversation handlers
我正在尝试编写一个机器人,用户在其中单击命令,发送 link 作为消息,然后机器人将 link 添加到某个数据库。它的外观如下:
所以我想我应该使用 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
中生成)。但我不知道如何传递这个变量。我该怎么做?
借助这个article,我就这样解决了
By using context.user_data
in any Handler callback, you have access to a user-specific dict.
所以我的代码将更改如下:
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"]
非常简单直观。这是输出:
我正在尝试编写一个机器人,用户在其中单击命令,发送 link 作为消息,然后机器人将 link 添加到某个数据库。它的外观如下:
所以我想我应该使用 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
中生成)。但我不知道如何传递这个变量。我该怎么做?
借助这个article,我就这样解决了
By using
context.user_data
in any Handler callback, you have access to a user-specific dict.
所以我的代码将更改如下:
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"]
非常简单直观。这是输出: