diff --git a/src/Sync.php b/src/Sync.php index 026345e..d355a2b 100644 --- a/src/Sync.php +++ b/src/Sync.php @@ -198,8 +198,10 @@ protected function applyConfiguration() if ($this->config['sendAlertsToTelegram']) { $bot = $this->config['sendAlertsToTelegram'][0]; $channel = $this->config['sendAlertsToTelegram'][1]; + $proxy = isset($this->config['sendAlertsToTelegram'][2]) ? $this->config['sendAlertsToTelegram'][2] : ''; if ($bot && $channel) { $handler = new TelegramHandler($bot, $channel, Logger::ALERT); + $handler->setProxy($proxy); $handler->setFormatter(new TelegramFormatter($this->getAlertTitle())); $this->logger->pushHandler($handler); } @@ -387,11 +389,12 @@ protected function getAlertTitle() * * @param $bot * @param $channel + * @param null $proxy * @return $this */ - public function sendAlertsToTelegram($bot, $channel) + public function sendAlertsToTelegram($bot, $channel, $proxy = null) { - $this->config['sendAlertsToTelegram'] = [$bot, $channel]; + $this->config['sendAlertsToTelegram'] = [$bot, $channel, $proxy]; return $this; } diff --git a/src/Telegram/TelegramHandler.php b/src/Telegram/TelegramHandler.php index c639333..c008ae2 100644 --- a/src/Telegram/TelegramHandler.php +++ b/src/Telegram/TelegramHandler.php @@ -6,6 +6,7 @@ use Monolog\Handler\MissingExtensionException; use Monolog\Handler\Curl; use Monolog\Logger; +use RuntimeException; class TelegramHandler extends AbstractProcessingHandler { @@ -18,6 +19,11 @@ class TelegramHandler extends AbstractProcessingHandler * @var int|string */ private $chatId; + + /** + * @var string|null + */ + private $proxy; /** * @param string $token Telegram API token @@ -64,6 +70,15 @@ protected function buildContent(array $record) return json_encode($content); } + /** + * @param $proxy + * @return mixed + */ + public function setProxy($proxy) + { + return $this->proxy = $proxy; + } + /** * {@inheritdoc} * @@ -72,17 +87,22 @@ protected function buildContent(array $record) protected function write(array $record) { $content = $this->buildContent($record); + $host = $this->proxy ? $this->proxy : 'https://api.telegram.org'; $ch = curl_init(); $headers = ['Content-Type: application/json']; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - curl_setopt($ch, CURLOPT_URL, sprintf('https://api.telegram.org/bot%s/sendMessage', $this->token)); + curl_setopt($ch, CURLOPT_URL, sprintf('%s/bot%s/sendMessage', $host, $this->token)); curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10000); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $content); - Curl\Util::execute($ch); + try { + Curl\Util::execute($ch); + } catch (RuntimeException $e) { + AddMessage2Log($e->getMessage(), "bitrix-sync"); + } } }