Creating multiple inline buttons for a Telegram bot

I’m trying to make a Telegram bot that shows a list of drinks and their prices. I want to create multiple inline buttons in a single row, but I’m having trouble figuring out how to do it.

Here’s what I’m working with:

const drinks = {
  soda: '1.50',
  juice: '2.00',
  water: '1.00'
}

// How can I create buttons for each drink?
bot.command('menu', (ctx) => {
  // Need help here
})

I want to show ‘Drink Menu’ once at the top, then have buttons for each drink and its price. I can’t seem to use a loop inside the ctx.reply function. Any ideas on how to make this work? Thanks!

I’ve tackled a similar challenge with my Telegram bot project. Here’s how I approached it:

First, create an array of buttons using Object.entries(drinks).map(). This lets you iterate over the drinks object and create a button for each drink.

Then, use Markup.inlineKeyboard() to arrange these buttons. You can split them into rows if needed.

Here’s a code snippet that should work:

const { Markup } = require('telegraf')

bot.command('menu', (ctx) => {
  const keyboard = Markup.inlineKeyboard(
    Object.entries(drinks).map(([name, price]) => 
      Markup.button.callback(`${name} - $${price}`, `order_${name}`)
    ),
    {columns: 2}
  )

  ctx.reply('Drink Menu', keyboard)
})

This creates a 2-column layout. Adjust the ‘columns’ value to change the layout. Also, don’t forget to handle the callback queries for these buttons!

hey, i’ve done something similar before. u can use the Markup.inlineKeyboard() method to make those buttons. Here’s a quick example:

bot.command('menu', (ctx) => {
  const buttons = Object.entries(drinks).map(([d, p]) => 
    Markup.button.callback(`${d}: $${p}`, `order_${d}`)
  );
  ctx.reply('Drink Menu', Markup.inlineKeyboard(buttons, {columns: 2}));
});

This should give u a nice 2-column layout for ur drinks. hope it helps!

Having worked on Telegram bots before, I can offer some insights. The key is to use the Markup.inlineKeyboard() method from the Telegraf library. You’ll want to create an array of button objects first, then pass that to the inlineKeyboard method.

Here’s a concise approach:

const { Markup } = require('telegraf');

bot.command('menu', (ctx) => {
  const buttons = Object.entries(drinks).map(([name, price]) => 
    Markup.button.callback(`${name}: $${price}`, `buy_${name}`)
  );

  ctx.reply('Drink Menu', Markup.inlineKeyboard(buttons, {columns: 3}));
});

This creates a single row with all drink buttons. Adjust the ‘columns’ value to change the layout. Remember to handle the callback queries for these buttons in your bot logic.