0

I'm writing a python telegram bot and I want to work with inline keyboard bottom. I write this code below:

from telegram import *
from telegram.ext import *
mbti_message = 'This is a test massage that should sent in MBTI part'

def startx(bot, update):
    keyboard = [[InlineKeyboardButton("Option 1", callback_data='1'),
                 InlineKeyboardButton("Option 2", callback_data='2')],

                [InlineKeyboardButton("Option 3", callback_data='3')]]

    reply_markup = InlineKeyboardMarkup(keyboard)
    chat_id = update.message.chat_id
    bot.sendMessage(chat_id, "Message Sent From Function STARTX", reply_markup=reply_markup)

def buttomx(bot, update):

    query = update.callback_query

    bot.edit_message_text(text="Selected option: %s" % query.data,
                          chat_id=query.message.chat_id,
                          message_id=query.message.message_id)

def start (bot,update):

    keyboard_list = [[InlineKeyboardButton("ABOUT US", callback_data='1')],
                     [InlineKeyboardButton("MBTI Test Start", callback_data='2')]]
    reply_markup = InlineKeyboardMarkup(keyboard_list)
    chat_id = update.message.chat_id
    start_message_sent = "Welcome To My Bot Please Chose Your Options"
    bot.sendMessage(chat_id,start_message_sent, reply_markup=reply_markup)
    bot.sendMessage(chat_id,start_message_sent_global,reply_markup=reply_markup)

def bottom(bot, update):

    counter = 0
    query = update.callback_query
    chat_id = query.message.chat_id
    selected_option = int(query.data)

    if selected_option==1 :
        bot.sendMessage(chat_id,"You Chose About Us Section Thanks For Choosing Us")
    elif selected_option==2:
        bot.sendMessage(chat_id,"You Are Ready To Start Test ...")
        command = 'mbti'
        updater.dispatcher.add_handler(CommandHandler(command=command, callback=startx))
        updater.dispatcher.add_handler(CallbackQueryHandler(buttomx))
        updater.start_polling()

When user press bottom MBTI set command to mbti and pass it to command handler and when command handler get mbti command start to run starx function.

but when i use this code below in the if condition it send it before checking if condition

updater.dispatcher.add_handler (CommandHandler (command = command , startx)

What should i do in this condition?

Ricardo
  • 587
  • 5
  • 14
MRM
  • 43
  • 1
  • 9
  • You are adding a handler in response to some update. The `add_handler` call should be done only once outside the `bottom` command – balki Aug 08 '17 at 18:51
  • @balki I got it but what should i do if i want when user press some bottom go and run a function? – MRM Aug 09 '17 at 08:24

1 Answers1

1

Use a ConversationHandler for deep interactive menu. other way you can storing userState, and call function with State.

for more information see docs example it also supported user_data and RegExp for powerful handling message from users.

don't forget: data stores in memory and you may have lost or incorrect in some case. good think cleaned user_data in entry point,

each function can have many returns to other entry and with other side - each of entry has many different function in different matching case.

dzNET
  • 930
  • 1
  • 9
  • 14