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);
});
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 :)
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:
Comment if it worked to help other users, thanks. 鉁岋笍馃槈