How to get info from reply on specific message - Telegram Bot

Viewed 256

Let's say I have this command handler:

@bot.message_handler(commands=['setalarm'])
def setalarmcmd(message):
    alarmMessage = "Let's start with setting up alarm.\n\n" \
                   "First of all, provide the pair you want to observe."
    bot.send_message(message.chat.id, alarmMessage)

I want to set some action when user click /setalarm command and replies on this message.

Actually, I know how to do that, but I already have handler on provided text:

@bot.message_handler(content_types=['text'])
def getpairfuncmessage(message):
    userMessage = message.text.strip().upper()
    pair = getPairApi(userMessage)
    if not pair:
        return bot.send_message(message.chat.id, "Nah, not that, try something else.")

    pairResult = printPairResult(pair)

    bot.send_message(message.chat.id, pairResult, parse_mode='html')

And that's the problem. By providing message, user triggers this action, but I want to set another one, after /setalarm command. Is this possible?

1 Answers

Here is how you can do this:

@bot.message_handler(commands=['setalarm'])
def setalarmcmd(message):
    alarmMessage = "Let's start with setting up alarm.\n\n" \
                   "First of all, provide the pair you want to observe."
    msg = bot.send_message(message.chat.id, alarmMessage)
    bot.register_next_step_handler(msg, setalarmcryptopair)


def setalarmcryptopair(pair):
    print(pair.text)

Using send_message and register_next_step_handler to go to next function.

Related