PHP Telegram Inline-Button with different Text and Command

toiithl6  于 2023-04-19  发布在  PHP
关注(0)|答案(1)|浏览(102)

我创建了一些带有for循环的Telegram内联按钮。

for ($i = 1; $i < 35; $i++) {
    $arr[] = array(array(
        "text"          => $i,
        "callback_data" => "Test_".$i,
    ));
}
$bot->sendKeyboard($chat_id, $reply, $arr);

public function sendKeyboard($chat_id, $text, $keyboard = Array())
{
    $action = 'sendMessage';
    $param = array(
        'chat_id'       => $chat_id,
        'reply_markup'  => json_encode(array("keyboard" => $keyboard, "one_time_keyboard" => true, "resize_keyboard" => true)),
        'text'          => $text
    );
    
    $res = $this->send($action, $param);
    if (!$res['ok']) {
        $result = Array("success" => 0, "info"  =>  "Error: " . $res['description']);
            } else {
        $result = Array("success" => 1, "info"  =>  "Keyboard show");
            }
    return $result;
}

例如,按钮文本是6,我希望Telegram在聊天中写Test_6
我需要做什么更改才能输出与按钮上的文本不同的文本?

monwx1rj

monwx1rj1#

按钮上的text是用户在Telegram中看到的。
callback_data是一个内部值,当有人按下按钮时会返回该值,因此可以使用它来处理click事件。
你需要像这样创建按钮:

for ($i = 1; $i < 35; $i++) {
    $arr[] = array(array(
        "text"          => "Test_".$i,
        "callback_data" => $i
    ));
}

因此用户可以看到Test_1直到Test_35。如果有人按下按钮,您将获得数据135的数字。

相关问题