Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram bot reply for loop

I just want to read my data in an Object and print multiply buttons in a row.
How could I do that?

A similar example :

var obj={"coca":"2$" , "7up":"3$" , "fanta":"4$"} 
for (var key in obj) {
  if (obj.hasOwnProperty(key)) {
      ctx.reply('price list', Extra.HTML().markup((m) =>
        m.inlineKeyboard([
          [m.callbackButton(`${key} : `, ` price : ${obj[key]}`)]
        ]))))

But the problem is I do not want to print "price list" every time.
I just want to print it 1 times on top, and I can not use for loop inside "ctx.reply"

like image 554
Amin Joharinia Avatar asked Feb 28 '26 17:02

Amin Joharinia


1 Answers

If you just want the first item to be replied, all you have to do is to break the loop after first reply.

Your code may be like this:

var obj={"coca":"2$" , "7up":"3$" , "fanta":"4$"} 
for (var key in obj) {
  if (obj.hasOwnProperty(key)) {
          ctx.reply('price list', Extra.HTML().markup((m) =>
          m.inlineKeyboard([
          [m.callbackButton(`${key} : `, ` price : ${obj[key]}`)]
        ]))));
  break;
  }
}
like image 195
Shady Avatar answered Mar 02 '26 11:03

Shady



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!