From: Aleksey Filippov Date: Fri, 4 Jul 2025 16:15:39 +0000 (+0300) Subject: [BC-278] Отправка сообщения в Телеграм X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=26486fc8cde7ffeffa452fbdc8f7a9e7626997bc;p=erp24_rep%2Fyii-erp24%2F.git [BC-278] Отправка сообщения в Телеграм --- diff --git a/erp24/actions/infoTable/TestAction.php b/erp24/actions/infoTable/TestAction.php index f0c5d1a5..ba98c1bf 100644 --- a/erp24/actions/infoTable/TestAction.php +++ b/erp24/actions/infoTable/TestAction.php @@ -49,7 +49,7 @@ class TestAction extends Action ]; //Алексей foreach ($chats as $chatId) { - TelegramService::sendPromoMessageToTelegramDocument($chatId); + TelegramService::sendPromo2MessageToTelegramDocument($chatId); } return 'ok'; } diff --git a/erp24/services/TelegramService.php b/erp24/services/TelegramService.php index 8289a8fd..4fcb9e7a 100644 --- a/erp24/services/TelegramService.php +++ b/erp24/services/TelegramService.php @@ -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) {