Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locking Telegram Custom Keyboard Buttons for Multiple Inputs

I am trying to lock the keys in the custom keyboard so the user can tap multiple buttons in the custom keyboard before sending the message. The default is you tap a custom keyboard button and message is sent, like with trivia bot. Any ideas on how to do this or if it is even possible?

like image 727
Nathan Bernard Avatar asked Oct 26 '25 12:10

Nathan Bernard


2 Answers

If I understood right, what you want to do is multiselect (checkbox); there is no such function in telegram, but you can implement it a bit differently.

First, you send message with inline buttons and empty checkboxes with some text:

switch ($callback_query){
     case 'choose':
          $inline_keyboard = [
            [
                ['text'=>'☐ 1', 'callback_data'=>"n1"],
                ['text'=>'☐ 2', 'callback_data'=>"n2"],
                ['text'=>'☐ 3', 'callback_data'=>"n3"]

            ],
            [
                ['text'=>'☐ 4', 'callback_data'=>"n4"],
                ['text'=>'☐ 5', 'callback_data'=>"n5"]
            ],
            [
                ['text'=>'Next', 'callback_data'=>"$someData"]
            ]
        ];
        $keyboard=["inline_keyboard"=>$inline_keyboard];
        $replyMarkup = json_encode($keyboard);
        sendMessage($chat_id_callback, "Lorem ipsum dolor sit amet.", $replyMarkup);
     break;

// When user is clicking on the buttons You process it with another case with all possible buttons in it and using telegram api to editMessageReplyMarkup

    case "n1":
    case "1":
    case "n2":
    case "2":
    case "n3":
    case "3":
    case "n4":
    case "4":
    case "n5":
    case "5":
        $empty_checkbox = "☐";
        $galochka = "✔";
        if (substr($data,0,1)== "n"){
            $is_checked = TRUE;
        } else {
            $is_checked = FALSE;
        }

        if ($is_checked){
            $what_is_checked = substr($data, 1);;
        } else {
            $what_is_checked = $data;
        }
// $output is variable, in wich telegram data is stored, which came through webhook
        $text1 = $output['callback_query']['message']['reply_markup']['inline_keyboard'][0][0]['text'];
        $text2 = $output['callback_query']['message']['reply_markup']['inline_keyboard'][0][1]['text'];
        $text3 = $output['callback_query']['message']['reply_markup']['inline_keyboard'][0][2]['text'];
        $text4 = $output['callback_query']['message']['reply_markup']['inline_keyboard'][1][0]['text'];
        $text5 = $output['callback_query']['message']['reply_markup']['inline_keyboard'][1][1]['text'];

        $room_callback_data1 = $output['callback_query']['message']['reply_markup']['inline_keyboard'][0][0]['callback_data'];
        $room_callback_data2 = $output['callback_query']['message']['reply_markup']['inline_keyboard'][0][1]['callback_data'];
        $room_callback_data3 = $output['callback_query']['message']['reply_markup']['inline_keyboard'][0][2]['callback_data'];
        $room_callback_data4 = $output['callback_query']['message']['reply_markup']['inline_keyboard'][1][0]['callback_data'];
        $room_callback_data5 = $output['callback_query']['message']['reply_markup']['inline_keyboard'][1][1]['callback_data'];

        if ($what_is_checked == "1"){
            if ($is_checked == TRUE) {
                $text1 = $galochka . " 1";
                $room_callback_data1 = "1";
            } else {
                $text1 = $empty_checkbox . " 1";
                $room_callback_data1 = 'n2';

            }
        } elseif ($what_is_checked == "2"){
            if ($is_checked == TRUE) {
                $text2 = $galochka . " 2";
                $room_callback_data2 = "2";
            } else {
                $text2 = $empty_checkbox . " 2";
                $room_callback_data2 = 'n2';

            }
        } elseif ($what_is_checked == "3"){
            if ($is_checked == TRUE) {
                $text3 = $galochka . " 3";
                $room_callback_data3 = "3";
            } else {
                $text3 = $empty_checkbox . " 3";
                $room_callback_data3 = 'n3';

            }
        } elseif ($what_is_checked == "4"){
            if ($is_checked == TRUE) {
                $text4 = $galochka . " 4";
                $room_callback_data4 = "4";
            } else {
                $text4 = $empty_checkbox . " 4";
                $room_callback_data4 = 'n4';

            }
        } elseif ($what_is_checked == "5"){
            if ($is_checked == TRUE){
                $text5 = $galochka . " 5";
                $room_callback_data5 = "5";
            } else {
                $text5 = $empty_checkbox . " 5";
                $room_callback_data5 = 'n5';

            }
        }

        $inline_keyboard = [
            [
                ['text'=>$text1, 'callback_data'=>$room_callback_data1],
                ['text'=>$text2, 'callback_data'=>$room_callback_data2],
                ['text'=>$text3, 'callback_data'=>$room_callback_data3]

            ],
            [
                ['text'=>$text4, 'callback_data'=>$room_callback_data4],
                ['text'=>$text5, 'callback_data'=>$room_callback_data5]
            ],
            [
                ['text'=>'Next', 'callback_data'=>"remont"]
            ]
        ];
        $keyboard=["inline_keyboard"=>$inline_keyboard];
        $replyMarkup = json_encode($keyboard);

        editMessageReplyMarkup($chat_id_callback, $message_id, $replyMarkup);
    break;
}

In the second case, you are just checking which button was pressed and simply change empty box on check mark.

Also, you need to use answerCallbackQuery, to work properly:

function send_answerCallbackQuery($callback_query_id, $text ='', $alert = 0){
    file_get_contents($GLOBALS['api']."/answerCallbackQuery?callback_query_id=".$callback_query_id . '&text=' . $text . '&show_alert=' . $alert);
}

if (isset($output['callback_query'])) {
    send_answerCallbackQuery($output['callback_query']['id']);
}
like image 132
Akim Avatar answered Oct 28 '25 04:10

Akim


It's a default behavior to preserve custom keyboard unless you set one_time_keyboard = True or return a ReplyKeyboardHide to a user.

See docs: https://core.telegram.org/bots/api#replykeyboardmarkup

Also you can send the same keyboard in a reply message every time you want to make sure the keyboard is displayed.

like image 27
Alexander Trakhimenok Avatar answered Oct 28 '25 02:10

Alexander Trakhimenok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!