在php电报机器人中回复标记

时间:2016-02-27 15:39:34

标签: telegram telegram-bot

我的电报机器人有问题。我想为我的机器人制作Keybaord。当我从浏览器运行我的电报api url时,它可以工作:

     https://api.telegram.org/mybottoken/sendmessage?chat_id=93119306&text=something&reply_markup={"keyboard":[["Yes","No"],["Maybe"],["1","2","3"]], "one_time_keyboard":true};

当我想在我的php文件中运行此url($ sendto Variable)时,这不起作用。 我的PHP代码是:

<?php 
define('BOT_TOKEN', '183690241:AAHgluc7D9g0DF_InurfBj2YdBgPE7fmymo');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
    $array = array(); 
// read incoming info and grab the chatID
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
$chatText = $update["message"]["text"]; 
// compose reply
$reply =  sendMessage();

// send reply
$sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".$reply."&reply_markup={"keyboard":[["Yes","No"],["Maybe"],["1","2","3"]], "one_time_keyboard":true};

file_get_contents($sendto);
function sendMessage(){
     global $chatID;
     global $chatText;
     if ($chatText =="/start") {
         $message = "Salam - Roboate Megat Hastam";
     }
     elseif ($chatText =="Khoobi?") {
         $message = "Merc -  Shomaa khobi?";
     }
       elseif ($chatText =="Chand salete?") {
         $message = "Be Tu Che!";
     }
 else
     {
         $message = "No Command";
     }


return rawurlencode($message);
}
?>

请在我犯错的地方帮忙。 谢谢所有人。

1 个答案:

答案 0 :(得分:3)

试试这段代码:

 var_dump($keyboard = json_encode($keyboard = [
                       'keyboard' => [
                         ['Yes'],['No'],['Maybe'],
                         ['1'],['2'],['3'],
                       ] ,

                       'resize_keyboard' => true,
                       'one_time_keyboard' => true,
                  'selective' => true
                   ]),true);


    function sendKeyboard($chat_id, $keyboard) {

        $text = "Merc -  Shomaa khobi?";
        file_get_contents(API_URL ."sendMessage?chat_id=".$chat_id."&reply_markup=".$keyboard."&text=".urlencode($text));

    }

    if($message == "/start"){

        sendKeyboard($chat_id, $keyboard);
    }
相关问题