用php从电报机器人获取数据

时间:2017-08-15 14:27:23

标签: php telegram telegram-bot

我有以下代码来制作telegram bot

<?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),我只是不知道它为什么不工作而且没有显示电话号码和地址。

如果有人知道我的问题在哪里,请帮我解决,谢谢。抱歉英文不好。

1 个答案:

答案 0 :(得分:0)

您没有处理共享联系人更新。 当用户分享他/她的联系时,它就像另一个需要处理的更新。例如,您需要将此if语句添加到您的代码中:

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

你还需要通过&#34; $ contact&#34;和&#34; $ name&#34;变量为&#34; sendContact&#34;功能;由于这些变量是全局的(您在函数之外定义它们),因此只能在函数外部访问它们。

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

共享位置相同。