]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Отправка сообщения в бот при начислении бонусов
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 10 Feb 2025 09:00:03 +0000 (12:00 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 10 Feb 2025 09:00:03 +0000 (12:00 +0300)
erp24/commands/CronController.php
erp24/services/TelegramService.php

index 6c8c64848b1776f248017417fc46e768a5d68b76..b1e21839447d4400192761c109bfff7aef0cf64e 100644 (file)
@@ -27,6 +27,7 @@ use yii_app\records\UsersBonusLevels;
 use yii_app\records\UsersMessageManagement;
 use yii_app\records\UsersTelegram;
 use yii_app\records\UsersTelegramLog;
+use yii_app\services\TelegramService;
 
 class CronController extends Controller
 {
@@ -427,6 +428,7 @@ class CronController extends Controller
                 ->andWhere(['date_start' => $kogortDate])
                 ->column();
             $testPhones = array_map('trim', explode(',', $messagesSettings->test_phones_list));
+            $countBonuses = 0;
             foreach ($kogortPhones as $key => $phone) {
                 if (in_array($phone, $testPhones)) {
                     continue;
@@ -450,9 +452,19 @@ class CronController extends Controller
                         );
                     } else {
                         Users::updateAll(['burn_balans' => $bonus], ['phone' => '' . $phone]);
+                        $countBonuses += 1;
                     }
                 }
             }
+            if ($countBonuses !== $countPhones) {
+                $messageBonuses = "⚠️ Внимание!
+                Количество начисленных бонусов отличается от количества телефонов в когорте.\n\n" .
+                    "📅 Для когорты **{$kogortDate}** — всего **{$countPhones}** номеров.\n" .
+                    "🎯 На целевую дату **{$targetDate}** бонусы начислены только для **{$countBonuses}** номеров.\n\n" .
+                    "📢 Проверьте данные!";
+
+                TelegramService::sendTargetStatToTelegramMessage($messageBonuses);
+            }
         } else {
             $this->stdout(
                 "Нет данных для отправки сообщений для когорты для целевой даты {$targetDate}.\n",
index 7db12d6a40168a396735f8afb9f6c32b5390a12f..7e81bd1a8c2e53725048f474937830ad83ac376d 100644 (file)
@@ -74,6 +74,29 @@ class TelegramService
         }
     }
 
+    public static function sendTargetStatToTelegramMessage($message)
+    {
+        $botToken = self::TELEGRAM_BOT_DEV;
+        $apiURL = "https://api.telegram.org/bot{$botToken}/sendMessage";
+        $chats = ['337084327', '730432579']; //Алексей и Владимир
+        $message = self::escapeMarkdown($message);
+        $client = new Client();
+        foreach ($chats as $chatId) {
+            try {
+                $client->post($apiURL, [
+                    'json' => [
+                        'chat_id' => $chatId,
+                        'text' => $message,
+                        'parse_mode' => 'MarkdownV2',
+
+                    ],
+                ]);
+            } catch (\Exception $e) {
+                Yii::error("Ошибка отправки сообщения в Telegram: " . $e->getMessage(), 'telegram');
+            }
+        }
+    }
+
     public static function sendMessageToTelegramClient($chatId, $message)
     {
         if (self::isDevEnv()) {