varrcan /
telegram.send
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Telegram\Send; |
||||
| 4 | |||||
| 5 | use Bitrix\Main\Diag\Debug; |
||||
| 6 | |||||
| 7 | /** |
||||
| 8 | * Class Sending |
||||
| 9 | * @package Telegram\Send |
||||
| 10 | */ |
||||
| 11 | class Sending |
||||
| 12 | { |
||||
| 13 | private $apiKey; |
||||
| 14 | private $chatId; |
||||
| 15 | private $arUpdates; |
||||
| 16 | private $apiBaseUri = 'https://api.telegram.org/bot'; |
||||
| 17 | |||||
| 18 | /** |
||||
| 19 | * Sending constructor. |
||||
| 20 | */ |
||||
| 21 | public function __construct() |
||||
| 22 | { |
||||
| 23 | $this->apiKey = Config::getToken(); |
||||
| 24 | $this->chatId = Config::getUser(); |
||||
| 25 | } |
||||
| 26 | |||||
| 27 | /** |
||||
| 28 | * Отправка сообщения |
||||
| 29 | * |
||||
| 30 | * @param $message |
||||
| 31 | */ |
||||
| 32 | public function processMessage($message) |
||||
| 33 | { |
||||
| 34 | foreach ($this->chatId as $key => $value) { |
||||
| 35 | $this->apiRequest('sendMessage', ['chat_id' => $key, 'text' => $message]); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 36 | } |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | /** |
||||
| 40 | * Подготовка curl запроса |
||||
| 41 | * |
||||
| 42 | * @param $method |
||||
| 43 | * @param bool $parameters |
||||
| 44 | * |
||||
| 45 | * @return mixed |
||||
| 46 | */ |
||||
| 47 | public function apiRequest($method, $parameters = false) |
||||
| 48 | { |
||||
| 49 | $handle = curl_init(); |
||||
| 50 | $options = [ |
||||
| 51 | CURLOPT_URL => $this->apiBaseUri . $this->apiKey . '/' . $method . '?' . http_build_query($parameters), |
||||
| 52 | CURLOPT_RETURNTRANSFER => true, |
||||
| 53 | CURLOPT_FOLLOWLOCATION => 1, |
||||
| 54 | CURLOPT_CONNECTTIMEOUT => 5, |
||||
| 55 | CURLOPT_TIMEOUT => 30, |
||||
| 56 | CURLOPT_POST => true, |
||||
| 57 | CURLOPT_FRESH_CONNECT => true, |
||||
| 58 | CURLOPT_UNRESTRICTED_AUTH => true, |
||||
| 59 | ]; |
||||
| 60 | |||||
| 61 | if (Config::statusProxy()) { |
||||
| 62 | $proxy = Config::proxyData(); |
||||
| 63 | |||||
| 64 | //TODO: why not work? |
||||
| 65 | //if (filter_var($proxy['url'], FILTER_VALIDATE_IP)) { |
||||
| 66 | // $options[CURLOPT_PROXYTYPE] = CURLPROXY_SOCKS5; |
||||
| 67 | //} else { |
||||
| 68 | // $options[CURLOPT_PROXYTYPE] = CURLPROXY_SOCKS5_HOSTNAME; |
||||
| 69 | //} |
||||
| 70 | |||||
| 71 | $options += [ |
||||
| 72 | CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5_HOSTNAME, |
||||
| 73 | CURLOPT_PROXY => $proxy['url'], |
||||
| 74 | CURLOPT_PROXYPORT => $proxy['port'], |
||||
| 75 | CURLOPT_PROXYUSERNAME => $proxy['user'], |
||||
| 76 | CURLOPT_PROXYPASSWORD => $proxy['pass'], |
||||
| 77 | ]; |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | curl_setopt_array($handle, $options); |
||||
| 81 | |||||
| 82 | return $this->execCurlRequest($handle); |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | /** |
||||
| 86 | * Отправка curl запроса |
||||
| 87 | * |
||||
| 88 | * @param $handle |
||||
| 89 | * |
||||
| 90 | * @return mixed |
||||
| 91 | */ |
||||
| 92 | public function execCurlRequest($handle) |
||||
| 93 | { |
||||
| 94 | $response = curl_exec($handle); |
||||
| 95 | if ((int)curl_getinfo($handle, CURLINFO_HTTP_CODE) === 200) { |
||||
| 96 | $response = json_decode($response, true)['result']; |
||||
| 97 | } else { |
||||
| 98 | Debug::writeToFile(curl_getinfo($handle), 'execCurlRequest', 'telegram-log'); |
||||
| 99 | curl_close($handle); |
||||
| 100 | } |
||||
| 101 | |||||
| 102 | return $response; |
||||
| 103 | } |
||||
| 104 | |||||
| 105 | /** |
||||
| 106 | * Обновление входящих запросов |
||||
| 107 | * |
||||
| 108 | * @param int $offset |
||||
| 109 | * @param int $limit |
||||
| 110 | * |
||||
| 111 | * @return mixed |
||||
| 112 | */ |
||||
| 113 | public function updatesUser($offset = 0, $limit = 100) |
||||
| 114 | { |
||||
| 115 | $paramRequest = ['offset' => $offset, 'limit' => $limit]; |
||||
| 116 | $this->arUpdates = $this->apiRequest('getUpdates', $paramRequest); |
||||
|
0 ignored issues
–
show
$paramRequest of type array<string,integer> is incompatible with the type boolean expected by parameter $parameters of Telegram\Send\Sending::apiRequest().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 117 | if (\count($this->arUpdates) >= 1) { |
||||
| 118 | $lastElementId = $this->arUpdates[\count($this->arUpdates) - 1]['update_id'] + 1; |
||||
| 119 | $paramRequest = ['offset' => $lastElementId, 'limit' => '1']; |
||||
| 120 | $this->apiRequest('getUpdates', $paramRequest); |
||||
|
0 ignored issues
–
show
$paramRequest of type array<string,integer|string> is incompatible with the type boolean expected by parameter $parameters of Telegram\Send\Sending::apiRequest().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 121 | } |
||||
| 122 | |||||
| 123 | return $this->arUpdates; |
||||
| 124 | } |
||||
| 125 | } // |
||||
| 126 |