From 81d1a070b64b6d44c74f3a41286fa5327b5bce8c Mon Sep 17 00:00:00 2001 From: Fasutoa Date: Wed, 18 Jun 2025 21:36:03 +0500 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=20=D0=BF=D0=BE=20=D1=82=D0=B3?= =?UTF-8?q?=D1=88=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/.DS_Store | Bin 8196 -> 8196 bytes app/Jobs/CreateTgPostJob.php | 165 +++++++++++++++++++++++++++++++++++ app/Jobs/UpdateTgPost.php | 51 +++++++++++ app/Services/.DS_Store | Bin 0 -> 6148 bytes 4 files changed, 216 insertions(+) create mode 100644 app/Jobs/CreateTgPostJob.php create mode 100644 app/Jobs/UpdateTgPost.php create mode 100644 app/Services/.DS_Store diff --git a/app/.DS_Store b/app/.DS_Store index 26feb45eef6646e752d47a6cd0b02f1b24fd8d10..852a6e2efa3bbdecdbea26c8f1e0d72ff70d3a54 100644 GIT binary patch delta 14 VcmZp1XmQvOAk4_PIZ${r4*(=N1atrZ delta 14 VcmZp1XmQvOAk4_HIZ${r4*(=H1aklY diff --git a/app/Jobs/CreateTgPostJob.php b/app/Jobs/CreateTgPostJob.php new file mode 100644 index 0000000..25fea7d --- /dev/null +++ b/app/Jobs/CreateTgPostJob.php @@ -0,0 +1,165 @@ +post_id = $post_id; + $this->title = $title; + $this->message = $message; + $this->images = $images; + $this->videos = $videos; + $this->documents = $documents; + $this->publish_date = $publish_date; + } + + public function handle() + { + $chat = TelegraphChat::find(4); + $baseUrl = config('app.url'); + $messageIds = []; + $captionSent = false; + + // Отправка медиагруппы, если есть изображения или видео + if (!empty($this->images) || !empty($this->videos)) { + $mediaGroup = $this->createMediaGroup($this->message, $this->images, $this->videos); + $response = $chat->mediaGroup($mediaGroup)->send(); + + if ($response->successful()) { + $messages = $response->json()['result']; + $messageIds = array_merge($messageIds, array_map(function($msg) { + return $msg['message_id']; + }, $messages)); + Log::info('Media group sent: ' . json_encode($messageIds)); + $captionSent = true; // Подпись отправлена с медиагруппой + } else { + Log::error('Failed to send media group: ' . $response->body()); + } + } + + // Отправка документов, если они есть + if (!empty($this->documents)) { + foreach ($this->documents as $index => $document) { + $fullDocument = $baseUrl . $document; + if ($index == 0 && !$captionSent) { + // Отправка первого документа с подписью, если она еще не отправлена + $response = $chat->document($fullDocument)->markdown($this->message)->send(); + $captionSent = true; + } else { + // Отправка документа без подписи + $response = $chat->document($fullDocument)->send(); + } + + if ($response->successful()) { + $messageId = $response->telegraphMessageId(); + $messageIds[] = $messageId; + Log::info('Document sent: ' . $messageId); + } else { + Log::error('Failed to send document: ' . $fullDocument . ' - ' . $response->body()); + } + } + } + + // Отправка текста, если ничего другого не отправлено + if (!$captionSent) { + $response = $chat->markdown($this->message)->send(); + + if ($response->successful()) { + $messageId = $response->telegraphMessageId(); + $messageIds[] = $messageId; + Log::info('Text message sent: ' . $messageId); + } else { + Log::error('Failed to send text message: ' . $response->body()); + } + } + + // Вставка записи в базу данных + if (!empty($messageIds)) { + $tgPostId = $messageIds[0]; + $mediaContent = count($messageIds) > 1 ? json_encode($messageIds) : null; + $this->insertPostRecord($tgPostId, $mediaContent); + } + } + + private function insertPostRecord($tgPostId, $mediaIds) + { + $mediaContent = is_array($mediaIds) ? json_encode($mediaIds) : $mediaIds; + + DB::table('posts_tg_posts')->insert([ + 'post_id' => $this->post_id, + 'tg_post_id' => $tgPostId, + 'media_content' => $mediaContent, + 'unchange_time_after' => Carbon::now()->addWeek(), + ]); + } + + private function createMediaGroup($text, $images, $videos) + { + try { + $baseUrl = config('app.url'); + $mediaItems = []; + + // Добавление изображений + foreach ($images as $image) { + $mediaItems[] = [ + 'type' => 'photo', + 'media' => $baseUrl . $image, + ]; + } + + Log::info('Media items sent: ' . json_encode($mediaItems)); + + // Добавление видео + foreach ($videos as $video) { + $mediaItems[] = [ + 'type' => 'video', + 'media' => $baseUrl . $video, + ]; + } + + // Ограничение до 10 элементов + $mediaItems = array_slice($mediaItems, 0, 10); + + // Установка подписи для первого элемента + if (!empty($mediaItems)) { + $mediaItems[0]['caption'] = $text; + $mediaItems[0]['parse_mode'] = 'Markdown'; + } + + return $mediaItems; + } catch (\Exception $e) { + Log::error('Ошибка при публикации в Telegram: ' . $e->getMessage()); + return []; + } + } +} + diff --git a/app/Jobs/UpdateTgPost.php b/app/Jobs/UpdateTgPost.php new file mode 100644 index 0000000..b58d31f --- /dev/null +++ b/app/Jobs/UpdateTgPost.php @@ -0,0 +1,51 @@ +select()->where('post_id', $this->post_id)->first(); + $tgService = new TgService(); + $tgService->updatePost($postRelation->tg_post_id, $this->text, $this->images, json_decode($postRelation->media_content)); + } catch (\Exception $e) { + Log::error('Ошибка при создании поста: ' . $e->getMessage()); + throw $e; + } + } + +} diff --git a/app/Services/.DS_Store b/app/Services/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..65573c975ea38610b836ae189b211668532b84fe GIT binary patch literal 6148 zcmeH~zY4-Y48{}fAh>jNyu}Cb4Gz)CQE(P@6dc6;9^Eg09IVb#l966r-`1~yL=LjM+|Sq3?RK?7+9YWucrSzNuYDvHpaN8Y3Qz$mFdhZ+ zD4)&8^GJLWDnJEBqJVuL3fx$eE$E*P1Rnvw4rMp2eU<==6~LNoK~!KGtzfjOk0Dm~ zcCh5Wnry*n7tP^A^Ui8h3{0b4v><_Lbudr?DlkxB8hLN${}%pj{vWh3r2