I am currently developing a bot in NodeJS and have reached the stage where I need to handle callback_query. However, the bot fails to respond when I click on the inline keyboard options.
Version Details:
"node-telegram-bot-api": "^0.66.0"
Example Code:
import TelegramBot from "node-telegram-bot-api";
import { config } from "dotenv";
config();
const BOT_API_KEY = process.env.TOKEN;
const bot = new TelegramBot(BOT_API_KEY, { polling: true });
const optionsForGame = {
reply_markup: {
inline_keyboard: [
[{text: 'A', callback_data:'A'}, {text: 'B', callback_data:'B'}, {text: 'C', callback_data:'C'}],
[{text: 'D', callback_data:'D'}, {text: 'E', callback_data:'E'}, {text: 'F', callback_data:'F'}],
[{text: 'G', callback_data:'G'}, {text: 'H', callback_data:'H'}, {text: 'I', callback_data:'I'}],
[{text: 'J', callback_data:'J'}]
]
}
};
function initializeBot() {
const gameSessions = {};
bot.setMyCommands([
{ command: '/begin', description: 'Start the bot' },
{ command: '/play', description: 'Play a game' }
]);
bot.on('message', async (msg) => {
const incomingText = msg.text;
const chatId = msg.chat.id;
if (incomingText === '/begin') {
return await bot.sendMessage(chatId, `You sent: ${incomingText}`);
}
if (incomingText === '/play') {
await bot.sendMessage(chatId, 'I will think of a letter from A to J');
const randomLetter = String.fromCharCode(65 + Math.floor(Math.random() * 10));
gameSessions[chatId] = randomLetter;
console.log(optionsForGame);
return await bot.sendMessage(chatId, "Your guess is needed:", optionsForGame);
}
return await bot.sendMessage(chatId, 'Sorry, I did not understand that.');
});
bot.on('callback_query', (callbackQuery) => {
console.log(callbackQuery);
});
}
initializeBot();