From 438222d2858a459b164376ff2c2858f79608dc57 Mon Sep 17 00:00:00 2001 From: dax Date: Wed, 22 May 2019 10:48:08 +0200 Subject: [PATCH] Add 'Telegram.class.php' --- Telegram.class.php | 316 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 316 insertions(+) create mode 100644 Telegram.class.php diff --git a/Telegram.class.php b/Telegram.class.php new file mode 100644 index 0000000..d4473f4 --- /dev/null +++ b/Telegram.class.php @@ -0,0 +1,316 @@ +Token = $token; + } + + # + # Set async calls + # + public function SetAsync($bool) + { + $this->Async = $bool; + } + + # + # add id to whitelist + # + public function PermitFromId($userId) + { + $this->PermittedSenders[] = $userId; + } + + # + # do http call + # + private function _httpSend($method, $data) + { + # + # init curl + # + $ch = curl_init("https://api.telegram.org/bot$this->Token/$method"); + + # + # set options + # + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + curl_setopt($ch, CURLOPT_VERBOSE, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + # + # TODO - manage timeout and retry + # + + # + # do call + # + $result = curl_exec($ch); + + # + # parse response + # + $j = json_decode($result, true); + + # + # output if debug + # + if($this->debug) print_r($j); + + # + # return response from api.telegram + # + return $j; + } + + # + # https://core.telegram.org/bots/api#sendChatAction + # + public function SendChatAction($destination, $action) + { + return $this->_httpSend('sendChatAction', array( + 'chat_id' => $destination, + 'action' => $action + )); + } + + # + # https://core.telegram.org/bots/api#sendMessage + # + public function SendMessage($destination, $textMessage) + { + return $this->_httpSend('sendMessage', array( + 'text' => $textMessage, + 'chat_id' => $destination, + 'parse_mode'=> 'HTML' + )); + } + + # + # https://core.telegram.org/bots/api#sendcontact + # + public function SendContact($destination, $number, $firstName, $lastName=null) + { + return $this->_httpSend('sendContact', array( + 'chat_id' => $destination, + 'phone_number' => $number, + 'first_name' => $firstName, + 'last_name' => $lastName + )); + } + + # + # Send location + # + public function SendLocation($destination, $lat, $long) + { + return $this->_httpSend('sendLocation', array( + 'chat_id' => $destination, + 'latitude' => $lat, + 'longitude' => $long + )); + } + + # + # send document + # + public function SendDocument($destination, $url) + { + if( strpos('http', $url) === 0 ) + { + return $this->_httpSend('sendDocument', array( + 'chat_id' => $destination, + 'photo' => $url + )); + }else{ + return $this->_httpSend('sendDocument', array( + 'chat_id' => $destination, + 'photo' => new CURLFile($url) + )); + } + } + + # + # send image + # + public function SendImage($destination, $url) + { + if( strpos('http', $url) === 0 ) + { + return $this->_httpSend('sendPhoto', array( + 'chat_id' => $destination, + 'photo' => $url + )); + }else{ + return $this->_httpSend('sendPhoto', array( + 'chat_id' => $destination, + 'photo' => new CURLFile($url) + )); + } + } + + # + # add method in queue + # + public function AddMessageHandler($x) + { + $this->actions[] = $x; + } + + # + # listen recevied messages 4 bot + # + public function Listen() + { + # + # last message readed cache + # + $offset = 0; + + # + # infinite loop + # + while(true) + { + try + { + # + # build query + # + $ApiCall = "https://api.telegram.org/bot$this->Token/getUpdates?".http_build_query(array( + 'offset' => $offset + )); + + # + # do query + # + $result = file_get_contents($ApiCall); + + # + # parse response + # + $x = json_decode($result); + + # + # length + # + $count = count((array)$x->result); + + if( $count ) + { + # + # each messages + # + foreach( $x->result as &$message ) + { + # + # message type helpers + # + $message->message->IsText = isset($message->message->text); + $message->message->IsDocument = isset($message->message->document); + $message->message->IsPhoto = isset($message->message->photo); + $message->message->IsVoice = isset($message->message->voice); + + # + # check whitelist + # + if( count($this->PermittedSenders)) + { + if(!in_array($message->message->from->id, $this->PermittedSenders)) + { + # + # debug + # + if($this->debug) echo "*** Discarded message from ".$message->message->from->id." ***\n"; + + # + # set as read + # + $offset = $message->update_id+1; + + # + # jump + # + continue; + } + } + + # + # debug + # + if($this->debug) print_r($message); + + # + # iterate actions + # + foreach( $this->actions as $action ) + { + # + # execute anonymous action (as function) + # + $action($message->message, $this); + } + + # + # update offset 4 next http call - we comunicate at api.telegram that we have readed the message + # + $offset = $message->update_id+1; + } + } + } + catch(Exception $e) + { + if($this->debug) echo $e->getMessage()."\n"; + } + finally { + # + # loop standby + # + usleep(1000); + } + } + } + + public static function Contains($x,$y) + { + return strpos( strtolower($y), strtolower($x) ) !== false; + } + +} + +?> \ No newline at end of file