Reputation: 168
I've been making a bot and I used filters=Filters.private
for start_command then i used it in a MessageHandler
and it is the code:
dp.add_handler(CommandHandler('start', start_command, filters=Filters.private))
dp.add_handler(MessageHandler(Filters.private, start_keyboard_answers))
it works perfectly but the problem is that i need to make another filter inside of second one and the code supposed to be like that:
dp.add_handler(CommandHandler('start', start_command, filters=Filters.private)) dp.add_handler(MessageHandler(Filters.private, start_keyboard_answers, filters=Filters.private)) dp.add_handler(MessageHandler(Filters.private, start_keyboard_answers))
but it's impossible and i have two filters in second line how can i handle this? i will put full project down here if u wanna know what I'm realy doing(im looking for other buttons after second keyboard just like what I did to first and second one):
from telegram.ext import Updater, Filters, CommandHandler, MessageHandler
from telegram import ReplyKeyboardMarkup
BOT_TOKEN = ''
def start_command(update, context):
chat_id = update.message.chat.id
context.bot.send_message(
chat_id=chat_id,
text='Hey, welcome to Chocolate Coffee!'
)
keyboard = [
['Make an Order']
]
context.bot.send_message(
chat_id=chat_id,
text='How can I help you?',
reply_markup=ReplyKeyboardMarkup(keyboard, one_time_keyboard=True, resize_keyboard=True)
)
def start_keyboard_answers(update, context):
message_text = str(update.message.text)
if message_text.lower() == 'make an order':
update.message.reply_text(
text='Okay :)',
quote=True
)
keyboard = [
['breakfast'],
['coffee'],
['milkshake'],
['colddrink'],
['food'],
['mocktail'],
['icecream'],
['organic'],
['smoothi'],
['glossy'],
['tea']
]
chat_id = update.message.chat.id
context.bot.sendMessage(chat_id,"ok, then?",reply_markup = ReplyKeyboardMarkup(keyboard, one_time_keyboard=True, resize_keyboard=True))
message_text = str(update.message.text)
import pdb; pdb.set_trace()
if message_text.lower() == 'breakfast':
update.message.reply_text(
text='Okay :)',
quote=True
)
def main():
updater = Updater(token=BOT_TOKEN, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler('start', start_command, filters=Filters.private))
dp.add_handler(MessageHandler(Filters.private, start_keyboard_answers))
updater.start_polling()
print('Started!')
updater.idle()
if __name__ == "__main__":
main()
Upvotes: 1
Views: 8337
Reputation: 496
The first argument to the MessageHandler
is filters
We can chain multiple filters with bitwise operators( &
- AND, |
- OR, ~
- NOT) and pass it as first argument. Like this,
MessageHandler(filters = Filters.private & Filters.chat([ADMINS ID here]), callbacks = who_do_you_wanna_call_here)
or simply
MessageHandler(Filters.private & Filters.command, start_cmd_ck)
Although from the code, I could see you would need a ConversationHandler
to arrange a step-by-step conversation with the users. Check out this example here
Upvotes: 7