I’m working on a Telegram bot using Python and running into an issue with filter combinations. I have a bot that processes commands and messages in private chats only using Filters.private. My current setup works fine but I need to add additional filtering logic to handle different message types.
Here’s my current working code:
from telegram.ext import Updater, Filters, CommandHandler, MessageHandler
from telegram import ReplyKeyboardMarkup
TOKEN = 'your_bot_token'
def welcome_handler(update, context):
user_id = update.message.chat.id
context.bot.send_message(
chat_id=user_id,
text='Welcome to Pizza Palace!'
)
menu_buttons = [['Place Order']]
context.bot.send_message(
chat_id=user_id,
text='What would you like to do?',
reply_markup=ReplyKeyboardMarkup(menu_buttons, one_time_keyboard=True, resize_keyboard=True)
)
def handle_menu_selection(update, context):
user_input = str(update.message.text)
if user_input.lower() == 'place order':
update.message.reply_text('Great choice!')
categories = [
['Pizza'], ['Pasta'], ['Salads'], ['Drinks']
]
user_id = update.message.chat.id
context.bot.sendMessage(user_id, "Choose a category:",
reply_markup=ReplyKeyboardMarkup(categories, one_time_keyboard=True, resize_keyboard=True))
def setup_bot():
bot_updater = Updater(token=TOKEN, use_context=True)
dispatcher = bot_updater.dispatcher
dispatcher.add_handler(CommandHandler('start', welcome_handler, filters=Filters.private))
dispatcher.add_handler(MessageHandler(Filters.private, handle_menu_selection))
bot_updater.start_polling()
bot_updater.idle()
if __name__ == "__main__":
setup_bot()
The problem is I want to add more specific filtering to my MessageHandler but I can’t figure out how to combine multiple filter conditions properly. How can I layer different filters to handle various message scenarios while keeping the private chat restriction?