How to create pagination with inline keyboard in telegram

醉酒当歌 提交于 2019-12-05 07:09:27

问题


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.

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.


回答1:


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);
});


来源:https://stackoverflow.com/questions/42874286/how-to-create-pagination-with-inline-keyboard-in-telegram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!