簡體   English   中英

使用PHP的Telegram機器人-回聲不起作用

[英]Telegram bot using PHP - echo doesn't work

我正在嘗試使用Google Maps API計算兩個地點之間的時間和距離。 我的代碼很簡單並且可以正常工作(使用鍵盤,並且運行平穩),但是當我部署代碼並嘗試使用該服務時,機器人給了我錯誤的答案...

這是代碼:

$details = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Miami&destinations=Miami+Beach&mode=driving&sensor=false";
    $json = file_get_contents($details);
    $arr = json_decode($json, TRUE);
    $distance = $arr[rows][0][elements][0][distance][text];
    $time = $arr[rows][0][elements][0][duration][text];
    $response = "You should be there in $time. Total distance you'll cover: $distance";

$parameters = array('chat_id' => $chatId, "text" => $response);
$parameters["method"] = "sendMessage";
echo json_encode($parameters);


這是機器人回答我的內容:

"You should be there in {. Total distance you'll cover: {";


這是實際的json:

 {
   "destination_addresses" : [ "Miami Beach, Florida, Stati Uniti" ],
   "origin_addresses" : [ "Miami, Florida, Stati Uniti" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "15,7 km",
                  "value" : 15745
               },
               "duration" : {
                  "text" : "22 min",
                  "value" : 1322
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}


這是怎么回事?!
更新:file_get_contents返回一個字符串。 因此,我將其解碼為一個PHP數組,但現在該漫游器甚至沒有回答!

$details = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Miami&destinations=Miami+Beach&mode=driving&sensor=false";
$json = file_get_contents($details);
$JsonObject = json_decode($json);
$distance = $JsonObject->rows[0]->elements[0]->distance->text;
$time = $JsonObject->rows[0]->elements[0]->duration->text;
$response = "You should be there in $time. Total distance you'll cover: $distance";
$chatId = "your telegram id here";
$botToken = "your bot token here";
file_get_contents("https://api.telegram.org/bot$botToken/sendMessage?chat_id=$chatId&text=$response");

這與file_get_contents一起使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM