]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
[ERP-416] Актуализировать кнопки в сообщении ТГ по когортам feature_filippov_ERP-416_tg_bottons origin/feature_filippov_ERP-416_tg_bottons
authorAleksey Filippov <Aleksey.Filippov@erp-flowers.ru>
Mon, 12 May 2025 08:50:05 +0000 (11:50 +0300)
committerAleksey Filippov <Aleksey.Filippov@erp-flowers.ru>
Mon, 12 May 2025 08:50:05 +0000 (11:50 +0300)
erp24/actions/infoTable/TestAction.php
erp24/services/TelegramService.php

index 8c4568b8eec11f0a392e0c32d2d0b8f9deebd906..c662008b72fabdb17f38a7059624b6741fa6364f 100644 (file)
@@ -30,41 +30,19 @@ use yii_app\services\DashboardService;
 use yii_app\services\ExportImportService;
 use yii_app\services\SalesService;
 use yii_app\services\StoreVisitorsService;
+use yii_app\services\TelegramService;
 
 class TestAction extends Action
 {
     public function run() {
-        $sales = Sales::find()->where(['operation' => Sales::OPERATION_SALE])
-            ->andWhere(['>=', 'date', '2024-05-30 21:28:00'])
-            ->andWhere(['<=', 'date', '2024-06-04 17:24:00'])->all();
-        $salesIds = ArrayHelper::getColumn($sales, 'id');
-        $returns = Sales::find()->select(['id', 'sales_check'])->where(['operation' => Sales::OPERATION_RETURN, 'sales_check' => $salesIds])->all();
-        $returnsIds = ArrayHelper::getColumn($returns, 'sales_check');
-        foreach ($sales as $sale) {
-            /** @var $sale Sales */
-            if (!in_array($sale->id, $returnsIds)) {
-                if (ClientHelper::phoneVerify($sale->phone)) {
-                    $usersBonusMinus = UsersBonus::find()->where(['tip' => 'minus', 'check_id' => $sale->id])->one();
-                    if ($usersBonusMinus) {
-                        $usersBonusPlus = UsersBonus::find()->where(['tip' => 'plus', 'check_id' => $sale->id])->one();
-                        if (!$usersBonusPlus) {
-                            $userBonus = new UsersBonus;
-                            $userBonus->phone = "" . $sale->phone;
-                            $userBonus->name = "Возврат с покупки 10% " . $sale->number . " сумма чека " . $sale->summ;
-                            $userBonus->date = date('Y-m-d H:i:s');
-                            $userBonus->site_id = 1;
-                            $userBonus->setka_id = 1;
-                            $userBonus->tip = 'plus';
-                            $userBonus->tip_sale = 'sale';
-                            $userBonus->bonus = floor($sale->summ * 0.1);
-                            $userBonus->date_start = $userBonus->date;
-                            $userBonus->date_end = date('Y-m-d H:i:s', strtotime('+366 day', strtotime($userBonus->date)));
-                            $userBonus->save();
-                        }
-                    }
-                }
-            }
-        }
+
+        $messageBonuses = "⚠️ Внимание! Тестовая рассылка для проверки кнопок!".
+
+                "\n\n" .
+            "📢 Проверьте данные!";
+
+        TelegramService::sendTargetStatToTelegramMessage($messageBonuses);
+
         return 'ok';
     }
 }
\ No newline at end of file
index aa08060cc99e6e5c019c680783301d5f037c529c..9e3a02d6c04281a9259ddec7d8c30e7fdadccd2a 100644 (file)
@@ -16,6 +16,7 @@ class TelegramService
     const TARGET_PROD_URL = "erp.erp-flowers.ru";
     const CHAT_CHANNEL_ID = "-1001861631125";
     const CHAT_CHANNEL_ERP_ID = "-1002338329426";
+    const CHATBOT_SALT = "ASIUdgb762gIUOYASgd7a6iuydb";
 
     public static function sendMessage($admin_id, $message, $reply_markup = null)
     {
@@ -83,11 +84,15 @@ class TelegramService
         $client = new Client();
         foreach ($chats as $chatId) {
             try {
+                $buttons = self::getTgButtons($chatId);
                 $client->post($apiURL, [
                     'json' => [
                         'chat_id' => $chatId,
                         'text' => $message,
                         'parse_mode' => 'MarkdownV2',
+                        'reply_markup' => Json::encode([
+                            "inline_keyboard" => $buttons
+                        ]),
 
                     ],
                 ]);
@@ -110,6 +115,7 @@ class TelegramService
         $apiURL = "https://api.telegram.org/bot{$botToken}/sendMessage";
 
         $message = self::escapeMarkdown($message);
+        $buttons = self::getTgButtons($chatId);
 
         $client = new Client();
         try {
@@ -119,16 +125,7 @@ class TelegramService
                     'text' => $message,
                     'parse_mode' => 'MarkdownV2',
                     'reply_markup' => Json::encode([
-                        "inline_keyboard" => [
-                            [
-                                ['text' => "Адреса магазинов", "web_app" => ['url' => 'https://chatbot.bazacvetov24.ru/bot/telegram?hash=$hash&start=store']],
-                                ['text' => "Заказ на сайте", "url" => "https://bazacvetov24.ru/"],
-                            ],
-                            [
-                                ['text' => "Забрать 1800 руб", "web_app" => ['url' => 'https://chatbot.bazacvetov24.ru/bot/telegram?hash=$hash&start=promo.1000']],
-                                ['text' => 'Списание бонусов', "web_app" => ['url' => 'https://chatbot.bazacvetov24.ru/bot/telegram?hash=$hash']]
-                            ]
-                        ]
+                        "inline_keyboard" => $buttons
                     ]),
                 ],
             ]);
@@ -146,6 +143,42 @@ class TelegramService
         }
     }
 
+    public static function getHashTG($chatId) : string
+    {
+        // Так можно создать hash для бота
+        $data = Json::encode([
+            'platform' => 'telegram',
+            'user_id' => $chatId
+        ]);
+
+        $sha1 = sha1(self::CHATBOT_SALT . ";" . $data);
+
+        return base64_encode($sha1 . "#" . $data);
+    }
+
+    public static function getTgButtons($chatId) : array
+    {
+        $hash = self::getHashTG($chatId);
+
+        $siteBc24Url = "https://bazacvetov24.ru/";
+        $chatBotBc24Url = 'https://chatbot.bazacvetov24.ru';
+
+        $linkAppStore = $chatBotBc24Url . "/bot/telegram?hash=$hash&start=store";
+        $linkAppEvents = $chatBotBc24Url . "/bot/telegram?hash=$hash&start=promo.1000";
+        $linkAppBalance = $chatBotBc24Url . "/bot/telegram?hash=$hash";
+
+        return [
+            [
+                ['text' => "Адреса магазинов", "web_app" => ['url' => $linkAppStore]],
+                ['text' => "Заказ на сайте", "url" => $siteBc24Url],
+            ],
+            [
+                ['text' => "Забрать 1800 руб", "web_app" => ['url' => $linkAppEvents]],
+                ['text' => 'Списание бонусов', "web_app" => ['url' => $linkAppBalance]]
+            ]
+        ];
+    }
+
 
     // Метод для экранирования символов MarkdownV2 для файла
     public static function escapeMarkdown($text)