Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data from inline keyboard as single result?

I have a telegram bot that is set up and connected to my backend. When users choose the "enter password" option I send an inline keyboard next to the request so that the user can enter his numeric password. I do this so that the password will not be shown in the chat. The problem I have is that I make use of the callback_data to assign a value to the pressed button, but these callbacks come through one by one as the button is pressed. I would like to know is it possible to build up an accumulative string of pressed characters, and once the "Submit" button is pressed send through the user's response.

My Inline Request Currently looks as follow:

{
  "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"}]
    ]
  }
}

Further more is it possible to make the inline buttons go away after the submit has been pressed, I have gone through the telegram bot documentation, and can find any option like this. https://core.telegram.org/bots/api#sendmessage

Kindly advise if this is possible or if I should take another approach.

like image 264
Terblanche Daniel Avatar asked Sep 08 '25 00:09

Terblanche Daniel


1 Answers

Based on your comment, a simplified version of a KeyPad using php;

Note:

  1. There should be more error checking (of course...)
  2. The little 'clocks' on the pressed button can be removed by letting Telegram know you've seen the message. (Please see 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);
    }

enter image description here

I'm using editMessageText() to alter the message, to remove the keyboard. You could also us ReplyKeyboardRemove()

like image 74
0stone0 Avatar answered Sep 11 '25 00:09

0stone0