Node-telegram-bot-api: Callback query not triggering

Created on 26 Jul 2018  路  2Comments  路  Source: yagop/node-telegram-bot-api

Hi! I have just started learning about inline keyboards. I've created an inline keyboard and I am now stuck trying to get it to call a callback query. They keyboard is displayed and is clickable, but nothing happens when buttons are clicked (except that "loading" icon appearing).

I've seen a similar question asked before but none of the answers work for me.

Is it possible that .on('message' already catches the event, so that is why .on('callback_query' never gets called?

async function sendButtons(msg) {
    // array of rows, where rows are arrays of buttons
    const opts = {
        "reply_markup": {
                    "inline_keyboard": [
                        [{
                            "text": "A1"
                            ,"callback_data": "A1"            
                        }, 
                        {
                            "text": "B1"
                            ,"callback_data": "B1"            
                        }]
                    ]
                }
        }
        let txt = 'Choose one of the options please:';
        bot.sendMessage(msg.chat.id, txt, opts);
}

bot.on('message', msg => {
// some stuff
});

bot.on('callback_query', function (msg) {
    console.log(msg);
});

Most helpful comment

bot.on ('message') only listens to the messages that the bot receives. In order for the buttons to have activity, you must do something similar to the following:

bot.onText(/\/example/, function(msg){
// code
})

bot.on('callback_query', function onCallbackQuery(example){
const action = example.data // This is responsible for checking the content of callback_data
const msg = example.message

if (action == 'A1'){
// code
}
else if (action == 'B1'){
// code
}
});

Comment if it worked to help other users, thanks. 鉁岋笍馃槈

All 2 comments

bot.on ('message') only listens to the messages that the bot receives. In order for the buttons to have activity, you must do something similar to the following:

bot.onText(/\/example/, function(msg){
// code
})

bot.on('callback_query', function onCallbackQuery(example){
const action = example.data // This is responsible for checking the content of callback_data
const msg = example.message

if (action == 'A1'){
// code
}
else if (action == 'B1'){
// code
}
});

Comment if it worked to help other users, thanks. 鉁岋笍馃槈

Excellent advice, thanks :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abbddo picture abbddo  路  3Comments

mbrammer picture mbrammer  路  3Comments

saeedhei picture saeedhei  路  4Comments

qunatumwhiskey picture qunatumwhiskey  路  4Comments

alikhil picture alikhil  路  3Comments