get data from telegram bot with php -
i have following code make 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 string\n"); return false; } if (!$parameters) { $parameters = array(); } else if (!is_array($parameters)) { error_log("parameters must array\n"); return false; } foreach ($parameters $key => &$val) { // encoding json array parameters, 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 open file!"); fwrite($myfile, "$txtdata"); fclose($myfile); } //========== if($t == "test1"){ var_dump(makereq('sendmessage',[ 'chat_id'=>$chat_id, 'text'=>"okay share number, location , 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 1 submit in our bot: name 👤 : $name id 🗣 : @$username phone number 📱 : $contact address 📫 : $location zip code 📦 : ", ])); } ?> it works fine , data users , shows name, id, not show user phone number, address...
i guess code fine (base on documantions), don't know why it's not working , not show phone number , address.
if know problem is, me solve it, thanks. sorry bad english.
you not handling share contact update. when user shares his/her contact, update need handled. example need add if statement code:
if(isset($contact)){ sendcontact($chat_id, $contact, $name); } also need pass "$contact" , "$name" variable "sendcontact" function; these variable global (you defined them outside of fucntion) can accessed outside function.
function sendcontact($chat_id, $contact, $name) { makereq('sendcontact', [ 'chat_id' => $chat_id, 'phone_number' => $contact, 'first_name' => $name ]); } the same sharing location.
Comments
Post a Comment