如何从内联键盘获取数据作为单一结果?

How to get data from inline keyboard as single result?

我有一个已设置并连接到我的后端的电报机器人。当用户选择“输入密码”选项时,我会在请求旁边发送一个内联键盘,以便用户可以输入他的数字密码。我这样做是为了密码不会显示在聊天中。我遇到的问题是我使用 callback_data 为按下的按钮分配一个值,但是当按下按钮时这些回调一个一个地通过。我想知道是否可以建立一个累积的按下字符字符串,一旦按下“提交”按钮就通过用户的响应发送。

我的内联请求目前如下所示:

{
  "chat_id": 99999999,
  "text": "Enter Password",
  "reply_markup": {
  
    "inline_keyboard": [
        
      [{"text": "1","callback_data":"1","pay":true},{"text": "2","callback_data":"2"},{"text": "3","callback_data":"3"}],
      [{"text": "4","callback_data":"4"},{"text": "5","callback_data":"5"},{"text": "6","callback_data":"6"}],
      [{"text": "7","callback_data":"7"},{"text": "8","callback_data":"8"},{"text": "9","callback_data":"9"}],
      [{"text": "0","callback_data":"0"}],
      [{"text": "Submit","callback_data":"Submit"}]
    ]
  }
}

此外,还有可能在按下提交后使内联按钮消失,我已经浏览了电报机器人文档,并且可以找到任何类似的选项。 https://core.telegram.org/bots/api#sendmessage

请告知这是否可行,或者我是否应该采取其他方法。

根据您的评论,使用 ;

的简化版键盘

:

  1. 应该有更多的错误检查(当然...)
  2. 按下按钮上的小 'clocks' 可以通过让 Telegram 知道您已经看到该消息来删除。 (请参阅there docs
<?php

    $myChatId = 1234567;
    $token = '859.....';

    // Send keypad
    $data = http_build_query([
        'text' => 'Please enter pin;',
        'chat_id' => $myChatId
    ]);
    $keyboard = json_encode([
        "inline_keyboard" => [
            [
                [ "text" => "1", "callback_data" => "1" ],
                [ "text" => "2", "callback_data" => "2" ],
                [ "text" => "3", "callback_data" => "3" ]
            ],
            [
                [ "text" => "4", "callback_data" => "4" ],
                [ "text" => "5", "callback_data" => "5" ],
                [ "text" => "6", "callback_data" => "6" ]
            ],
            [
                [ "text" => "7", "callback_data" => "7" ],
                [ "text" => "8", "callback_data" => "8" ],
                [ "text" => "9", "callback_data" => "9" ]
            ],
            [
                [ "text" => "<", "callback_data" => "<" ],
                [ "text" => "0", "callback_data" => "0" ],
                [ "text" => "OK", "callback_data" => "ok" ]
            ]
        ]
    ]);
    $res = @file_get_contents("https://api.telegram.org/bot$token/sendMessage?{$data}&reply_markup={$keyboard}");

    // Get message_id to alter later
    $message_id = json_decode($res)->result->message_id;

    // Remember pressId, so we wont add the same input
    $last_presesd_id = 0;

    // Continually check for a 'press', until we've reached 'ok' callback
    // TODO; max 30sec - 4inputs
    $pincode = [];
    while (true) {

        // Call /getUpdates
        $updates = @file_get_contents("https://api.telegram.org/bot$token/getUpdates");
        $updates = json_decode($updates);

        // Check if we've got a button press
        if (count($updates->result) > 0 && isset(end($updates->result)->callback_query->data)) {

            // And this is the first time
            if ($last_presesd_id === end($updates->result)->callback_query->id) {
                continue;
            }

            // Get callback data (pressed number)
            $callBackData = end($updates->result)->callback_query->data;

            // Remember $last_presesd_id
            $last_presesd_id = end($updates->result)->callback_query->id;

            // Handle data
            switch ($callBackData) {

                // Stop, remove keyboard, show result
                case 'ok':

                    // Show pincode and remove keyboard
                    $data = http_build_query([
                        'text' => 'Pincode: ' . implode('-', $pincode),
                        'chat_id' => $myChatId,
                        'message_id' => $message_id
                    ]);
                    $alter_res = @file_get_contents("https://api.telegram.org/bot$token/editMessageText?{$data}");

                    // End while
                    break 2;

                // <, remove last from pin input
                case '<': {
                    array_pop($pincode);
                    break;
                }

                // 1, 2, 3, ...
                default:
                    // Add to pincode
                    $pincode[] = $callBackData;
                    break;
            }
        }

        // Sleep for a second, and check again
        sleep(1);
    }

我正在使用 editMessageText() to alter the message, to remove the keyboard. You could also us ReplyKeyboardRemove()