Commit f0dbdae20db636f846421024fca6fbda314f8a17

Authored by Hayk Nazaryan
1 parent 1455f7d700
Exists in master

fix vacancy text to send in tg

Showing 1 changed file with 1 additions and 2 deletions Inline Diff

app/Components/Integrations/Telegram/VacancyChannel.php
1 <?php 1 <?php
2 2
3 namespace App\Components\Integrations\Telegram; 3 namespace App\Components\Integrations\Telegram;
4 4
5 use App\Models\Ad_employer; 5 use App\Models\Ad_employer;
6 use Str; 6 use Str;
7 use Telegram\Bot\Api; 7 use Telegram\Bot\Api;
8 use Telegram\Bot\Exceptions\TelegramSDKException; 8 use Telegram\Bot\Exceptions\TelegramSDKException;
9 use Telegram\Bot\Laravel\Facades\Telegram; 9 use Telegram\Bot\Laravel\Facades\Telegram;
10 use Telegram\Bot\Objects\Update; 10 use Telegram\Bot\Objects\Update;
11 11
12 class VacancyChannel 12 class VacancyChannel
13 { 13 {
14 const REKAMORE_CHANNEL_ID = 'REKAMORE_CHANNEL_ID'; 14 const REKAMORE_CHANNEL_ID = 'REKAMORE_CHANNEL_ID';
15 15
16 private Api $botApi; 16 private Api $botApi;
17 17
18 public function __construct() 18 public function __construct()
19 { 19 {
20 $this->botApi = Telegram::bot('channel_bot'); 20 $this->botApi = Telegram::bot('channel_bot');
21 } 21 }
22 22
23 /** 23 /**
24 * @throws TelegramSDKException 24 * @throws TelegramSDKException
25 */ 25 */
26 private function getChatId() 26 private function getChatId()
27 { 27 {
28 $updates = $this->botApi->getUpdates(); 28 $updates = $this->botApi->getUpdates();
29 /** @var Update $update*/ 29 /** @var Update $update*/
30 $chatId = null; 30 $chatId = null;
31 foreach ($updates as $update) { 31 foreach ($updates as $update) {
32 if ($update->message?->chat->title === config('services.telegram.chat_title')) { 32 if ($update->message?->chat->title === config('services.telegram.chat_title')) {
33 $chatId = $update->message->chat->id; 33 $chatId = $update->message->chat->id;
34 \Cache::set(self::REKAMORE_CHANNEL_ID, $chatId, null); 34 \Cache::set(self::REKAMORE_CHANNEL_ID, $chatId, null);
35 break; 35 break;
36 } 36 }
37 } 37 }
38 38
39 if ($chatId === null) { 39 if ($chatId === null) {
40 $chatId = \Cache::get(self::REKAMORE_CHANNEL_ID); 40 $chatId = \Cache::get(self::REKAMORE_CHANNEL_ID);
41 if ($chatId === null) { 41 if ($chatId === null) {
42 throw new TelegramSDKException('Cant locate group chat id'); 42 throw new TelegramSDKException('Cant locate group chat id');
43 } 43 }
44 } 44 }
45 45
46 return $chatId; 46 return $chatId;
47 } 47 }
48 48
49 /** 49 /**
50 * @throws TelegramSDKException 50 * @throws TelegramSDKException
51 */ 51 */
52 public function sendVacancy(Ad_employer $vacancy): void 52 public function sendVacancy(Ad_employer $vacancy): void
53 { 53 {
54 $chatId = $this->getChatId(); 54 $chatId = $this->getChatId();
55 55
56 $text = $this->formatTextFotTelegram($vacancy->text); 56 $text = $this->formatTextFotTelegram($vacancy->text);
57 57
58 $this->botApi->sendMessage([ 58 $this->botApi->sendMessage([
59 'chat_id' => $chatId, 59 'chat_id' => $chatId,
60 'text' => $text, 60 'text' => $text,
61 'disable_web_page_preview' => true, 61 'disable_web_page_preview' => true,
62 'parse_mode' => 'HTML' 62 'parse_mode' => 'HTML'
63 ]); 63 ]);
64 } 64 }
65 65
66 public function formatTextFotTelegram(string $text): string 66 public function formatTextFotTelegram(string $text): string
67 { 67 {
68 $text = str_ireplace('<p>', '', $text); 68 $text = str_ireplace('<p>', '', $text);
69 $text = str_ireplace('</p>', '', $text); 69 $text = str_ireplace('</p>', '', $text);
70 70
71 $text = str_ireplace('<br>', '', $text); 71 $text = str_ireplace('<br>', '', $text);
72 $text = str_ireplace('<br />', '', $text); 72 $text = str_ireplace('<br />', '', $text);
73 73
74 $text = str_ireplace('<h3>', '<b>', $text); 74 $text = str_ireplace('<h3>', '<b>', $text);
75 $text = str_ireplace('</h3>', '</b>', $text); 75 $text = str_ireplace('</h3>', '</b>', $text);
76 76
77 $text = str_ireplace('<strong>', '<b>', $text); 77 $text = str_ireplace('<strong>', '<b>', $text);
78 $text = str_ireplace('</strong>', '</b>', $text); 78 $text = str_ireplace('</strong>', '</b>', $text);
79 79
80 $text = str_ireplace('<em>', '<i>', $text); 80 $text = str_ireplace('<em>', '<i>', $text);
81 $text = str_ireplace('</em>', '</i>', $text); 81 $text = str_ireplace('</em>', '</i>', $text);
82 82
83 83 return html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
84 return $text;
85 } 84 }
86 } 85 }
87 86