I’m working on building a Telegram bot using the python-telegram-bot library and running into some issues. I want to create a bot that lets group members add items to a shared collection using a /register command. Each item should be stored with the owner’s name.
Right now I’m storing items in a dictionary where the item name is the key and the owner’s username is the value. Here’s my current code:
import logging
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler, Filters
item_collection = {}
bot_updater = Updater(token='YOUR_BOT_TOKEN')
bot_dispatcher = bot_updater.dispatcher
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
bot_updater.start_polling()
def welcome(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="Hello! I'm here to help manage your group items!")
def store_item(bot, update):
item_collection[update.message.text] = update.message.from_user.username
def handle_unknown(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="I don't recognize that command.")
def register(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="What item would you like to register?")
bot_dispatcher.add_handler(MessageHandler(Filters.text, store_item))
bot_dispatcher.add_handler(CommandHandler('start', welcome))
bot_dispatcher.add_handler(CommandHandler('register', register))
bot_dispatcher.add_handler(MessageHandler(Filters.command, handle_unknown))
My problem is that I want the /register command to collect both the item name and owner name in sequence. I need the bot to ask for the item first, wait for the user’s response, then ask for the owner name, and wait for that response too. Is there a way to handle multiple message inputs synchronously within a single command function?