锁定多个输入的电报自定义键盘按钮

时间:2016-04-26 17:53:55

标签: telegram telegram-bot

我正在尝试锁定自定义键盘中的键,以便用户在发送消息之前可以点击自定义键盘中的多个按钮。默认情况下,您点击自定义键盘按钮并发送消息,就像使用trivia bot一样。关于如何做到这一点或有甚么可能的任何想法?

2 个答案:

答案 0 :(得分:0)

除非您设置one_time_keyboard = True或将ReplyKeyboardHide返回给用户,否则保留自定义键盘是默认行为。

请参阅文档:https://core.telegram.org/bots/api#replykeyboardmarkup

每次要确保键盘显示时,您也可以在回复信息中发送相同的键盘。

答案 1 :(得分:0)

如果我理解正确,您要做的是多选(复选框);电报中没有这样的功能,但你可以用不同的方式实现它。

首先,您发送带有内嵌按钮和带有一些文本的空复选框的消息:

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;
}

在第二种情况下,您只是检查按下了哪个按钮,只需更改复选标记上的空框即可。

此外,您需要使用 answerCallbackQuery 才能正常工作:

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