Source of file api.telegram.php
Size: 35,800 Bytes - Last Modified: 2024-04-27T15:26:05+03:00
/tmp/current_snapshot/api/libs/api.telegram.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963 | <?php /** * Telegram bot API implementation */ class UbillingTelegram { /** * Contains system alter config as key=>value * * @var array */ protected $altCfg = array(); /** * Contains current instance bot token * * @var string */ protected $botToken = ''; /** * Default debug flag wich enables telegram replies display * * @var bool */ protected $debug = false; /** * Contains base Telegram API URL */ protected $apiUrl = 'https://api.telegram.org/bot'; /** * Contains telegram messages path */ const QUEUE_PATH = 'content/telegram/'; /** * Maximum message length */ const MESSAGE_LIMIT = 4095; /** * Creates new Telegram object instance * * @param string $token */ public function __construct($token = '') { if (!empty($token)) { $this->botToken = $token; } $this->loadAlter(); $this->setOptions(); } /** * Sets current instance auth token * * @param string $token * * @return void */ public function setToken($token) { $this->botToken = $token; } /** * Object instance debug state setter * * @param bool $state * * @return void */ public function setDebug($state) { $this->debug = $state; } /** * Loads system alter config into protected property for further usage * * @global object $ubillingConfig * * @return void */ protected function loadAlter() { global $ubillingConfig; $this->altCfg = $ubillingConfig->getAlter(); } /** * Sets some current instance options if required * * @return void */ protected function setOptions() { //settin debug flag if (isset($this->altCfg['TELEGRAM_DEBUG'])) { if ($this->altCfg['TELEGRAM_DEBUG']) { $this->debug = true; } } if (isset($this->altCfg['TELEGRAM_API_URL'])) { if (!empty($this->altCfg['TELEGRAM_API_URL'])) { $this->setApiUrl($this->altCfg['TELEGRAM_API_URL']); } } } /** * Setter of custom API URL (legacy fallback) * * @param string $url * * @return void */ protected function setApiUrl($url) { $this->apiUrl = $url; } /** * Stores message in telegram sending queue. Use this method in your modules. * * @param int $chatid * @param string $message * @param bool $translit * @param string $module * * @return bool */ public function sendMessage($chatid, $message, $translit = false, $module = '') { $result = false; $chatid = trim($chatid); $module = (!empty($module)) ? ' MODULE ' . $module : ''; $prefix = 'tlg_'; if (!empty($chatid)) { $message = str_replace(array("\n\r", "\n", "\r"), ' ', $message); if ($translit) { $message = zb_TranslitString($message); } $message = trim($message); $queueId = time(); $offset = 0; $filename = self::QUEUE_PATH . $prefix . $queueId . '_' . $offset; if (file_exists($filename)) { while (file_exists($filename)) { $offset++; //incremeting number of messages per second $filename = self::QUEUE_PATH . $prefix . $queueId . '_' . $offset; } } $storedata = 'CHATID="' . $chatid . '"' . "\n"; $storedata .= 'MESSAGE="' . $message . '"' . "\n"; file_put_contents($filename, $storedata); log_register('UTLG SEND MESSAGE FOR `' . $chatid . '` AS `' . $prefix . $queueId . '_' . $offset . '` ' . $module); $result = true; } return ($result); } /** * Returns count of messages available in queue * * @return int */ public function getQueueCount() { $messagesQueueCount = rcms_scandir(self::QUEUE_PATH); $result = sizeof($messagesQueueCount); return ($result); } /** * Returns array containing all messages queue data as index=>data * * @return array */ public function getQueueData() { $result = array(); $messagesQueue = rcms_scandir(self::QUEUE_PATH); if (!empty($messagesQueue)) { foreach ($messagesQueue as $io => $eachmessage) { $messageDate = date("Y-m-d H:i:s", filectime(self::QUEUE_PATH . $eachmessage)); $messageData = rcms_parse_ini_file(self::QUEUE_PATH . $eachmessage); $result[$io]['filename'] = $eachmessage; $result[$io]['date'] = $messageDate; $result[$io]['chatid'] = $messageData['CHATID']; $result[$io]['message'] = $messageData['MESSAGE']; } } return ($result); } /** * Deletes message from local queue * * @param string $filename Existing message filename * * @return int 0 - ok, 1 - deletion unsuccessful, 2 - file not found */ public function deleteMessage($filename) { if (file_exists(self::QUEUE_PATH . $filename)) { rcms_delete_files(self::QUEUE_PATH . $filename); $result = 0; if (file_exists(self::QUEUE_PATH . $filename)) { $result = 1; } } else { $result = 2; } return ($result); } /** * Returns raw updates array * * @param int $offset * @param int $limit * @param int $timeout * * @return array * * @throws Exception */ protected function getUpdatesRaw($offset = '', $limit = '', $timeout = '') { $result = array(); $timeout = vf($timeout, 3); $limit = vf($limit, 3); $offset = mysql_real_escape_string($offset); $timeout = (!empty($timeout)) ? $timeout : 0; //default timeout in seconds is 0 $limit = (!empty($limit)) ? $limit : 100; //defult limit is 100 /** * Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates. * By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is * called with an offset higher than its update_id. The negative offset can be specified to retrieve updates starting from -offset update from * the end of the updates queue. All previous updates will forgotten. */ $offset = (!empty($offset)) ? '&offset=' . $offset : ''; if (!empty($this->botToken)) { $options = '?timeout=' . $timeout . '&limit=' . $limit . $offset; $url = $this->apiUrl . $this->botToken . '/getUpdates' . $options; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POST, 1); @$reply = curl_exec($ch); if ($this->debug) { $curlError = curl_error($ch); show_info($url); if (!empty($curlError)) { show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError); } else { show_success(__('Telegram API connection to') . ' ' . $this->apiUrl . ' ' . __('success')); } } curl_close($ch); if (!empty($reply)) { $result = json_decode($reply, true); } if ($this->debug) { debarr($result); } } else { throw new Exception('EX_TOKEN_EMPTY'); } return ($result); } /** * Returns all messages received by bot * * @return array * @throws Exception */ public function getBotChats() { $result = array(); if (!empty($this->botToken)) { $rawUpdates = $this->getUpdatesRaw(); if (!empty($rawUpdates)) { if (isset($rawUpdates['result'])) { $allUpdates = $rawUpdates['result']; foreach ($allUpdates as $io => $each) { if (isset($each['message'])) { if (isset($each['message']['chat'])) { if (isset($each['message']['chat']['type'])) { $messageData = $each['message']; if ($messageData['chat']['type'] == 'private') { //direct message if (isset($messageData['message_id'])) { $messageId = $messageData['message_id']; $result[$messageId]['id'] = $messageId; $result[$messageId]['date'] = date("Y-m-d H:i:s", $messageData['date']); $result[$messageId]['chatid'] = $messageData['from']['id']; $result[$messageId]['from'] = @$messageData['from']['username']; $result[$messageId]['text'] = @$messageData['text']; $result[$messageId]['type'] = 'user'; $result[$messageId]['chanid'] = ''; $result[$messageId]['channame'] = ''; $result[$messageId]['updateid'] = @$each['update_id']; } } //supergroup message if ($messageData['chat']['type'] == 'supergroup') { if (isset($messageData['message_id'])) { $messageId = $messageData['message_id']; $result[$messageId]['id'] = $messageId; $result[$messageId]['date'] = date("Y-m-d H:i:s", $messageData['date']); $result[$messageId]['chatid'] = $messageData['from']['id']; $result[$messageId]['from'] = @$messageData['from']['username']; $result[$messageId]['text'] = @$messageData['text']; $result[$messageId]['type'] = 'supergroup'; $result[$messageId]['chanid'] = $messageData['chat']['id']; $result[$messageId]['channame'] = $messageData['chat']['username']; $result[$messageId]['updateid'] = ''; $result[$messageId]['updateid'] = @$each['update_id']; } } } } } //channel message if (isset($each['channel_post'])) { $messageData = $each['channel_post']; if (isset($messageData['message_id'])) { $messageId = $messageData['message_id']; $result[$messageId]['id'] = $messageId; $result[$messageId]['date'] = date("Y-m-d H:i:s", $messageData['date']); $result[$messageId]['chatid'] = $messageData['chat']['id']; $result[$messageId]['from'] = @$messageData['chat']['username']; $result[$messageId]['text'] = @$messageData['text']; $result[$messageId]['type'] = 'channel'; } } } } } } else { throw new Exception('EX_TOKEN_EMPTY'); } return ($result); } /** * Returns current bot contacts list as chat_id=>name * * @return array */ public function getBotContacts() { $result = array(); $updatesRaw = $this->getUpdatesRaw(); if (!empty($updatesRaw)) { if (isset($updatesRaw['result'])) { if (!empty($updatesRaw['result'])) { foreach ($updatesRaw['result'] as $io => $each) { //supergroup messages if (isset($each['message'])) { if (isset($each['message']['chat'])) { if (isset($each['message']['chat']['type'])) { if ($each['message']['chat']['type'] = 'supergroup') { $groupData = $each['message']['chat']; $result[$groupData['id']]['chatid'] = $groupData['id']; $groupName = (!empty($groupData['username'])) ? $groupData['username'] : @$groupData['title']; //only title for private groups $result[$groupData['id']]['name'] = $groupName; $result[$groupData['id']]['first_name'] = @$groupData['title']; $result[$groupData['id']]['last_name'] = ''; $result[$groupData['id']]['type'] = 'supergroup'; $result[$groupData['id']]['lastmessage'] = strip_tags(@$each['message']['text']); } } } } //direct user message if (isset($each['message'])) { if (isset($each['message']['from'])) { if (isset($each['message']['from']['id'])) { $messageData = $each['message']['from']; $result[$messageData['id']]['chatid'] = $messageData['id']; $result[$messageData['id']]['name'] = @$messageData['username']; //may be empty $result[$messageData['id']]['first_name'] = @$messageData['first_name']; $result[$messageData['id']]['last_name'] = @$messageData['last_name']; $result[$messageData['id']]['type'] = 'user'; $result[$messageData['id']]['lastmessage'] = strip_tags(@$each['message']['text']); } } } //channel message if (isset($each['channel_post'])) { if (isset($each['channel_post']['chat'])) { if (isset($each['channel_post']['chat']['id'])) { $chatData = $each['channel_post']['chat']; $result[$chatData['id']]['chatid'] = $chatData['id']; $result[$chatData['id']]['name'] = $chatData['username']; $result[$chatData['id']]['first_name'] = ''; $result[$chatData['id']]['last_name'] = ''; $result[$chatData['id']]['type'] = 'channel'; $result[$messageData['id']]['lastmessage'] = strip_tags(@$each['message']['text']); } } } } } } } return ($result); } /** * Preprocess keyboard for sending with directPushMessage * * @param array $buttonsArray * @param bool $inline * @param bool $resize * @param bool $oneTime * * @return array */ public function makeKeyboard($buttonsArray, $inline = false, $resize = true, $oneTime = false) { $result = array(); if (!empty($buttonsArray)) { if (!$inline) { $result['type'] = 'keyboard'; $keyboardMarkup = array( 'keyboard' => $buttonsArray, 'resize_keyboard' => $resize, 'one_time_keyboard' => $oneTime ); $result['markup'] = $keyboardMarkup; } if ($inline) { $result['type'] = 'inline'; $keyboardMarkup = $buttonsArray; $result['markup'] = $keyboardMarkup; } } return($result); } /** * Split message into chunks of safe size * * @param string $message * * @return array */ protected function splitMessage($message) { $result = preg_split('~~u', $message, -1, PREG_SPLIT_NO_EMPTY); $chunks = array_chunk($result, self::MESSAGE_LIMIT); foreach ($chunks as $i => $chunk) { $chunks[$i] = join('', (array) $chunk); } $result = $chunks; return ($result); } /** * Sends message to some chat id using Telegram API * * @param int $chatid remote chatId * @param string $message text message to send * @param array $keyboard keyboard encoded with makeKeyboard method * @param bool $nosplit dont automatically split message into 4096 slices * @param int $replyToMsgId optional message ID which is reply for * * @return string/bool */ public function directPushMessage($chatid, $message, $keyboard = array(), $noSplit = false, $replyToMsgId = '') { $result = ''; if ($noSplit) { $result = $this->apiSendMessage($chatid, $message, $keyboard, $replyToMsgId); } else { $messageSize = mb_strlen($message, 'UTF-8'); if ($messageSize > self::MESSAGE_LIMIT) { $messageSplit = $this->splitMessage($message); if (!empty($messageSplit)) { foreach ($messageSplit as $io => $eachMessagePart) { $result = $this->apiSendMessage($chatid, $eachMessagePart, $keyboard, $replyToMsgId); } } } else { $result = $this->apiSendMessage($chatid, $message, $keyboard, $replyToMsgId); } } return($result); } /** * Sends message to some chat id via Telegram API * * @param int $chatid remote chatId * @param string $message text message to send * @param array $keyboard keyboard encoded with makeKeyboard method * @param int $replyToMsgId optional message ID which is reply for * @throws Exception * * @return string/bool */ protected function apiSendMessage($chatid, $message, $keyboard = array(), $replyToMsgId = '') { $result = ''; $data['chat_id'] = $chatid; $data['text'] = $message; if ($this->debug) { debarr($data); } //default sending method $method = 'sendMessage'; //setting optional replied message ID for normal messages if ($replyToMsgId) { $method = 'sendMessage?reply_to_message_id=' . $replyToMsgId; } //location sending if (ispos($message, 'sendLocation:')) { $cleanGeo = str_replace('sendLocation:', '', $message); $cleanGeo = explode(',', $cleanGeo); $geoLat = trim($cleanGeo[0]); $geoLon = trim($cleanGeo[1]); $locationParams = '?chat_id=' . $chatid . '&latitude=' . $geoLat . '&longitude=' . $geoLon; if ($replyToMsgId) { $locationParams .= '&reply_to_message_id=' . $replyToMsgId; } $method = 'sendLocation' . $locationParams; } //custom markdown if (ispos($message, 'parseMode:{')) { if (preg_match('!\{(.*?)\}!si', $message, $tmpMode)) { $cleanParseMode = $tmpMode[1]; $parseModeMask = 'parseMode:{' . $cleanParseMode . '}'; $cleanMessage = str_replace($parseModeMask, '', $message); $data['text'] = $cleanMessage; $method = 'sendMessage?parse_mode=' . $cleanParseMode; if ($replyToMsgId) { $method .= '&reply_to_message_id=' . $replyToMsgId; } } } //venue sending if (ispos($message, 'sendVenue:')) { if (preg_match('!\[(.*?)\]!si', $message, $tmpGeo)) { $cleanGeo = $tmpGeo[1]; } if (preg_match('!\((.*?)\)!si', $message, $tmpAddr)) { $cleanAddr = $tmpAddr[1]; } if (preg_match('!\{(.*?)\}!si', $message, $tmpTitle)) { $cleanTitle = $tmpTitle[1]; } $data['title'] = $cleanTitle; $data['address'] = $cleanAddr; $cleanGeo = explode(',', $cleanGeo); $geoLat = trim($cleanGeo[0]); $geoLon = trim($cleanGeo[1]); $locationParams = '?chat_id=' . $chatid . '&latitude=' . $geoLat . '&longitude=' . $geoLon; if ($replyToMsgId) { $locationParams .= '&reply_to_message_id=' . $replyToMsgId; } $method = 'sendVenue' . $locationParams; } //photo sending if (ispos($message, 'sendPhoto:')) { if (preg_match('!\[(.*?)\]!si', $message, $tmpPhoto)) { $cleanPhoto = $tmpPhoto[1]; } if (preg_match('!\{(.*?)\}!si', $message, $tmpCaption)) { $cleanCaption = $tmpCaption[1]; $cleanCaption = urlencode($cleanCaption); } $photoParams = '?chat_id=' . $chatid . '&photo=' . $cleanPhoto; if (!empty($cleanCaption)) { $photoParams .= '&caption=' . $cleanCaption; } if ($replyToMsgId) { $photoParams .= '&reply_to_message_id=' . $replyToMsgId; } $method = 'sendPhoto' . $photoParams; } //sending keyboard if (!empty($keyboard)) { if (isset($keyboard['type'])) { if ($keyboard['type'] == 'keyboard') { $encodedKeyboard = json_encode($keyboard['markup']); $data['reply_markup'] = $encodedKeyboard; } if ($keyboard['type'] == 'inline') { $encodedKeyboard = json_encode(array('inline_keyboard' => $keyboard['markup'])); $data['reply_markup'] = $encodedKeyboard; $data['parse_mode'] = 'HTML'; } $method = 'sendMessage'; } } //removing keyboard if (ispos($message, 'removeKeyboard:')) { $keybRemove = array( 'remove_keyboard' => true ); $encodedMarkup = json_encode($keybRemove); $cleanMessage = str_replace('removeKeyboard:', '', $message); if (empty($cleanMessage)) { $cleanMessage = __('Keyboard deleted'); } $data['text'] = $cleanMessage; $data['reply_markup'] = $encodedMarkup; } //POST data encoding $data_json = json_encode($data); if (!empty($this->botToken)) { $url = $this->apiUrl . $this->botToken . '/' . $method; if ($this->debug) { deb($url); } $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); if ($this->debug) { $result = curl_exec($ch); deb($result); $curlError = curl_error($ch); if (!empty($curlError)) { show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError); } else { show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success')); } } else { $result = curl_exec($ch); } curl_close($ch); } else { throw new Exception('EX_TOKEN_EMPTY'); } return($result); } /** * Sets HTTPS web hook URL for some bot * * @param string $webHookUrl HTTPS url to send updates to. Use an empty string to remove webhook integration * @param int $maxConnections Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40. * * @return string */ public function setWebHook($webHookUrl, $maxConnections = 40) { $result = ''; if (!empty($this->botToken)) { $data = array(); if (!empty($webHookUrl)) { $method = 'setWebhook'; if (ispos($webHookUrl, 'https://')) { $data['url'] = $webHookUrl; $data['max_connections'] = $maxConnections; } else { throw new Exception('EX_NOT_SSL_URL'); } } else { $method = 'deleteWebhook'; } $url = $this->apiUrl . $this->botToken . '/' . $method; if ($this->debug) { deb($url); } $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POST, 1); if (!empty($data)) { $data_json = json_encode($data); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); } if ($this->debug) { $result = curl_exec($ch); deb($result); $curlError = curl_error($ch); if (!empty($curlError)) { show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError); } else { show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success')); } } else { $result = curl_exec($ch); } curl_close($ch); } else { throw new Exception('EX_TOKEN_EMPTY'); } return($result); } /** * Returns bot web hook info * * @return string */ public function getWebHookInfo() { $result = ''; if (!empty($this->botToken)) { $method = 'getWebhookInfo'; $url = $this->apiUrl . $this->botToken . '/' . $method; if ($this->debug) { deb($url); } $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); if ($this->debug) { $result = curl_exec($ch); deb($result); $curlError = curl_error($ch); if (!empty($curlError)) { show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError); } else { show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success')); } } else { $result = curl_exec($ch); } curl_close($ch); } return($result); } /** * Returns chat data array by its chatId * * @param int chatId * * @return array */ public function getChatInfo($chatId) { $result = array(); if (!empty($this->botToken) AND ( !empty($chatId))) { $method = 'getChat'; $url = $this->apiUrl . $this->botToken . '/' . $method . '?chat_id=' . $chatId; if ($this->debug) { deb($url); } $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); if ($this->debug) { $result = curl_exec($ch); deb($result); $curlError = curl_error($ch); if (!empty($curlError)) { show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError); } else { show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success')); } } else { $result = curl_exec($ch); } curl_close($ch); if (!empty($result)) { $result = json_decode($result, true); } } return($result); } /** * Returns file path by its file ID * * @param string $fileId * * @return string */ public function getFilePath($fileId) { $result = ''; if (!empty($this->botToken)) { $method = 'getFile'; $url = $this->apiUrl . $this->botToken . '/' . $method . '?file_id=' . $fileId; if ($this->debug) { deb($url); } $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); if ($this->debug) { $result = curl_exec($ch); deb($result); $curlError = curl_error($ch); if (!empty($curlError)) { show_error(__('Error') . ' ' . __('Telegram') . ': ' . $curlError); } else { show_success(__('Telegram API sending via') . ' ' . $this->apiUrl . ' ' . __('success')); } } else { $result = curl_exec($ch); } curl_close($ch); if (!empty($result)) { $result = json_decode($result, true); if (@$result['ok']) { //we got it! $result = $result['result']['file_path']; } else { //something went wrong $result = ''; } } } return($result); } /** * Returns some file content * * @param string $filePath * * @return mixed */ public function downloadFile($filePath) { $result = ''; if (!empty($this->botToken)) { $cleanApiUrl = str_replace('bot', '', $this->apiUrl); $url = $cleanApiUrl . 'file/bot' . $this->botToken . '/' . $filePath; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); $result = curl_exec($ch); curl_close($ch); } return($result); } /** * Returns preprocessed message in standard, fixed fields format * * @param array $messageData * @param bool $isChannel * * @return array */ protected function preprocessMessageData($messageData, $isChannel = false) { $result = array(); $result['message_id'] = $messageData['message_id']; if (!$isChannel) { //normal messages/groups $result['from']['id'] = $messageData['from']['id']; $result['from']['first_name'] = $messageData['from']['first_name']; @$result['from']['username'] = $messageData['from']['username']; @$result['from']['language_code'] = $messageData['from']['language_code']; } else { //channel posts $result['from']['id'] = $messageData['sender_chat']['id']; $result['from']['first_name'] = $messageData['sender_chat']['title']; @$result['from']['username'] = $messageData['sender_chat']['username']; @$result['from']['language_code'] = ''; } $result['chat']['id'] = $messageData['chat']['id']; $result['date'] = $messageData['date']; $result['chat']['type'] = $messageData['chat']['type']; @$result['text'] = $messageData['text']; @$result['photo'] = $messageData['photo']; @$result['document'] = $messageData['document']; //photos and documents have only caption if (!empty($result['photo']) OR ! empty($result['document'])) { @$result['text'] = $messageData['caption']; } @$result['voice'] = $messageData['voice']; @$result['audio'] = $messageData['audio']; @$result['video_note'] = $messageData['video_note']; @$result['location'] = $messageData['location']; @$result['sticker'] = $messageData['sticker']; @$result['new_chat_member'] = $messageData['new_chat_member']; @$result['left_chat_member'] = $messageData['left_chat_member']; @$result['reply_to_message'] = $messageData['reply_to_message']; //decode replied message too if received if ($result['reply_to_message']) { $result['reply_to_message'] = $this->preprocessMessageData($result['reply_to_message']); } return($result); } /** * Returns webhook data * * @param bool $rawData receive raw reply or preprocess to something more simple. * * @return array */ public function getHookData($rawData = false) { $result = array(); $postRaw = file_get_contents('php://input'); if (!empty($postRaw)) { $postRaw = json_decode($postRaw, true); if ($this->debug) { debarr($result); } if (!$rawData) { if (isset($postRaw['message'])) { if (isset($postRaw['message']['from'])) { $result = $this->preprocessMessageData($postRaw['message']); } } else { if (isset($postRaw['channel_post'])) { $result = $this->preprocessMessageData($postRaw['channel_post'], true); } } } else { $result = $postRaw; } } return($result); } } |