【发布时间】: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