Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Proxy for telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
arrilot committed Jan 17, 2019
1 parent 4344818 commit 33be77c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down
24 changes: 22 additions & 2 deletions src/Telegram/TelegramHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Monolog\Handler\MissingExtensionException;
use Monolog\Handler\Curl;
use Monolog\Logger;
use RuntimeException;

class TelegramHandler extends AbstractProcessingHandler
{
Expand All @@ -18,6 +19,11 @@ class TelegramHandler extends AbstractProcessingHandler
* @var int|string
*/
private $chatId;

/**
* @var string|null
*/
private $proxy;

/**
* @param string $token Telegram API token
Expand Down Expand Up @@ -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}
*
Expand All @@ -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");
}
}
}

0 comments on commit 33be77c

Please sign in to comment.