【问题标题】:Telegram bot inline keyboard markup callback usage for channel messagesTelegram bot 内联键盘标记回调用于通道消息
【发布时间】:2018-03-31 10:28:49
【问题描述】:

我的 Telegram 机器人需要向频道发送消息并为每条消息提供内联键盘,如下所示:inline message keyboard

我需要对此键盘按钮单击事件做出反应,但我找不到说明如何操作的文档或示例。 Here in docs我只能看到这样的按钮可以打开网址或切换聊天,但这不是我需要的功能。

目前我的消息发送代码如下(我使用 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 }) }
    )
);

所以,我需要知道,如何让机器人对频道消息中的内联消息键盘交互做出反应。

【问题讨论】:

  • 自从我开发 Telegram 机器人以来已经很长时间了,但是当我在 Python 上使用它时,我必须使用这个命令“CallbackQueryHandler(”你的方法来回复内联消息)”。所以查看 Telegraf 的文档并搜索 CallbackQueryHandler 之类的内容

标签: javascript node.js keyboard telegram telegram-bot


【解决方案1】:

您可以使用事件 action() 或在 TelegrafContext 中使用 callbackQuery()answerCallbackQuery()
GitHubGist 上的上下文方法

成功了:

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()

完整示例here

【讨论】:

  • 有没有办法给回调函数传参数?
猜你喜欢
  • 2017-06-18
  • 1970-01-01
  • 2020-01-05
  • 2017-06-02
  • 2017-07-04
  • 2016-10-17
  • 2016-10-20
  • 1970-01-01
  • 2018-08-09
相关资源
最近更新 更多