2017-08-15 22 views
0

電報機器人的數據,我有以下代碼,以使telegram bot得到用PHP

<?php 
define('API_KEY','*****'); 
function makereq($method,$datas=[]){ 
    $url = "https://api.telegram.org/bot".API_KEY."/".$method; 
    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
    curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($datas)); 
    $res = curl_exec($ch); 
    if(curl_error($ch)){ 
     var_dump(curl_error($ch)); 
    }else{ 
     return json_decode($res); 
    } 
} 
$result=json_decode($message,true); 
//##############=--API_REQ 
function apiRequest($method, $parameters) { 
    if (!is_string($method)) { 
    error_log("Method name must be a string\n"); 
    return false; 
    } 
    if (!$parameters) { 
    $parameters = array(); 
    } else if (!is_array($parameters)) { 
    error_log("Parameters must be an array\n"); 
    return false; 
    } 
    foreach ($parameters as $key => &$val) { 
    // encoding to JSON array parameters, for example reply_markup 
    if (!is_numeric($val) && !is_string($val)) { 
     $val = json_encode($val); 
    } 
    } 
    $url = "https://api.telegram.org/bot".API_KEY."/".$method.'?'.http_build_query($parameters); 
    $handle = curl_init($url); 
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); 
    curl_setopt($handle, CURLOPT_TIMEOUT, 60); 
    return exec_curl_request($handle); 
} 
//----######------ 
//--------- 
$update = json_decode(file_get_contents('php://input')); 
var_dump($update); 
$messages = $update->message; 
//========= 
$chat_id = $messages->chat->id; 
$message_id = $update->callback_query->message->message_id; 
$from_id = $messages->from->id; 
$name = $messages->from->first_name; 
$username = $messages->from->username; 
$t = isset($messages->text)?$messages->text:''; 
$reply = $messages->reply_to_message->forward_from->id; 
$sticker = $messages->sticker; 
$text = $messages->text; 
$data = $update->callback_query->data; 
$forward = $messages->forward_from; 
$location = $messages->location->longitude; 
$contact = $messages->contact->phone_number; 
$admin = 88120404; 

//------- 
function SendMessage($chat_id, $TextMsg) 
{ 
makereq('sendMessage',[ 
'chat_id'=>$chat_id, 
'text'=>$TextMsg, 
'parse_mode'=>"MarkDown" 
]); 
} 
function SendContact($chat_id, $contactMsg) 
{ 
makereq('sendContact',[ 
'chat_id'=>$chat_id, 
'phone_number'=>$contact, 
'first_name'=>$name 
]); 
} 
function SendSticker($chat_id, $sticker_ID) 
{ 
makereq('sendSticker',[ 
'chat_id'=>$chat_id, 
'sticker'=>$sticker_ID 
]); 
} 
function bots($method,$datas=[]){ 
    $url = "https://api.telegram.org/bot".API_KEY."/".$method; 
    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
    curl_setopt($ch,CURLOPT_POSTFIELDS,$datas); 
    $res = curl_exec($ch); 
    if(curl_error($ch)){ 
     var_dump(curl_error($ch)); 
    }else{ 
     return json_decode($res); 
    } 
} 
function typing($chat_id) 
{ 
file_get_contents(API_TELEGRAM.'sendChatAction?chat_id='.$chat_id.'&action=typing'); 
} 
function Forward($KojaShe,$AzKoja,$KodomMSG) 
{ 
makereq('ForwardMessage',[ 
'chat_id'=>$KojaShe, 
'from_chat_id'=>$AzKoja, 
'message_id'=>$KodomMSG 
]); 
} 
function save($filename,$TXTdata) 
    { 
    $myfile = fopen($filename, "w") or die("Unable to open file!"); 
    fwrite($myfile, "$TXTdata"); 
    fclose($myfile); 
    } 
//========== 

if($t == "test1"){ 
var_dump(makereq('sendMessage',[ 
     'chat_id'=>$chat_id, 
     'text'=>"Okay Share Your Number, Location And Your Zip Code", 
     'parse_mode'=>'MarkDown', 
     'reply_markup'=>json_encode([ 
      'keyboard'=>[ 
       [ 
       ['text'=>"Zip Code"],['text'=>"Address",'request_location'=>true], ['text'=>"Phone Number",'request_contact'=>true] 
       ], 
       [ 
       ['text'=>"Done!"] 
       ], 
      ], 
      'resize_keyboard'=>true 
     ]) 
    ])); 
} 
if($t == "Done!"){ 
var_dump(makereq('sendMessage',[ 
'chat_id'=>$Admin, 
'text'=>"Hey some one just submit in our bot: 
name : $name 
id : @$username 
phone number : $contact 
address : $location 
zip code : 
", 
])); 
} 
?> 

它工作正常,並獲得用戶的數據,並顯示姓名,ID,但它不會顯示用戶的電話號碼,地址...

我想我的代碼是好的(base on this documantions),我只是不知道爲什麼它不工作,並沒有顯示電話號碼和地址。

如果有人知道我的問題在哪裏,請幫我解決它,謝謝。對不起英語不好。

+0

有什麼問題嗎? – jackson

回答

0

您未處理共享聯繫人更新。 當用戶分享他/她的聯繫人時,它就像另一個需要處理的更新。例如,你需要把它添加if語句代碼:

if(isset($contact)){ 
    SendContact($chat_id, $contact, $name); 
} 

你也需要通過「$接觸」和「$名」變量的「sendContact」功能;因爲這些變量是全局變量(您在函數外部定義它們),所以只能在函數之外訪問它們。

function SendContact($chat_id, $contact, $name) 
{ 
    makereq('sendContact', [ 
     'chat_id' => $chat_id, 
     'phone_number' => $contact, 
     'first_name' => $name 
    ]); 
} 

相同的分享位置。