Creating a Telegram bot using python-telegram-bot for group item management

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?

You’re facing a common challenge with conversational bots. The issue with your approach is that you’re adding handlers dynamically, which can lead to conflicts and disrupt the conversation flow. I’ve encountered a similar issue while developing an inventory bot. Implementing a state management system is key here. When a user invokes the /register command, store their user ID along with a status like ‘waiting_for_item’. By using a global state dictionary, you can manage the registration process for multiple users simultaneously without overlap.

Your problem is adding message handlers inside command functions - that creates conflicts and breaks conversation state tracking. I hit this same issue building a registration bot for my gaming community. Don’t add handlers dynamically. Use ConversationHandler from python-telegram-bot instead. Set up conversation states and transitions between them. Make separate functions for each registration step and use constants for states like WAITING_FOR_ITEM and WAITING_FOR_OWNER. ConversationHandler automatically manages the flow and keeps responses in the right context for each user, even when multiple people register at once.

Yeah, adding handlers inside functions is messy and breaks things. I ran into this with my Discord bot too. Use context.user_data to track each user’s registration progress instead of global dictionaries - way cleaner and handles multiple users registering at once without conflicts.