如何在电报中使用嵌入式键盘创建分页 [英] How to create pagination with inline keyboard in telegram
问题描述
我正在创建一个带有Node.js的Telegram机器人,并且正在使用node-telegram-bot-api模块.
I am creating a Telegram bot wit Node.js and I am using node-telegram-bot-api module.
我当前的问题是:
使用嵌入式键盘创建分页.
在文档此处中,有一个有趣的示例我需要.
My current issue is:
To create pagination with inline keyboard.
In documentation here, has an interesting example of what I need.
对于外观,我必须使用方法 editMessageText ,但是对于更新嵌入式键盘,我需要传输参数 inline_message_id .不幸的是,我不知道该怎么做.
For appearances, I must use method editMessageText but for update inline keyboard I need to transfer param inline_message_id. Unfortunately I could not understand how to do it.
对于任何用于更新嵌入式键盘以及如何在此示例.
I will be very much appreciate for any example to update inline keyboard and how it release in this example.
推荐答案
您需要使用editMessageText
传递更新的分页:
You need pass updated pagination with editMessageText
:
var bookPages = 100;
function getPagination( current, maxpage ) {
var keys = [];
if (current>1) keys.push({ text: `«1`, callback_data: '1' });
if (current>2) keys.push({ text: `‹${current-1}`, callback_data: (current-1).toString() });
keys.push({ text: `-${current}-`, callback_data: current.toString() });
if (current<maxpage-1) keys.push({ text: `${current+1}›`, callback_data: (current+1).toString() })
if (current<maxpage) keys.push({ text: `${maxpage}»`, callback_data: maxpage.toString() });
return {
reply_markup: JSON.stringify({
inline_keyboard: [ keys ]
})
};
}
bot.onText(/\/book/, function(msg) {
bot.sendMessage(msg.chat.id, 'Page: 25', getPagination(25,bookPages));
});
bot.on('callback_query', function (message) {
var msg = message.message;
var editOptions = Object.assign({}, getPagination(parseInt(message.data), bookPages), { chat_id: msg.chat.id, message_id: msg.message_id});
bot.editMessageText('Page: ' + message.data, editOptions);
});
这篇关于如何在电报中使用嵌入式键盘创建分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!