generate inline keyboard telegram bot from the database

大憨熊 提交于 2019-12-25 17:37:53

问题


hi I have some product cat in my database I want to generate inline keyboard from the theme but I want to like this

we have

cat1-ca2-ca3-ca4-ca5

I want to like this

cat1  cat2
cat3  cat4
   cat5

I need the loop for this result thanks


回答1:


When you get the values that you want to show in the keyboard, you need to store these in array. This example was did in Javascript, you must found the equivalent in php code.

If you only have 5 products you don't need a loop, just:

...
{
  'reply_markup': JSON.stringify({
    keyboard: 
    [
      [{'text': 'cat1'},{'text': 'cat2'}],
      [{'text': 'cat3'},{'text': 'cat4'}],
      [{'text': 'cat5'}]
    ],
    one_time_keyboard: true,
    resize_keyboard: true
  })
}
...

If you have undefined products elements you must use a for loop:

var keyboard = [];
var products = ['cat1', 'cat2', 'cat3', 'cat4', 'cat5'];

for (var i = 0; i < products.length; i++) {
  keyboard.push([{'text': products[i]}]);
}

...
{ 
  'reply_markup': JSON.stringify({
    inline_keyboard: keyboard
  })
}


来源:https://stackoverflow.com/questions/44697686/generate-inline-keyboard-telegram-bot-from-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!