-
-
Save uzbekdev1/81598f9138ee3bb82fb6502892632bf9 to your computer and use it in GitHub Desktop.
php: telegram bot api example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // get API KEY: https://core.telegram.org/bots#botfather | |
| // get chad id: http://stackoverflow.com/questions/31078710/how-to-obtain-telegram-chat-id-for-a-specific-user | |
| $api_key = 'FAKE1234'; | |
| $chat_id = '7850602' | |
| $url = "https://api.telegram.org/bot".$api_key."/"; | |
| // UNCOMMENT desired method | |
| // getMe | |
| $method = 'getMe'; | |
| $params = array | |
| ( | |
| ); | |
| // sendMessage | |
| /* | |
| $method = 'sendMessage'; | |
| $params = array | |
| ( | |
| 'chat_id' => $chat_id, | |
| 'text' => 'kaixo', | |
| 'disable_web_page_preview' => null, | |
| 'reply_to_message_id' => null, | |
| 'reply_markup' => null | |
| ); | |
| */ | |
| // forwardMessage | |
| /* | |
| $method = 'forwardMessage'; | |
| $params = array | |
| ( | |
| 'chat_id' => $chat_id, | |
| 'from_chat_id' => '', | |
| 'message_id' => '' | |
| ); | |
| */ | |
| // getUpdates | |
| /* | |
| $method = 'getUpdates'; | |
| $params = array | |
| ( | |
| 'offset' => null, | |
| 'limit' => null, | |
| 'timeout' => null | |
| ); | |
| */ | |
| // sendPhoto | |
| /* | |
| $method = 'sendPhoto'; | |
| $filename = '/home/zital/Downloads/gif/hxTS1AB.jpg' | |
| if(class_exists('CURLFile')) | |
| $cfile = new CURLFile($filename); | |
| else | |
| $cfile = "@".$filename; | |
| $params = array | |
| ( | |
| 'chat_id' => $chat_id, | |
| 'photo' => $cfile, | |
| 'reply_to_message_id' => null, | |
| 'reply_markup' => null | |
| ); | |
| */ | |
| // sendAudio | |
| /* | |
| $method = 'sendAudio'; | |
| $filename = '/home/zital/Downloads/loop.mp3' | |
| if(class_exists('CURLFile')) | |
| $cfile = new CURLFile($filename); | |
| else | |
| $cfile = "@".$filename; | |
| $params = array | |
| ( | |
| 'chat_id' => $chat_id, | |
| 'audio' => $cfile, | |
| 'reply_to_message_id' => null, | |
| 'reply_markup' => null | |
| ); | |
| */ | |
| // sendVideo | |
| /* | |
| $method = 'sendVideo'; | |
| $filename = '/home/zital/Downloads/output.mp4'; | |
| if(class_exists('CURLFile')) | |
| $cfile = new CURLFile($filename); | |
| else | |
| $cfile = "@".$filename; | |
| $params = array | |
| ( | |
| 'chat_id' => $chat_id, | |
| 'video' => $cfile, | |
| 'reply_to_message_id' => null, | |
| 'reply_markup' => null | |
| ); | |
| */ | |
| // sendDocument | |
| /* | |
| $method = 'sendDocument'; | |
| $filename = '/home/zital/Downloads/OYSE002.pdf' | |
| if(class_exists('CURLFile')) | |
| $cfile = new CURLFile($filename); | |
| else | |
| $cfile = "@".$filename; | |
| $params = array | |
| ( | |
| 'chat_id' => $chat_id, | |
| 'document' => $cfile, | |
| 'reply_to_message_id' => null, | |
| 'reply_markup' => null | |
| ); | |
| */ | |
| // sendLocation | |
| /* | |
| $method = 'sendLocation'; | |
| $params = array | |
| ( | |
| 'chat_id' => $chat_id, | |
| 'latitude' => '43.4193408', | |
| 'longitude' => '-2.7253832,16', | |
| 'reply_to_message_id' => null, | |
| 'reply_markup' => null | |
| ); | |
| */ | |
| $c = new curl(); | |
| $r = $c->request($url.$method, 'POST', $params); | |
| $j = json_decode($r, true); | |
| if($j) | |
| print_r($j); | |
| else | |
| echo $r; | |
| class curl | |
| { | |
| private $curl_obj; | |
| public function __construct() | |
| { | |
| if(!function_exists('curl_init')) | |
| { | |
| echo 'ERROR: Install CURL module for php'; | |
| exit(); | |
| } | |
| $this->init(); | |
| } | |
| public function init() | |
| { | |
| $this->curl_obj = curl_init(); | |
| } | |
| public function request($url, $method = 'GET', $params = array(), $opts = array()) | |
| { | |
| $method = trim(strtoupper($method)); | |
| // default opts | |
| $opts[CURLOPT_FOLLOWLOCATION] = true; | |
| $opts[CURLOPT_RETURNTRANSFER] = 1; | |
| //$opts[CURLOPT_FRESH_CONNECT] = true; | |
| if($method==='GET') | |
| { | |
| $url .= "?".$params; | |
| $params = http_build_query($params); | |
| } | |
| elseif($method==='POST') | |
| { | |
| $opts[CURLOPT_POST] = 1; | |
| $opts[CURLOPT_POSTFIELDS] = $params; | |
| } | |
| $opts[CURLOPT_URL] = $url; | |
| curl_setopt_array($this->curl_obj, $opts); | |
| $content = curl_exec($this->curl_obj); | |
| return $content; | |
| } | |
| public function close() | |
| { | |
| if(gettype($this->curl_obj) === 'resource') | |
| curl_close($this->curl_obj); | |
| } | |
| public function __destruct() | |
| { | |
| $this->close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment