Search code examples
node.jstelegramtelegrafnode-telegram-bot-api

Dynamic inlineKeyboard buttons (nodejs, telegraf)


I need to create a menu using inlineKeyboard from an array of "n" element that can change in value and number. I'm working with telegraf API and this is how i create a static one:

const bookMenu = Telegraf.Extra
  .markdown()
  .markup((m) => m.inlineKeyboard([[
     m.callbackButton('book1', 'book1-callback'), 
     m.callbackButton("book2", "book2-callback")], 
    [m.callbackButton("book3", "book3-callback")]
    //.....
    //for n buttons
    //.....
]))

How can i do that? I couldn't manage to do a for cycle inside markup


Solution

  • const bookMenu = Telegraf.Extra
        .markdown()
        .markup((m) => {                    
            let list = []      
            let j = 0;
    
            async.each(arrayOfLabel, ()=>{ 
                list.push(m.callbackButton(books[j], "your-unic-callback"))
                j++
            })
        
            return m.inlineKeyboard(list)
    })
    

    returning m.inlineKeyboard with the wanted list solved the problem.