Bereznyak
Bereznyak

Reputation: 75

How to catch keyboard response in Telegram

bot.onText(/Pizza/, (msg) => {
    bot.sendMessage(msg.chat.id, "OK, " + msg.from.first_name + ". What kind of pizza?", {
        "reply_markup": {
            "keyboard": [["Peperoni", "4 cheese", "Vegetarian", "Tomato"]],
            "resize_keyboard": true
        }
    });
});

bot.onText(/Peperoni/, (msg) => {
    console.log(msg.text);
    if (msg.text === "Peperoni") {
        console.log("Entered IF");
        bot.sendMessage(msg.chat.id, "Peperoni is there " + msg.from.first_name + ".Choose the quantity", {
            "reply_markup": {
                "keyboard": [["1", "2", "3"]],
                "resize_keyboard": true
            }
        })
        console.log(msg.text);
    }
});

Using node telegram bot api, my code is above. I can catch Peperoni, when user presses Peperoni, but how do I catch the quantity which I ask later. Trying to insert the last console.log at all the places, but this never happens. Any ideas please ;)

Upvotes: 0

Views: 378

Answers (2)

Saeed Heidarizarei
Saeed Heidarizarei

Reputation: 8916

All incomming messages to bot are an object 😉:
(text, voice, video and ...)

For example User Selected: 1

bot.on('message', (msg) => {
  console.log(msg.text);
});

Console Output: // 1

Upvotes: 0

Ashik Basheer
Ashik Basheer

Reputation: 1601

For this you need to save the last user request into a database table or a file for the chat_id and then get the quantity. You can retrieve the pizza type anytime from the table or the file for the chat_id

enter image description here

In the picture above you can see I save chat_id of every user making a request. I also save the request_type. In you case you can have request_value and save the value as pepperoni. Then in the next request get the pizza count 1 and fetch the previous value from request_value field for the same chat_id

Upvotes: 1

Related Questions