【问题标题】:How do i use Switch for inline keyboard and replykeyboard at the same time #PHP我如何同时将 Switch 用于内联键盘和回复键盘#PHP
【发布时间】:2018-04-06 00:23:06
【问题描述】:

这是一个 php 电报 webhook 脚本:

//calling telegram api after setting $access_token with the bot api
$api = 'https://api.telegram.org/bot' . $access_token;
$output = json_decode(file_get_contents('php://input'), TRUE);

                //setting variables from telegram api
                $chat_id = $output['message']['chat']['id'];
                $first_name = $output['message']['chat']['first_name'];
                $last_name  = $output["message"]["chat"]["last_name"];
                $message = $output['message']['text'];
                $callback_query = $output['callback_query'];
                $data = $callback_query['data'];
                $message_id = ['callback_query']['message']['message_id'];
                $chat_id_in = $callback_query['message']['chat']['id'];  
                 switch($message) {
                     case '/start':
                     $inline_button1 = array("text"=>"Website","url"=>"http://google.com");
                     $inline_button2 = array("text"=>"test","callback_data"=>'/test');
                     $inline_keyboard = [[$inline_button1,$inline_button2]];
                     $keyboard=array("inline_keyboard"=>$inline_keyboard);
                     $replyMarkup = json_encode($keyboard);
                      sendMessage($chat_id, "hello", $replyMarkup);
                     break;
                     case '/help':
                     sendMessage($chat_id,"you need help?",$replyMarkup);
                     break;
                     default:
                     sendMessage($chat_id,"thank you",$replyMarkup);
                 }
                switch($data){
                    case '/test':
                    sendMessage($chat_id_in, "Coming Soon .....");
                    break;
                }
    //the sendMessage function
                function sendMessage($chat_id, $message, $replyMarkup) {
                  file_get_contents($GLOBALS['api'] . '/sendMessage?chat_id=' . $chat_id . '&text=' . urlencode($message) . '&reply_markup=' . $replyMarkup);
                } 

上面的代码在电报上使用内联键盘对我来说很好,但我喜欢添加replykeyboard 以及一些命令,但我遇到了问题,请参见下文。我认为这是我需要正确处理的 curl 功能,你们的想法是什么。

你能帮我什么忙?

        $api = 'https://api.telegram.org/bot' . $access_token;
        $output = json_decode(file_get_contents('php://input'), TRUE);
        $chat_id = $output['message']['chat']['id'];
        $first_name = $output['message']['chat']['first_name'];
        $last_name  = $output["message"]["chat"]["last_name"];
        $message = $output['message']['text'];
        $callback_query = $output['callback_query'];
        $data = $callback_query['data'];
        $message_id = ['callback_query']['message']['message_id'];
        $chat_id_in = $callback_query['message']['chat']['id'];  
         switch($message) {
             case '/start':
             $inline_button1 = array("text"=>"Website","url"=>"http://google.com");
             $inline_button2 = array("text"=>"test","callback_data"=>'/test');
             $inline_keyboard = [[$inline_button1,$inline_button2]];
             $keyboard=array("inline_keyboard"=>$inline_keyboard);
             $replyMarkup = json_encode($keyboard);
              sendMessage($chat_id, "Hello", $replyMarkup);
             break;
             case '/help':
             $keyboard1 = array("text"=>"Button 1","text"=>"Button 1","resize_keyboard"=>'true',"one_time_keyboard"=>'true');
             $keyboard2 = array("text"=>"Button 2","text"=>"Button 2","resize_keyboard"=>'true',"one_time_keyboard"=>'true');
             $keyboardset = [[$keyboard1,$keyboard2]];
             $keyboard=array("KeyboardButton"=>$keyboardset)
             $replyMarkup = json_encode($keyboard);
             sendMessage($chat_id,"$you need help",$replyMarkup);
             break;
             default:
             sendMessage($chat_id,"$Thanks",$replyMarkup);
         }
        switch($data){
            case '/test':
            sendMessage($chat_id_in, "Coming Soon .....");
            break;
        }



//call the curl $ch = curl_init($api . 'sendMessage');
        curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch,
        CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, ($data)); curl_setopt($ch,
        CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch);
        curl_close($ch);

            function sendMessage($chat_id, $message, $replyMarkup) {
              file_get_contents($GLOBALS['api'] . '/sendMessage?chat_id=' . $chat_id . '&text=' . urlencode($message) . '&reply_markup=' .
        $replyMarkup);
            }

提前致谢

【问题讨论】:

    标签: php switch-statement telegram telegram-bot php-telegram-bot


    【解决方案1】:

    这是完整的代码

     $api = 'https://api.telegram.org/bot' . $access_token;
            $output = json_decode(file_get_contents('php://input'), TRUE);
            $chat_id = $output['message']['chat']['id'];
            $first_name = $output['message']['chat']['first_name'];
            $last_name  = $output["message"]["chat"]["last_name"];
            $message = $output['message']['text'];
            $callback_query = $output['callback_query'];
            $data = $callback_query['data'];
            $message_id = ['callback_query']['message']['message_id'];
            $chat_id_in = $callback_query['message']['chat']['id'];  
             switch($message) {
                 case '/start':
                 $inline_button1 = array("text"=>"Website","url"=>"http://google.com");
                 $inline_button2 = array("text"=>"test","callback_data"=>'/test');
                 $inline_keyboard = [[$inline_button1,$inline_button2]];
                 $keyboard=array("inline_keyboard"=>$inline_keyboard);
                 $replyMarkup = json_encode($keyboard);
                  sendMessage($chat_id, "Hello", $replyMarkup);
                 break;
                 case '/help':
                 $keyboard = array(array("Cancel","Go Ahead","test"));
       $resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
       $reply_ = json_encode($resp);
                 break;
                 default:
                 sendMessage($chat_id,"$Thanks",$replyMarkup);
             }
            switch($data){
                case '/test':
                sendMessage($chat_id_in, "Coming Soon .....");
                break;
            }
    

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-04
      • 2016-10-20
      • 2016-10-17
      • 2017-06-02
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 2017-06-28
      相关资源
      最近更新 更多