I am making a telegram-bot in NodeJS. This is a code snippet I am having an issue with:
let counter = 0
bot.onText(/flexbox (.+)/i, async (msg, match) => {
console.log(msg)
console.log(match)
const chatId = msg.chat.id;
bot.sendMessage(msg.from.id, 'Original Text', {
reply_markup: {
inline_keyboard: [
[
{
text: `sample text`,
callback_data: 'callbackData',
url: `https://example.com`,
}
]
]
}
});
bot.on('callback_query', function onCallbackQuery(callbackQuery) {
// increment counter when everytime the button is pressed
counter = counter + 1
console.log(counter)
});
So basically what I am trying to achieve is that whenever the user clicks on the button, I want to increment the counter so that I can have a track/count of the total button clicks.
The callback function is not getting triggered at all if I use the field url
in the inline_keyboard
. If I remove the url
field the callback is triggered.
Can someone help me achieve this functionality?
2
Answers
The API Documnetation explains that exactly one of the fields
data
orgame_short_name
will be present in acallback_query
update. This means that the bot gets notified if and only if the button had adata
field or acallback_game
defined for it. Since your button has neither of them, pressing the button will not notify the bot.Please remove the tag py-telegram-bot-api, py is for python.