ili*_*eja 2 javascript keyboard node.js telegram telegram-bot
我的 Telegram bot 需要向频道发送消息并为每条消息提供内联键盘,它看起来像这样:内联消息键盘
我需要对此键盘按钮点击事件做出反应,但我找不到说明如何操作的文档或示例。在文档中,我只能看到此类按钮可以打开 URL 或切换聊天,但这不是我需要的功能。
目前我的消息发送代码如下(我使用 NodeJS Telegraf 框架):
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 }) }
)
);
Run Code Online (Sandbox Code Playgroud)
所以,我需要知道,如何让机器人对频道消息中的内联消息键盘交互做出反应。
您可以
在GitHubGist上使用事件action()或 TelegrafContextcallbackQuery()和answerCallbackQuery()
上下文方法
这是工作:
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()
Run Code Online (Sandbox Code Playgroud)
完整的例子在这里