src/Social/FrontendBundle/Controller/HooksController.php line 24

Open in your IDE?
  1. <?php
  2. namespace Social\FrontendBundle\Controller;
  3. use Psr\Log\LoggerInterface;
  4. use Social\InternalBundle\Entity\TelegramBot;
  5. use Social\InternalBundle\Entity\TelegramBotEvent;
  6. use Social\InternalBundle\Entity\TelegramBotMessage;
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  8. use Symfony\Component\HttpClient\HttpClient;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. class HooksController extends Controller
  12. {
  13.     /**
  14.      * @var LoggerInterface
  15.      */
  16.     private $telegramLogger;
  17.     public function __construct(LoggerInterface $telegramLogger)
  18.     {
  19.         $this->telegramLogger $telegramLogger;
  20.     }
  21.     /**
  22.      * @param Request $request
  23.      *
  24.      * @return JsonResponse
  25.      */
  26.     public function mailReportAction(Request $request)
  27.     {
  28.         try {
  29.             $sparkService $this->get('social.emailfilters.sparkpost');
  30.             
  31.             $response $sparkService->checkAuth($request);
  32.             if (true === $response['error_flag']) {
  33.                 return $response['response'];
  34.             }
  35.             $response $sparkService->captureEvent($request);
  36.             
  37.             return new JsonResponse($response);
  38.         } catch (\Exception $exception) {
  39.             $this->get('sentry.client')->captureException($exception);
  40.         }
  41.     }
  42.     /**
  43.      * @param Request $request
  44.      *
  45.      * @return JsonResponse
  46.      */
  47.     public function telegramEventsAction(Request $request)
  48.     {
  49.         try {
  50.             $em $this->getDoctrine()->getManager();
  51.             $this->telegramLogger->info(json_encode($request->getContent()));
  52.             $data json_decode($request->getContent(), true);
  53.             if (isset($data['message']['chat']['id'])) {
  54.                 $text $data['message']['text'];
  55.                 $chatId $data['message']['chat']['id'];
  56.                 $messageId $data['message']['message_id'];
  57.                 $firstName $data['message']['from']['first_name'];
  58.                 $lastName $data['message']['from']['last_name'];
  59.                 $username = isset($data['message']['from']['username']) ? $data['message']['from']['username'] : null;
  60.                 $time $data['message']['date'];
  61.                 $this->telegramLogger->info('chatId = ' $chatId);
  62.                 /** @var TelegramBot|null $telegramBot */
  63.                 $telegramBot null;
  64.                 if ($text && strpos($text"start")) {
  65.                     $text explode("start "$text);
  66.                     $telegramBot $em->getRepository(TelegramBot::class)->findOneBy(['action' => $text[1], 'active' => true]);
  67.                 }
  68.                 if ($telegramBot) {
  69.                     $this->telegramLogger->info("telegramBot = " $telegramBot->getId());
  70.                     $botAPIToken $telegramBot->getTgBotSetting()->getApiToken();
  71.                     /** @var TelegramBotMessage $botMessage */
  72.                     foreach ($telegramBot->getTgBotMessages() as $botMessage) {
  73.                         $message $botMessage->getMessage();
  74.                         $botMessageMedia $botMessage->getMedia();
  75.                         $client HttpClient::create();
  76.                         if ($botMessageMedia) {
  77.                             $json = [];
  78.                             $json['chat_id'] = $chatId;
  79.                             if ($message) {
  80.                                 $json['caption'] = str_replace(['<p>''</p>''<b>''</b>'], ''$botMessage->getMessage());
  81.                                 $json['parse_mode'] = 'HTML';
  82.                             }
  83.                             if ($botMessage->getButtonText() && $botMessage->getUrl()) {
  84.                                 $json['reply_markup'] = ['inline_keyboard' => [[['text' => $botMessage->getButtonText(), 'url' => $botMessage->getUrl()]]]];
  85.                             }
  86.                             $this->telegramLogger->info('$json = ' json_encode($json));
  87. //                            if ($botMessage->getDelayTime()) {
  88. //                                sleep($botMessage->getDelayTime());
  89. //                            }
  90.                             if ($botMessage->getMimeType() == 'video/mp4') {
  91.                                 $json['video'] = $request->getUriForPath('/media/user_tg/' $botMessage->getMedia());
  92.                                 $response $client->request('POST''https://api.telegram.org/bot' $botAPIToken '/sendVideo', [
  93.                                     'json' => $json
  94.                                 ]);
  95.                                 $data json_decode($response->getContent(), true);
  96.                                 if ($data && $data['ok']) {
  97.                                     $this->addTelegramEvents($chatId$messageId$firstName$lastName$time$botMessage$username);
  98.                                     $this->telegramLogger->info("Video sent successfully.");
  99.                                 } else {
  100.                                     $this->telegramLogger->info("Failed to send video.");
  101.                                 }
  102.                             } elseif ($botMessage->getMimeType() == 'audio/mpeg') {
  103.                                 $json['audio'] = $request->getUriForPath('/media/user_tg/' $botMessage->getMedia());
  104.                                 $response $client->request('POST''https://api.telegram.org/bot' $botAPIToken '/sendAudio', [
  105.                                     'json' => $json
  106.                                 ]);
  107.                                 $data json_decode($response->getContent(), true);
  108.                                 $this->telegramLogger->error('response = ' json_encode($response));
  109.                                 if ($data && $data['ok']) {
  110.                                     $this->addTelegramEvents($chatId$messageId$firstName$lastName$time$botMessage$username);
  111.                                     $this->telegramLogger->info("Audio sent successfully.");
  112.                                 } else {
  113.                                     $this->telegramLogger->info("Failed to send Audio.");
  114.                                 }
  115.                             } else {
  116.                                 $json['photo'] = $request->getUriForPath('/media/user_tg/' $botMessage->getMedia());
  117.                                 $response $client->request('POST''https://api.telegram.org/bot' $botAPIToken '/sendPhoto', [
  118.                                     'json' => $json
  119.                                 ]);
  120.                                 $this->telegramLogger->error('response = ' json_encode($response));
  121.                                 $data json_decode($response->getContent(), true);
  122.                                 if ($data && $data['ok']) {
  123.                                     $this->addTelegramEvents($chatId$messageId$firstName$lastName$time$botMessage$username);
  124.                                     $this->telegramLogger->info("Image sent successfully.");
  125.                                 } else {
  126.                                     $this->telegramLogger->info("Failed to send image.");
  127.                                 }
  128.                             }
  129.                         } elseif ($message) {
  130.                             $this->telegramLogger->info("In message " $message);
  131.                             if ($botMessage->getDelayTime()) {
  132.                                 sleep($botMessage->getDelayTime());
  133.                             }
  134.                             $client HttpClient::create();
  135.                             $response $client->request('POST''https://api.telegram.org/bot' $botAPIToken '/sendMessage', [
  136.                                 'json' => [
  137.                                     'chat_id' => $chatId,
  138.                                     'text' => str_replace(['<p>''</p>'], ''$message),
  139.                                     'parse_mode' => 'HTML'
  140.                                 ],
  141.                             ]);
  142.                             $data json_decode($response->getContent(), true);
  143.                             if ($data && $data['ok']) {
  144.                                 $this->addTelegramEvents($chatId$messageId$firstName$lastName$time$botMessage$username);
  145.                                 $this->telegramLogger->info("Message sent successfully.");
  146.                             } else {
  147.                                 $this->telegramLogger->info("Failed to send message.");
  148.                             }
  149.                         }
  150.                     }
  151.                 }
  152.             }
  153.             return new JsonResponse(['success' => true]);
  154.         } catch (\Exception $exception) {
  155.             $this->get('sentry.client')->captureException($exception);
  156.             exit;
  157.         }
  158.     }
  159.     public function addTelegramEvents($chatId$messageId$firstName$lastName$time$botMessage$username=null)
  160.     {
  161.         try {
  162.             $em $this->getDoctrine()->getManager();
  163.             $telegramBotEvent = new TelegramBotEvent();
  164.             $telegramBotEvent->setChatId($chatId);
  165.             $telegramBotEvent->setMessageId($messageId);
  166.             $telegramBotEvent->setFirstName($firstName);
  167.             $telegramBotEvent->setLastName($lastName);
  168.             $telegramBotEvent->setMessageSentAt(\DateTime::createFromFormat('U'$time));
  169.             $telegramBotEvent->setTelegramBotMessage($botMessage);
  170.             if ($username) {
  171.                 $telegramBotEvent->setUsername($username);
  172.             }
  173.             $em->persist($telegramBotEvent);
  174.             $em->flush();
  175.         } catch (\Exception $exception) {
  176.             $this->get('sentry.client')->captureException($exception);
  177.             $this->telegramLogger->error('error while inserting event ' $exception->getMessage());
  178.         }
  179.     }
  180. }