]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[BC-278] Отправка сообщения в Телеграм
authorAleksey Filippov <Aleksey.Filippov@erp-flowers.ru>
Fri, 4 Jul 2025 16:15:39 +0000 (19:15 +0300)
committerAleksey Filippov <Aleksey.Filippov@erp-flowers.ru>
Fri, 4 Jul 2025 16:15:39 +0000 (19:15 +0300)
erp24/actions/infoTable/TestAction.php
erp24/services/TelegramService.php

index f0c5d1a55e97fcb0aa3a7ac8e8cb89fcfc8c05f4..ba98c1bfb8aed86f1eccd568349588d6b4368e0f 100644 (file)
@@ -49,7 +49,7 @@ class TestAction extends Action
         ]; //Алексей
 
         foreach ($chats as $chatId) {
-            TelegramService::sendPromoMessageToTelegramDocument($chatId);
+            TelegramService::sendPromo2MessageToTelegramDocument($chatId);
         }
         return 'ok';
     }
index 8289a8fd3d96f9236ce77f09aff2320c86cac665..4fcb9e7a5384afc099fd718d7d21ce5e7bd8b13e 100644 (file)
@@ -189,6 +189,103 @@ class TelegramService
 
         return $result;
     }
+    public static function sendPromo2MessageToTelegramDocument($chatId)
+    {
+        if (self::isDevEnv()) {
+            $botToken = self::TELEGRAM_BOT_DEV;
+        } else {
+            $botToken = self::TELEGRAM_BOT_PROD;
+        }
+        $apiURL1 = "https://api.telegram.org/bot{$botToken}/sendMediaGroup";
+        $apiURL2 = "https://api.telegram.org/bot{$botToken}/sendMessage";
+
+        $text = 'АКЦИЯ: –20% НА БУКЕТЫ ДНЯ — с 4 по 11 июля 🔥
+        
+Только 7 дней: Скидка 20% на три стильных букета из нашей летней коллекции!';
+
+        $pic1Path = curl_file_create(
+        __DIR__ . '/../web/images/pic_1.jpg',
+        'image/jpg',
+        'pic_1.jpg'
+        );
+
+        $pic2Path = curl_file_create(
+        __DIR__ . '/../web/images/pic_2.jpg',
+        'image/jpg',
+        'pic_2.jpg'
+        );
+
+        $pic3Path = curl_file_create(
+        __DIR__ . '/../web/images/pic_3.jpg',
+        'image/jpg',
+        'pic_3.jpg'
+        );
+
+        $result = 'false';
+
+        if (!empty($chatId)) {
+            try {
+                $buttons = self::getTgShortButtons($chatId);
+
+                $client = new Client();
+                try {
+                    $response1 = $client->post($apiURL1, [
+                        'json' => [
+                            'chat_id' => $chatId,
+                            'media' => json_encode([
+                                ['type' => 'photo', 'media' => 'attach://pic_1.jpg', "caption" => $text],
+                                ['type' => 'photo', 'media' => 'attach://pic_2.jpg' ],
+                                ['type' => 'photo', 'media' => 'attach://pic_3.jpg' ],
+                            ]),
+                            'pic_1.jpg' => $pic1Path,
+                            'pic_2.jpg' => $pic2Path,
+                            'pic_3.jpg' => $pic3Path,
+                        ],
+                    ]);
+
+                    if ($response1->getStatusCode() !== 200) {
+                        Yii::error("Ошибка при отправке сообщения в Telegram. Код ответа: "
+                            . $response1->getStatusCode(), 'telegram');
+                        return $response1->getStatusCode();
+                    }
+                } catch (\Exception $e) {
+                    Yii::error("Ошибка отправки сообщения в Telegram: " . $e->getMessage(), 'telegram');
+                    return $e->getMessage();
+                }
+
+                try {
+                    $response = $client->post($apiURL2, [
+                        'json' => [
+                            'chat_id' => $chatId,
+                            "text" => "Выберите действие:",
+                            'parse_mode' => 'MarkdownV2',
+                            'reply_markup' => Json::encode([
+                                "inline_keyboard" => $buttons
+                            ]),
+                        ],
+                    ]);
+
+                    if ($response->getStatusCode() !== 200) {
+                        Yii::error("Ошибка при отправке сообщения в Telegram. Код ответа: "
+                            . $response->getStatusCode(), 'telegram');
+                        return $response->getStatusCode();
+                    }
+
+                    return $response->getReasonPhrase();
+                } catch (\Exception $e) {
+                    Yii::error("Ошибка отправки сообщения в Telegram: " . $e->getMessage(), 'telegram');
+                    return $e->getMessage();
+                }
+
+            } catch (\Exception $e) {
+
+                Yii::error("Ошибка отправки сообщения в Telegram: " . $e->getMessage(), 'telegram');
+            }
+            $result = 'OK';
+        }
+
+        return $result;
+    }
 
     public static function sendMessageToTelegramClient($chatId, $message)
     {