Mihail Kuznetsov Mihail Kuznetsov -4 years ago 538
Node.js Question

How to create pagination with inline keyboard in telegram

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.

Answer Source

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);
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download