0

Using node-telegram-bot-api. I have a Webhook that works properly - i get updates when messages are sent, but no update is sent when the user presses a button in an inline keyboard.

class BotController {
 async handleUpdate(req, res, next){
 console.log(req.body);
 if (req.body.message.text === "/getTypes"){
 sendTypesKeyboard(req.body.message.chat.id);
 }
 return res.json({message: "update handled"});
 } 
}
const sendTypesKeyboard = async (chatId) => {
 const types = await typeDbHandler.getAll();
 const options = {
 reply_markup: JSON.stringify({
 inline_keyboard: types.map((type) => 
 [{"text": type.name, "callback_data": type.id}]
 )
 })
 }
 bot.sendMessage(chatId, "Choose the type", options);
}

I tried to use polling and handle a callback_query event, but it didn't work either

const TelegramApi = require('node-telegram-bot-api');
const BOT_TOKEN = process.env.BOT_TOKEN;
const bot = new TelegramApi(BOT_TOKEN, {polling: true});
// bot.setWebHook(`${process.env.URL}/bot`);
bot.on('callback_query', (query) => {
 console.log(query);
})
bot.on('message', msg => {
 console.log(msg);
})
module.exports = bot;

Again, updates with messages are recieved, but callback_query is never fired

1 Answer 1

1

I'm wondering if you're even getting the callback_query updates. Can you please update the LongPolling example with the following code:

const bot = new TelegramBot("TOKEN", {
 polling: {
 params: {
 allowed_updates: [
 "message", "callback_query", // list any other ...
 ]
 }
 }
});

Basically what changed is, we know explicitly request callback_query updates to be sent from Telegram Bot API server.

And you can also do the same while calling the /setWebhook call. Specify the allowed_updates as query parameter or in the JSON body.

Example:

https://api.telegram.org/bot<TOKEN>/setWebhook?url=<YOUR_URL>&allowed_updates=["message", "callback_query"]

Make sure to replace with your actual Bot Token, and <YOUR_URL> with your webhook URL. Also, feel free to add any further update types you want to listen to the allowed_updates parameter list.

Hope this helps.

answered Jul 14, 2024 at 5:15
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.