From 8069ad7f950cc466bea17b2dd7731dccd733cf65 Mon Sep 17 00:00:00 2001 From: Aleksey Filippov Date: Mon, 12 May 2025 11:50:05 +0300 Subject: [PATCH] =?utf8?q?[ERP-416]=20=D0=90=D0=BA=D1=82=D1=83=D0=B0=D0=BB?= =?utf8?q?=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BA?= =?utf8?q?=D0=BD=D0=BE=D0=BF=D0=BA=D0=B8=20=D0=B2=20=D1=81=D0=BE=D0=BE?= =?utf8?q?=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=A2=D0=93=20=D0=BF?= =?utf8?q?=D0=BE=20=D0=BA=D0=BE=D0=B3=D0=BE=D1=80=D1=82=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/actions/infoTable/TestAction.php | 40 +++++-------------- erp24/services/TelegramService.php | 53 +++++++++++++++++++++----- 2 files changed, 52 insertions(+), 41 deletions(-) diff --git a/erp24/actions/infoTable/TestAction.php b/erp24/actions/infoTable/TestAction.php index 8c4568b8..c662008b 100644 --- a/erp24/actions/infoTable/TestAction.php +++ b/erp24/actions/infoTable/TestAction.php @@ -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 diff --git a/erp24/services/TelegramService.php b/erp24/services/TelegramService.php index aa08060c..9e3a02d6 100644 --- a/erp24/services/TelegramService.php +++ b/erp24/services/TelegramService.php @@ -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) -- 2.39.5