Blame view

app/Components/Integrations/Telegram/VacancyChannel.php 2.51 KB
088323a40   Fedor   task-132687 teleg...
1
2
3
4
5
6
7
8
9
10
11
12
13
  <?php
  
  namespace App\Components\Integrations\Telegram;
  
  use App\Models\Ad_employer;
  use Str;
  use Telegram\Bot\Api;
  use Telegram\Bot\Exceptions\TelegramSDKException;
  use Telegram\Bot\Laravel\Facades\Telegram;
  use Telegram\Bot\Objects\Update;
  
  class VacancyChannel
  {
465aace57   Fedor   task-132687 minor...
14
      const REKAMORE_CHANNEL_ID = 'REKAMORE_CHANNEL_ID';
088323a40   Fedor   task-132687 teleg...
15
16
17
18
19
20
21
22
23
24
      private Api $botApi;
  
      public function __construct()
      {
          $this->botApi = Telegram::bot('channel_bot');
      }
  
      /**
       * @throws TelegramSDKException
       */
465aace57   Fedor   task-132687 minor...
25
      private function getChatId()
088323a40   Fedor   task-132687 teleg...
26
27
28
29
30
      {
          $updates = $this->botApi->getUpdates();
          /** @var Update $update*/
          $chatId = null;
          foreach ($updates as $update) {
465aace57   Fedor   task-132687 minor...
31
32
33
34
              if ($update->message?->chat->title === config('services.telegram.chat_title')) {
                  $chatId = $update->message->chat->id;
                  \Cache::set(self::REKAMORE_CHANNEL_ID, $chatId, null);
                  break;
174021ab5   Hayk Nazaryan   fix telegram
35
36
37
38
              } elseif ($update->channel_post?->chat->title === config('services.telegram.chat_title')) {
                  $chatId = $update->channel_post->chat->id;
                  \Cache::set(self::REKAMORE_CHANNEL_ID, $chatId, null);
                  break;
088323a40   Fedor   task-132687 teleg...
39
40
              }
          }
465aace57   Fedor   task-132687 minor...
41

088323a40   Fedor   task-132687 teleg...
42
          if ($chatId === null) {
465aace57   Fedor   task-132687 minor...
43
44
45
46
              $chatId = \Cache::get(self::REKAMORE_CHANNEL_ID);
              if ($chatId === null) {
                  throw new TelegramSDKException('Cant locate group chat id');
              }
088323a40   Fedor   task-132687 teleg...
47
          }
465aace57   Fedor   task-132687 minor...
48
49
50
51
52
53
54
55
56
          return $chatId;
      }
  
      /**
       * @throws TelegramSDKException
       */
      public function sendVacancy(Ad_employer $vacancy): void
      {
          $chatId = $this->getChatId();
ab181e741   Fedor   task-132985 autol...
57
          $text = $this->formatTextFotTelegram($vacancy->text);
088323a40   Fedor   task-132687 teleg...
58
59
60
61
62
63
64
65
  
          $this->botApi->sendMessage([
              'chat_id' => $chatId,
              'text' => $text,
              'disable_web_page_preview' => true,
              'parse_mode' => 'HTML'
          ]);
      }
ab181e741   Fedor   task-132985 autol...
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  
      public function formatTextFotTelegram(string $text): string
      {
          $text = str_ireplace('<p>', '', $text);
          $text = str_ireplace('</p>', '', $text);
  
          $text = str_ireplace('<br>', '', $text);
          $text = str_ireplace('<br />', '', $text);
  
          $text = str_ireplace('<h3>', '<b>', $text);
          $text = str_ireplace('</h3>', '</b>', $text);
  
          $text = str_ireplace('<strong>', '<b>', $text);
          $text = str_ireplace('</strong>', '</b>', $text);
  
          $text = str_ireplace('<em>', '<i>', $text);
          $text = str_ireplace('</em>', '</i>', $text);
f0dbdae20   Hayk Nazaryan   fix vacancy text ...
83
          return html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
ab181e741   Fedor   task-132985 autol...
84
      }
088323a40   Fedor   task-132687 teleg...
85
  }