vendor/mero/telegram-handler/src/Mero/Monolog/Handler/TelegramHandler.php line 18

Open in your IDE?
  1. <?php
  2. namespace Mero\Monolog\Handler;
  3. use Mero\Monolog\Formatter\HtmlFormatter;
  4. use Monolog\Handler\AbstractProcessingHandler;
  5. use Monolog\Handler\MissingExtensionException;
  6. use Monolog\Handler\Curl;
  7. use Monolog\Logger;
  8. /**
  9.  * Sends notifications through Telegram API.
  10.  *
  11.  * @author Rafael Mello <merorafael@gmail.com>
  12.  *
  13.  * @see    https://core.telegram.org/bots/api
  14.  */
  15. class TelegramHandler extends AbstractProcessingHandler
  16. {
  17.     /**
  18.      * @var string Telegram API token
  19.      */
  20.     private $token;
  21.     /**
  22.      * @var int Chat identifier
  23.      */
  24.     private $chatId;
  25.     /**
  26.      * @var int Request timeout
  27.      */
  28.     private $timeout;
  29.     /**
  30.      * @param string $token  Telegram API token
  31.      * @param int    $chatId Chat identifier
  32.      * @param int    $level  The minimum logging level at which this handler will be triggered
  33.      * @param bool   $bubble Whether the messages that are handled can bubble up the stack or not
  34.      *
  35.      * @throws MissingExtensionException If the PHP cURL extension is not loaded
  36.      */
  37.     public function __construct(
  38.         $token,
  39.         $chatId,
  40.         $level Logger::CRITICAL,
  41.         $bubble true
  42.     ) {
  43.         if (!extension_loaded('curl')) {
  44.             throw new MissingExtensionException('The cURL PHP extension is required to use the TelegramHandler');
  45.         }
  46.         $this->token $token;
  47.         $this->chatId $chatId;
  48.         $this->timeout 0;
  49.         parent::__construct($level$bubble);
  50.     }
  51.     /**
  52.      * Define a timeout to Telegram send message request.
  53.      *
  54.      * @param int $timeout Request timeout
  55.      *
  56.      * @return TelegramHandler
  57.      */
  58.     public function setTimeout($timeout)
  59.     {
  60.         $this->timeout $timeout;
  61.         return $this;
  62.     }
  63.     /**
  64.      * Builds the header of the API Call.
  65.      *
  66.      * @param string $content
  67.      *
  68.      * @return array
  69.      */
  70.     protected function buildHeader($content)
  71.     {
  72.         return [
  73.             'Content-Type: application/json',
  74.             'Content-Length: '.strlen($content),
  75.         ];
  76.     }
  77.     /**
  78.      * Builds the body of API call.
  79.      *
  80.      * @param array $record
  81.      *
  82.      * @return string
  83.      */
  84.     protected function buildContent(array $record)
  85.     {
  86.         $content = [
  87.             'chat_id' => $this->chatId,
  88.             'text' => $record['formatted'],
  89.         ];
  90.         if ($this->formatter instanceof HtmlFormatter) {
  91.             $content['parse_mode'] = 'HTML';
  92.         }
  93.         return json_encode($content);
  94.     }
  95.     /**
  96.      * {@inheritdoc}
  97.      *
  98.      * @param array $record
  99.      */
  100.     protected function write(array $record)
  101.     {
  102.         $content $this->buildContent($record);
  103.         $ch curl_init();
  104.         curl_setopt($chCURLOPT_HTTPHEADER$this->buildHeader($content));
  105.         curl_setopt($chCURLOPT_URLsprintf('https://api.telegram.org/bot%s/sendMessage'$this->token));
  106.         curl_setopt($chCURLOPT_POSTtrue);
  107.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  108.         curl_setopt($chCURLOPT_POSTFIELDS$content);
  109.         curl_setopt($chCURLOPT_TIMEOUT$this->timeout);
  110.         Curl\Util::execute($ch);
  111.     }
  112. }