<?php
namespace Social\InternalBundle\Service\Core;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class CustomTelegramIntegration
*
* @package Social\InternalBundle\Service\Core
*/
class CustomTelegramIntegration
{
const CHANNEL_SALES = 'SALES';
const CHANNEL_DEVS = 'DEVS';
const CHANNEL_ALL = 'ALL';
const CHANNEL_DEPLOYER = 'DEPLOYER';
protected $url = 'https://api.telegram.org/bot{{bot_token}}/sendMessage';
protected $chatId = '';
protected $deployerChatId = '';
protected $salesChatId = '';
protected $botToken = '';
protected $deployerBotToken = '';
protected $siteTitle;
protected $host;
protected $environment;
protected $channels;
protected $logger;
const STATUS_OK = 1;
const STATUS_VERY_BAD = 2;
const STATUS_NOT_OK = 3;
const STATUS_TO_BE_CHECKED = 4;
const STATUS_ICON_NOT_OK = " \xE2\x9D\x97 ";
const STATUS_ICON_VERY_BAD = " \xE2\x9D\x97 \xE2\x9D\x8C \xE2\x9D\x8C \xE2\x9D\x8C \xE2\x9D\x8C ";
const STATUS_ICON_OK = " \xE2\x9C\x85 ";
const STATUS_ICON_TO_BE_CHECKED = " \xE2\x9D\x93 ";
protected $statuses = [
self::STATUS_VERY_BAD => " \xE2\x9D\x97 \xE2\x9D\x8C \xE2\x9D\x8C \xE2\x9D\x8C \xE2\x9D\x8C ",
self::STATUS_NOT_OK => " \xE2\x9D\x97 ",
self::STATUS_OK => " \xE2\x9C\x85 ",
self::STATUS_TO_BE_CHECKED => " \xE2\x9D\x93 ",
];
public function __construct(ContainerInterface $container)
{
$this->botToken = $container->getParameter('telegram_bot_id');
$this->deployerBotToken = $container->getParameter('telegram_deployer_bot_id');
$this->salesChatId = $container->getParameter('telegram_sales_channel');
$this->chatId = $container->getParameter('telegram_dev_channel');
$this->deployerChatId = $container->getParameter('telegram_deployer_channel');
$this->channels = [
self::CHANNEL_DEVS => $this->chatId,
self::CHANNEL_SALES => $this->salesChatId,
self::CHANNEL_DEPLOYER => $this->deployerChatId,
];
try {
$this->siteTitle = $container->get('social.theme_manager')->getSiteTitle();
} catch (\Exception $exception) {
$this->siteTitle = 'CLI';
}
$this->host = $container->get('router')->getContext()->getHost();
$this->logger = $container->get('sentry.client');
}
/**
* @param string $message - Message
* @param array $vars - Data to display
* @param string $operator - Who operated
* @param string $channel - On what channel
* @param int $status - Message status
*
* @return bool
*/
public function sendMessage(
string $message,
array $vars = [],
string $operator = '',
string $channel = self::CHANNEL_SALES,
int $status = 1
): bool {
try {
$callingUrl = str_replace('{{bot_token}}', $this->botToken, $this->url);
if ($channel == self::CHANNEL_DEPLOYER) {
$callingUrl = str_replace('{{bot_token}}', $this->deployerBotToken, $this->url);
}
$finalMessage = $this->statuses[$status];
$finalMessage .= ' Site[' . $this->siteTitle . ']';
$finalMessage .= '::Url[' . $this->host . ']';
if ($operator != '::' && $operator != null) {
$finalMessage .= '[' . $operator . ']';
}
$finalMessage .= PHP_EOL . PHP_EOL;
$finalMessage .= $message;
$finalMessage .= PHP_EOL . PHP_EOL;
foreach ($vars as $key => $value) {
$finalMessage .= $key . ": " . $value . PHP_EOL;
}
$now = new \DateTime('now', new \DateTimeZone('Europe/Bucharest'));
$finalMessage .= 'Date: ' . $now->format('d-m-Y H:i:s');
$data = [
'chat_id' => null,
'text' => $finalMessage,
];
if ($channel == self::CHANNEL_ALL) {
foreach ($this->channels as $key => $value) {
$data['chat_id'] = $value;
$this->sendMessageToBot($callingUrl, $data);
}
} else {
$data['chat_id'] = $this->channels[$channel];
$this->sendMessageToBot($callingUrl, $data);
}
return false;
} catch (\Exception $exception) {
$this->logger->captureException($exception);
return false;
}
}
private function sendMessageToBot($callingUrl, $data)
{
try {
ob_start();
$handler = curl_init();
curl_setopt($handler, CURLOPT_URL, $callingUrl);
curl_setopt($handler, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($handler, CURLOPT_POST, true);
curl_setopt($handler, CURLOPT_HEADER, false);
curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($handler);
curl_close($handler);
ob_end_clean();
} catch (\Exception $exception) {
$this->logger->captureException($exception);
throw new \Exception($exception);
}
}
}