1
votes

I'm at odds with what's probably a very simple problem, but I've just started looking into the Telegram API, and specifically the python-telegram-bot library, so forgive me if this is a naive question. Simply put, I want to make a very simple bot which allows the members of the group to add "objects"(as in generic stuff, not a python object) to a list together with the name of the member who owns the object (this is done through an /add command). I store the objects as keys of a dictionary, whose values are the names of the members. Here's the code I've come up with so far:

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))

As you can see, there is a problem: in the obj function, I'm setting the sender's username as value, while the key is the "object" I assume will be input by the same sender. The thing is, I'd like to use just the /add command to both add the "object" as key and the username as value, and I'd like that to be possible for every member of the group (so it should be possible for every member to add "objects" for other members). But I don't know if it's possible to add two message handlers in the same function (the /add command, in this case) in a synchronous way, that is to ask the user to add the key, wait for the response, and then and only then ask to add the value, which means that the second send_message should only appear after the key has been input by the user.

2

2 Answers

1
votes

ConversationHandler seems like the most plausible solution for you. You need to map out your conversation flow. What states (steps) do you desire in the conversation?

Take a look at the repo's simple example for ConversationHandler: ConversationHandler State Diagram

  • Grey boxes are States
  • Red boxes are messages to the user
  • Green boxes are commands / keyboard choices

You can use the different filter types to make sure you get the object types you desire, see Filters Module

I believe their example code has what you're looking for.

0
votes

If you want to ask a question and wait for a response with python-telegram-bot, you can use the ConversationHandler