Dynamically creating inline keyboard buttons Markup Telegraf?

See original GitHub issue

Hello, I am trying to send buttons to my telegram bot using the telegram bot api. For now, for creating the buttons, I have to hard code the values in the fields but lets say I want to create the inline keyboard buttons dynamically through an array and passing the array index and the array values as parameters. How should i go about it ? This is what I tried so far.

import Telegraf from 'telegraf';
const { Markup } = Telegraf;

const buttons = ['low', 'middle', 'high'];

const inlineKeyboard = Markup
  .inlineKeyboard(
    buttons.map((button) => {
      Markup.callbackButton(button, button);
    })
  )
    .resize()
    .extra();

Thanks

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
shnigicommented, Oct 1, 2021

Here you go. The buttons have to be passed as an array. You can have 8 buttons in one row so if you want less you have to split your array / create chunks. Here is example with lodash chunks.

const sortedDota2Heroes = _.sortBy(dota2heroes, ['localized_name']);
const dota2heroesChunk = _.chunk(sortedDota2Heroes, 4);

const inlineMessageHeroes = Markup.inlineKeyboard(
    dota2heroesChunk.map((chunk) => chunk.map((hero) => Markup.button.callback(hero.localized_name, hero.localized_name))),
);

const dota2heroperformanceSelectHero = async (ctx, playerId) => {
    selectedPlayer = playerId;
    ctx.editMessageText(
        'Choose hero',
        inlineMessageHeroes,
    );
};
0reactions
humblemodulocommented, Sep 15, 2022

@shnigi How, exactly, do you handle .action() for dynamically created buttons?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic inlineKeyboard buttons (nodejs, telegraf)
I'm working with telegraf API and this is how i create a static one: const bookMenu = Telegraf.Extra .markdown() .markup((m) ...
Read more >
Inline Keyboards And Menus | Telegram Bot Development
Telegram bot development using node.js and Telegraf API. What you will learn: Inline Keyboards and creating menus.
Read more >
telegraf-inline-menu
Inline Menus for Telegraf made simple.. Latest version: 6.3.0, last published: a year ago. Start using telegraf-inline-menu in your project ...
Read more >
Inline Keyboard Markup - YouTube
Build Telegram Chat Bots with Node.js using the Modern Telegraf Framework ...
Read more >
Building your First Telegram Bot using Node.js and Telegraf
To add more functionality let's write code that shows you images of animals when you click inline keyboard buttons. Create a res folder...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found