Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/aby.telegram/lib/ |
Upload File : |
<?php namespace Aby\Telegram; class Notification { private $api_url = 'https://api.telegram.org/bot'; /** * @param $token * @param $chats * @param $text * @return bool */ public function send($token, $chats, $text){ if($token && $chats && count($chats) && $text){ $text = $this->convertEncoding($text); $text = htmlspecialchars_decode($text); if($text) { $send_result = false; foreach ($chats as $chat_id) { $send_result = $this->apiRequest("sendMessage", $token, array('chat_id' => $chat_id, "parse_mode" => 'HTML', "text" => $text)); } return $send_result; } } return false; } /** * @param $text * @return null|string */ private function convertEncoding($text){ if (function_exists('mb_convert_encoding')) { if(mb_detect_encoding($text) != 'UTF-8') { return mb_convert_encoding($text, 'UTF-8', ['Windows-1251', 'UTF-8', mb_detect_encoding($text)]); } } return $text; } /** * @param $handle * @return bool */ private function exec_curl_request($handle) { $log_class = new Log(); $response = curl_exec($handle); if ($response === false) { $errno = curl_errno($handle); $error = curl_error($handle); $log_class->write_log("Curl returned error $errno: $error", __FILE__, __LINE__); curl_close($handle); return false; } $http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE)); curl_close($handle); if ($http_code >= 500) { // do not want to DDOS server if something goes wrong sleep(1); return false; } else if ($http_code != 200) { $response = json_decode($response, true); $log_class->write_log("Request has failed with error {$response['error_code']}: {$response['description']}", __FILE__, __LINE__); if ($http_code == 401) { $log_class->write_log("Invalid access token provided", __FILE__, __LINE__); } return false; } else { $response = json_decode($response, true); $response = $response['result']; } return true; } /** * @param $method * @param $token * @param $parameters * @return bool */ private function apiRequest($method, $token, $parameters): bool { 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 = $this->api_url . $token . '/' . $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, 10); return $this->exec_curl_request($handle); } }