【问题标题】:How to pass keyboard's dynamic button value in telegram bot?如何在电报机器人中传递键盘的动态按钮值?
【发布时间】:2018-12-03 19:47:24
【问题描述】:

我有一个问题,我正在我的项目中实现电报机器人,我想知道如何在电报机器人中传递动态键盘的按钮值而不是静态值。 我有一个按钮数组。

$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'])
                         );

如何使下划线动态化。

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

row() 方法中传递。

【问题讨论】:

    标签: php laravel telegram telegram-bot


    【解决方案1】:
    $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);
    

    【讨论】:

    • 感谢您的回答……它工作正常……我接受了这个答案……还有一件事是如何使用 array_map 函数中的值访问每个元素的索引……@mscho
    • 如果您需要索引,请使用常规的 foreach 或使用例如此库中的函数mapgithub.com/lstrojny/functional-php array_map 无法为您提供索引。
    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 2018-03-23
    • 2022-01-16
    • 2018-06-10
    • 2017-02-14
    • 2016-03-22
    相关资源
    最近更新 更多