src/Social/InternalBundle/Service/Core/CustomTelegramIntegration.php line 67

Open in your IDE?
  1. <?php
  2. namespace Social\InternalBundle\Service\Core;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. /**
  5.  * Class CustomTelegramIntegration
  6.  *
  7.  * @package Social\InternalBundle\Service\Core
  8.  */
  9. class CustomTelegramIntegration
  10. {
  11.     const CHANNEL_SALES    'SALES';
  12.     const CHANNEL_DEVS     'DEVS';
  13.     const CHANNEL_ALL      'ALL';
  14.     const CHANNEL_DEPLOYER 'DEPLOYER';
  15.     protected $url 'https://api.telegram.org/bot{{bot_token}}/sendMessage';
  16.     protected $chatId '';
  17.     protected $deployerChatId '';
  18.     protected $salesChatId '';
  19.     protected $botToken '';
  20.     protected $deployerBotToken '';
  21.     protected $siteTitle;
  22.     protected $host;
  23.     protected $environment;
  24.     protected $channels;
  25.     protected $logger;
  26.     const STATUS_OK            1;
  27.     const STATUS_VERY_BAD      2;
  28.     const STATUS_NOT_OK        3;
  29.     const STATUS_TO_BE_CHECKED 4;
  30.     const STATUS_ICON_NOT_OK        " \xE2\x9D\x97 ";
  31.     const STATUS_ICON_VERY_BAD      " \xE2\x9D\x97 \xE2\x9D\x8C \xE2\x9D\x8C \xE2\x9D\x8C \xE2\x9D\x8C ";
  32.     const STATUS_ICON_OK            " \xE2\x9C\x85 ";
  33.     const STATUS_ICON_TO_BE_CHECKED " \xE2\x9D\x93 ";
  34.     protected $statuses = [
  35.         self::STATUS_VERY_BAD      => " \xE2\x9D\x97 \xE2\x9D\x8C \xE2\x9D\x8C \xE2\x9D\x8C \xE2\x9D\x8C ",
  36.         self::STATUS_NOT_OK        => " \xE2\x9D\x97 ",
  37.         self::STATUS_OK            => " \xE2\x9C\x85 ",
  38.         self::STATUS_TO_BE_CHECKED => " \xE2\x9D\x93 ",
  39.     ];
  40.     public function __construct(ContainerInterface $container)
  41.     {
  42.         $this->botToken         $container->getParameter('telegram_bot_id');
  43.         $this->deployerBotToken $container->getParameter('telegram_deployer_bot_id');
  44.         $this->salesChatId      $container->getParameter('telegram_sales_channel');
  45.         $this->chatId           $container->getParameter('telegram_dev_channel');
  46.         $this->deployerChatId   $container->getParameter('telegram_deployer_channel');
  47.         $this->channels = [
  48.             self::CHANNEL_DEVS     => $this->chatId,
  49.             self::CHANNEL_SALES    => $this->salesChatId,
  50.             self::CHANNEL_DEPLOYER => $this->deployerChatId,
  51.         ];
  52.         try {
  53.             $this->siteTitle $container->get('social.theme_manager')->getSiteTitle();
  54.         } catch (\Exception $exception) {
  55.             $this->siteTitle 'CLI';
  56.         }
  57.         $this->host   $container->get('router')->getContext()->getHost();
  58.         $this->logger $container->get('sentry.client');
  59.     }
  60.     /**
  61.      * @param string $message  - Message
  62.      * @param array  $vars     - Data to display
  63.      * @param string $operator - Who operated
  64.      * @param string $channel  - On what channel
  65.      * @param int    $status   - Message status
  66.      *
  67.      * @return bool
  68.      */
  69.     public function sendMessage(
  70.         string $message,
  71.         array $vars = [],
  72.         string $operator '',
  73.         string $channel self::CHANNEL_SALES,
  74.         int $status 1
  75.     ): bool {
  76.         try {
  77.             $callingUrl str_replace('{{bot_token}}'$this->botToken$this->url);
  78.             if ($channel == self::CHANNEL_DEPLOYER) {
  79.                 $callingUrl str_replace('{{bot_token}}'$this->deployerBotToken$this->url);
  80.             }
  81.             $finalMessage $this->statuses[$status];
  82.             $finalMessage .= ' Site[' $this->siteTitle ']';
  83.             $finalMessage .= '::Url[' $this->host ']';
  84.             if ($operator != '::' && $operator != null) {
  85.                 $finalMessage .= '[' $operator ']';
  86.             }
  87.             $finalMessage .= PHP_EOL PHP_EOL;
  88.             $finalMessage .= $message;
  89.             $finalMessage .= PHP_EOL PHP_EOL;
  90.             foreach ($vars as $key => $value) {
  91.                 $finalMessage .= $key ": " $value PHP_EOL;
  92.             }
  93.             $now          = new \DateTime('now', new \DateTimeZone('Europe/Bucharest'));
  94.             $finalMessage .= 'Date: ' $now->format('d-m-Y H:i:s');
  95.             $data = [
  96.                 'chat_id' => null,
  97.                 'text'    => $finalMessage,
  98.             ];
  99.             if ($channel == self::CHANNEL_ALL) {
  100.                 foreach ($this->channels as $key => $value) {
  101.                     $data['chat_id'] = $value;
  102.                     $this->sendMessageToBot($callingUrl$data);
  103.                 }
  104.             } else {
  105.                 $data['chat_id'] = $this->channels[$channel];
  106.                 $this->sendMessageToBot($callingUrl$data);
  107.             }
  108.             return false;
  109.         } catch (\Exception $exception) {
  110.             $this->logger->captureException($exception);
  111.             return false;
  112.         }
  113.     }
  114.     private function sendMessageToBot($callingUrl$data)
  115.     {
  116.         try {
  117.             ob_start();
  118.             $handler curl_init();
  119.             curl_setopt($handlerCURLOPT_URL$callingUrl);
  120.             curl_setopt($handlerCURLOPT_POSTFIELDShttp_build_query($data));
  121.             curl_setopt($handlerCURLOPT_POSTtrue);
  122.             curl_setopt($handlerCURLOPT_HEADERfalse);
  123.             curl_setopt($handlerCURLOPT_SSL_VERIFYPEERfalse);
  124.             $response curl_exec($handler);
  125.             curl_close($handler);
  126.             ob_end_clean();
  127.         } catch (\Exception $exception) {
  128.             $this->logger->captureException($exception);
  129.             throw new \Exception($exception);
  130.         }
  131.     }
  132. }