ilinieja
ilinieja

Reputation: 115

Telegram bot inline keyboard markup callback usage for channel messages

My Telegram bot needs to send messages to channel and provide inline keyboard for every message, it looks like this: inline message keyboard

I need to react on this keyboard button click event, but I can't find docs or examples showing how to do it. Here in docs I can only see that such buttons can open URL or switch chat, but it's not the functionality I need.

Currently my code for message sending looks like this (I use NodeJS Telegraf framework):

const Telegraf = require('telegraf');
const { Markup, Telegram } = Telegraf;

const telegram = new Telegram(process.env.BOT_TOKEN);

const inlineMessageRatingKeyboard = [[
    { text: '👍', callback_data: 'like' },
    { text: '👎', callback_data: 'dislike' }
]];

telegram.sendMessage(
    process.env.TELEGRAM_CHANNEL,
    'test',
    { reply_markup: JSON.stringify({ inline_keyboard: inlineMessageRatingKeyboard }) }
    )
);

So, I need to know, how to make bot react on inline message keyboard interaction in channel messages.

Upvotes: 6

Views: 34994

Answers (1)

dzNET
dzNET

Reputation: 970

you can use event action() or in TelegrafContext with callbackQuery() and answerCallbackQuery()
context methods on GitHubGist

it's work:

const Telegraf = require('telegraf')
const { Router, Markup } = Telegraf

const telegram = new Telegraf(process.env.BOT_TOKEN)

const inlineMessageRatingKeyboard = Markup.inlineKeyboard([
    Markup.callbackButton('👍', 'like'),
    Markup.callbackButton('👎', 'dislike')
]).extra()

telegram.on('message', (ctx) => ctx.telegram.sendMessage(
    ctx.from.id,
    'Like?',
    inlineMessageRatingKeyboard)
)

telegram.action('like', (ctx) => ctx.editMessageText('🎉 Awesome! 🎉'))
telegram.action('dislike', (ctx) => ctx.editMessageText('okey'))

telegram.startPolling()

full example here

Upvotes: 14

Related Questions