Node-telegram-bot-api: Inline Keyboard, Callback_query not being called

Created on 22 Mar 2017  Â·  24Comments  Â·  Source: yagop/node-telegram-bot-api

It is amazing tool for NodeJS.

But I am stuck with in-line keyboard.

bot.onText(/(.+)$/, function (msg, match) {
    const opts = {
    reply_markup: {
      inline_keyboard: [
        [
          {
            text: 'Edit Text',
            callback_data: 'edit'
          }
        ]
      ]
    }
  };
  bot.sendMessage(msg.from.id, 'Original Text', opts);
});

bot.on("callback_query", function(callbackQuery) {
    // 'callbackQuery' is of type CallbackQuery
    console.log(callbackQuery);
});

Referring to the above code, callback_query is not executed upon clicking Edit Text inline-button

How can i solve this?

sorry! my bad!

Most helpful comment

I found the problem, @GochoMugo... in my package.json I had "node-telegram-bot-api": "^0.21.1", changed the version and it's worked! Thanks!

All 24 comments

Hey @shah-smit ,

Maybe do it like this?
bot.on("callback_query", function nameOfTheFunction(callbackQuery) {
// do your stuff here
})

@reactSearch Thanks for the revert.

Nope, It isnt consol-ing anything. When I do it.

var Bot = require('node-telegram-bot-api');
var TelegramToken = '<TOKEN>';
const bot = new Bot(TelegramToken, {polling: true});
console.log('Bot started...');

bot.onText(/(.+)$/, function (msg, match) {
    const opts = {
    reply_markup: {
      inline_keyboard: [
        [
          {
            text: 'Edit Text',
            callback_data: 'edit'
          }
        ]
      ]
    }
  };
  bot.sendMessage(msg.from.id, 'Original Text', opts);
});

bot.on("callback_query", function onCallbackQuery(callbackQuery) {
    // 'callbackQuery' is of type CallbackQuery
    console.log("query: ",callbackQuery);
});

Any news about it?

Works for me.

screenshot

You can share your code @GochoMugo? I've tried do this, but without success... :/

const TelegramBot = require("node-telegram-bot-api");
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN, { polling: true });

bot.onText(/\/callbackquery/, (msg, match) => {
    bot.sendMessage(msg.chat.id, "Click a button won't you!", {
        "reply_markup": {
            "inline_keyboard": [
                [
                    {
                        text: "Click me!",
                        callback_data: "click",
                    },
                ],
            ],
        },
    });
});


bot.on("callback_query", (callbackQuery) => {
    const msg = callbackQuery.message;
    bot.answerCallbackQuery(callbackQuery.id)
        .then(() => bot.sendMessage(msg.chat.id, "You clicked!"));
});

captura de tela 2017-06-22 as 17 15 37

@GochoMugo I copied your code and put in my bot, when I clicked in the button, appears a 'waiting' icon and nothing happens... do you know why?

I found the problem, @GochoMugo... in my package.json I had "node-telegram-bot-api": "^0.21.1", changed the version and it's worked! Thanks!

But my problem now is get the callback going from 'keyboard' object and not only from 'inline_keyboard', it's other procedure? 🤔

Could you write a small demo of what you are trying to achieve? Code is usually more precise. :grinning:

telegramBot.onText(/\/match/, msg => {
    const opts = {
        reply_to_message_id: msg.message_id,
        reply_markup: JSON.stringify({
            keyboard: [
                ['Opt1'],
                ['Opt2']            
            ],
            one_time_keyboard: true,
            selective: true
        })
    }
    telegramBot.sendMessage(msg.chat.id, 'Select One', opts)
})

I need a callback when user choose the option on keyboard not on inline_keyboard...

@luiguild When user presses a button on a regular keyboard they send a message with a text which is displayed on a keyboard. You just need to make

telegramBot.onText(/Opt1/, msg => {
    // Do whatever you need
}

@mnb3000 but in this case anyone that send one message with the same content, the bot will execute the function 😨

It's not possible using something like callback_data: "Opt1", that is used in inline keyboard?

@luiguild nope, that's how regular keyboards work

So... Ok... I will try another approach... Thank you!! 😃

BTW, I think that this issue can be closed. Thank's! 🎉

@luiguild what version did you change it to? I'm facing a similar problem.

@ravi72munde I believe the latest version on npm already supports callback queries, etc.


OP has not reacted to any comments. I'll go ahead and assume the issue is resolved by @luiguild's solution above. Thanks everyone. :+1:

ANS

const TelegramBot = require("node-telegram-bot-api");
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN, { polling: true });

bot.onText(/\/callbackquery/, (msg, match) => {
    bot.sendMessage(msg.chat.id, "Click a button won't you!", {
        "reply_markup": {
            "inline_keyboard": [
                [
                    {
                        text: "Click me!",
                        callback_data: "click",
                    },
                ],
            ],
        },
    });
});


bot.on("callback_query", (callbackQuery) => {
    const msg = callbackQuery.message;
    bot.answerCallbackQuery(callbackQuery.id)
        .then(() => bot.sendMessage(msg.chat.id, "You clicked!"));
});

IF not Click any buttons ,how many hours it will wait for response. suppose after 3 hours when i click (Click me!)buttons ,what will happen, it will work or not?
is this handle any time response?
is there possible to remove (Click me!)buttons after set of time- for example if not receive any response button will remove or hide after 10 mnts
@GochoMugo help to solve this one

@GochoMugo
how to change text inline_keyboard whne onClick in item ?

@GochoMugo I need help, please help. How do I do that when the inline buttons are pressed 1, the bot has to drop another file, when the button is pressed 2, the bot has to drop another file
Uploading image.png…

@GochoMugo I need help, please help. How do I do that when the inline buttons are pressed 1, the bot has to drop another file, when the button is pressed 2, the bot has to drop another file

hi dear,
please check
name ---> callback_data
for example :
after click button and change callback_data
const item = callbackQuery.data;
use
switch- case
please check this image image

thank you dear but I could not find a definite solution 😊

@GochoMugo I need help, please help. How do I do that when the inline buttons are pressed 1, the bot has to drop another file, when the button is pressed 2, the bot has to drop another file

hi dear,
please check
name ---> callback_data
for example :
after click button and change callback_data
const item = callbackQuery.data;
use
switch- case
please check this image image

thank you dear but I could not find a definite solution 😊

@GochoMugo broooo plzzzz help me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Hostile picture Hostile  Â·  3Comments

kenanchristian picture kenanchristian  Â·  4Comments

Lemmmy picture Lemmmy  Â·  3Comments

abbddo picture abbddo  Â·  3Comments

hems picture hems  Â·  3Comments