带有 python-telegram-bot 库的电报机器人
Telegram bot with python-telegram-bot library
我对一个可能非常简单的问题不以为然,但我刚刚开始研究 Telegram API,特别是 python-telegram-bot 库,所以请原谅我,如果这是一个天真的问题。简而言之,我想制作一个非常简单的机器人,它允许组成员将 "objects"(如通用内容,而不是 python 对象)连同成员姓名一起添加到列表中谁拥有该对象(这是通过 /add 命令完成的)。我将对象存储为字典的键,其值是成员的名称。
这是我到目前为止提出的代码:
import logging
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler, Filters
list_of_stuff = {}
updater = Updater(token='698307431:AAG9kExmheQ5hLvBieJNtsTqV9M4U_GNFv0')
dispatcher = updater.dispatcher
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
updater.start_polling()
def start(bot,update):
bot.send_message(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")
def obj(bot, update):
list_of_stuff[update.message.text] = update.message.from_user.username
def unknown(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="Sorry, I didn't understand that command.")
def add(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="Choose an object.")
dispatcher.add_handler(MessageHandler(Filters.text, obj))
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CommandHandler('add',add))
dispatcher.add_handler(MessageHandler(Filters.command, unknown))
如您所见,存在一个问题:在 obj 函数中,我将发件人的用户名设置为值,而密钥是 "object" 我假设将由同一发件人输入。问题是,我只想使用 /add 命令来添加 "object" 作为键和用户名作为值,我希望组中的每个成员都可以这样做(所以它每个成员都应该可以为其他成员添加 "objects")。但我不知道是否可以在同一个函数(在本例中为 /add 命令)中以同步方式添加两个消息处理程序,即要求用户添加密钥,等待响应,然后then and only then ask to add the value,这意味着第二个 send_message 应该只在用户输入密钥后出现。
如果您想使用 python-telegram-bot 提问并等待回复,您可以使用 ConversationHandler
ConversationHandler 似乎是最适合您的解决方案。您需要规划您的对话流程。您希望对话中的哪些状态(步骤)?
查看存储库中 ConversationHandler 的简单示例:ConversationHandler State Diagram
- 灰框是州
- 红框是给用户的消息
- 绿框是命令/键盘选择
您可以使用不同的过滤器类型来确保获得所需的对象类型,请参阅 Filters Module
我相信他们的 example code 有您要找的东西。
我对一个可能非常简单的问题不以为然,但我刚刚开始研究 Telegram API,特别是 python-telegram-bot 库,所以请原谅我,如果这是一个天真的问题。简而言之,我想制作一个非常简单的机器人,它允许组成员将 "objects"(如通用内容,而不是 python 对象)连同成员姓名一起添加到列表中谁拥有该对象(这是通过 /add 命令完成的)。我将对象存储为字典的键,其值是成员的名称。 这是我到目前为止提出的代码:
import logging
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler, Filters
list_of_stuff = {}
updater = Updater(token='698307431:AAG9kExmheQ5hLvBieJNtsTqV9M4U_GNFv0')
dispatcher = updater.dispatcher
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
updater.start_polling()
def start(bot,update):
bot.send_message(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")
def obj(bot, update):
list_of_stuff[update.message.text] = update.message.from_user.username
def unknown(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="Sorry, I didn't understand that command.")
def add(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="Choose an object.")
dispatcher.add_handler(MessageHandler(Filters.text, obj))
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CommandHandler('add',add))
dispatcher.add_handler(MessageHandler(Filters.command, unknown))
如您所见,存在一个问题:在 obj 函数中,我将发件人的用户名设置为值,而密钥是 "object" 我假设将由同一发件人输入。问题是,我只想使用 /add 命令来添加 "object" 作为键和用户名作为值,我希望组中的每个成员都可以这样做(所以它每个成员都应该可以为其他成员添加 "objects")。但我不知道是否可以在同一个函数(在本例中为 /add 命令)中以同步方式添加两个消息处理程序,即要求用户添加密钥,等待响应,然后then and only then ask to add the value,这意味着第二个 send_message 应该只在用户输入密钥后出现。
如果您想使用 python-telegram-bot 提问并等待回复,您可以使用 ConversationHandler
ConversationHandler 似乎是最适合您的解决方案。您需要规划您的对话流程。您希望对话中的哪些状态(步骤)?
查看存储库中 ConversationHandler 的简单示例:ConversationHandler State Diagram
- 灰框是州
- 红框是给用户的消息
- 绿框是命令/键盘选择
您可以使用不同的过滤器类型来确保获得所需的对象类型,请参阅 Filters Module
我相信他们的 example code 有您要找的东西。