2

I have an issue, I'm implementing the telegram bot on my project, i want to know that how to pass dynamic keyboard's button value in telegram bot instead of static value. I have an buttons array.

$buttons    = array('button 1', 'button 2', 'button 3', .....);

$keyboard   = Keyboard::make()
                    ->inline()
                    ->row(
                             Keyboard::inlineButton(['text' => 'Button 1', 'callback_data' => 'callback_data1']),
                             Keyboard::inlineButton(['text' => 'Button 2', 'callback_data' => 'callback_data2'])
                         );

How to make dynamic below line.

Keyboard::inlineButton(['text' => 'Button 1', 'callback_data' => 'callback_data1']);

which are passing in row() method.

viju
  • 91
  • 2
  • 3
  • 14

1 Answers1

2
$buttons    = array('button 1', 'button 2', 'button 3', .....);

$buttons = array_map(function($name) {
    // this line needs to be modified, but the concept should be clear
    return Keyboard::inlineButton(['text' => $name, 'callback_data' => 'callback_data1']);
}, $buttons);

$inline   = Keyboard::make()->inline();

$keyboard = call_user_func_array([$inline, 'row'], $buttons);
mscho
  • 860
  • 7
  • 16
  • thanks for answering... its working fine.. i accepted this answer..one more thing how to access index of each elements with values in array_map function.. @mscho – viju Jun 25 '18 at 10:42
  • If you need the index, either use a conventional foreach or use the e.g. the function `map` from this lib: https://github.com/lstrojny/functional-php array_map cannot give you the index. – mscho Jun 25 '18 at 10:50