namespace yii_app\commands;
-use app\jobs\SendTelegramMessageDBJob;
+
use app\jobs\SendTelegramMessageJob;
use app\jobs\SendWhatsappMessageJob;
use DateTime;
use yii\console\ExitCode;
use yii\db\Expression;
use yii\helpers\BaseConsole;
-use yii\helpers\Console;
use yii_app\helpers\ClientHelper;
use yii_app\records\BonusLevels;
-use yii_app\records\EqualizationRemains;
-use yii_app\records\Product1cReplacement;
-use yii_app\records\Products1c;
-use yii_app\records\ReplacementInvoice;
-use yii_app\records\ReplacementInvoiceProducts;
use yii_app\records\Sales;
use yii_app\records\SentKogort;
use yii_app\records\Users;
BaseConsole::FG_GREEN
);
- $channel = WhatsAppService::getChannelByName('WABA');
- $limit = $channel['messagingLimit'] ?? 250;
-
+ $limit = $messagesSettings ? $messagesSettings->channel_limit : 250;
+ $cascadeId = $messagesSettings ? $messagesSettings->cascade_id : 5686;
$salesData = Sales::find()
->select([
'phone',
$messageData['kogort_date'] = $kogortDate;
$messageData['target_date'] = $targetDate;
$messageData['message'] = $messageText;
+ $messageData['cascade_id'] = $cascadeId;
Yii::$app->queue->push(new SendWhatsappMessageJob([
'messageData' => $messageData,
use yii_app\records\UsersMessageManagementLogs;
use yii_app\records\UsersTelegram;
use yii_app\records\UsersTelegramLog;
+use yii_app\services\WhatsAppService;
class UsersMessageManagementController extends Controller
{
$model->updated_at = date('Y-m-d H:i:s');
$model->updated_by = Yii::$app->user->id;
if ($model->validate()) {
+ if (!empty($model->channel_name)) {
+ $channelData = WhatsappService::getChannelByName($model->channel_name);
+ if (!empty($channelData)) {
+ if (isset($channelData['subjectId'])) {
+ $model->subject_id = $channelData['subjectId'];
+ }
+ if (isset($channelData['messagingLimit'])) {
+ $model->channel_limit = $channelData['messagingLimit'];
+ }
+ }
+ }
+
+ if (!empty($model->cascade_name)) {
+ $cascadeId = WhatsappService::getCascadeIdByName($model->cascade_name);
+ if ($cascadeId) {
+ $model->cascade_id = $cascadeId;
+ }
+ }
+ if (!empty($model->subject_id) && !empty($model->template_name)) {
+ $templateId = WhatsappService::getMessageMatcherIdBySubjectId(
+ $model->subject_id,
+ $model->template_name,
+ ["OPERATOR", "USER", "CUSTOM"]
+ );
+ if ($templateId) {
+ $model->template_id = $templateId;
+ }
+ }
$model->save();
$newAttributes = $model->getAttributes();
self::saveLogs($oldAttributes, $newAttributes);
}
$phone = $this->messageData['phone'];
-
+ $isTest = $this->isTest;
+ $message = ($this->messageData)['message'];
+ $kogortDate = ($this->messageData)['kogort_date'];
+ $targetDate = ($this->messageData)['target_date'];
$apiKey = Yii::$app->params['WHATSAPP_API_KEY'];
- $cascadeId = WhatsAppService::getCascadeIdByName('WABA') ?? 5686;
- $whatsappService = new WhatsAppService($apiKey, $cascadeId);
+ $cascadeId = ($this->messageData)['cascade_id'];
$requestId = DataHelper::uuid();
+ $whatsappService = new WhatsAppService($apiKey, $cascadeId);
+
try {
- $isTest = $this->isTest;
- $message = ($this->messageData)['message'];
- $kogortDate = ($this->messageData)['kogort_date'];
- $targetDate = ($this->messageData)['target_date'];
- if ($this->isTest) {
- $message = "Здравствуйте\nУзнать подробности вы можете на нашем сайте https://bazacvetov24.ru.";
+ if (!$this->isTest) {
+ // $message = "Здравствуйте\nУзнать подробности вы можете на нашем сайте https://bazacvetov24.ru.";
+ $response = $whatsappService->sendMessage($requestId, $phone, $message, $isTest);
}
- $response = $whatsappService->sendMessage($requestId, $phone, $message, $isTest);
-
$status = 'sent';
if (!$response instanceof WhatsAppMessageResponse) {
$record->created_at = date('Y-m-d H:i:s');
if ($record->save()) {
- Yii::warning("WhatsApp сообщение успешно отправлено и сохранено для телефона {$phone}. Request ID: " .
- $response->requestId, 'whatsapp');
+ if (!$this->isTest) {
+ Yii::warning(
+ "WhatsApp сообщение успешно отправлено и сохранено для телефона {$phone}. Request ID: " .
+ $response->requestId,
+ 'whatsapp'
+ );
+ } else {
+ Yii::warning(
+ "WhatsApp сообщение успешно сохранено для телефона {$phone}. Request ID: " .
+ $requestId,
+ 'whatsapp'
+ );
+ }
} else {
- Yii::warning("WhatsApp сообщение отправлено, но не удалось сохранить запись для телефона {$phone}." . json_encode($record->getErrors(), JSON_UNESCAPED_UNICODE), 'whatsapp');
+ Yii::warning(
+ "WhatsApp сообщение отправлено, но не удалось сохранить запись для телефона {$phone}."
+ . json_encode($record->getErrors(), JSON_UNESCAPED_UNICODE),
+ 'whatsapp'
+ );
}
} catch (\Exception $e) {
Yii::error("Ошибка отправки WhatsApp сообщения для телефона {$phone}: " . $e->getMessage(), 'whatsapp');
* @property string $test_phones_list Список тестовых телефонов
* @property string $test_phones_active Активность тестового списка
* @property string $channel_name Имя канала
+ * @property string $channel_id Идентификатор канала - подпись
+ * @property string $channel_limit Суточный лимит сообщений
+ * @property string $cascade_name Имя каскада
+ * @property string $cascade_id ID каскада
+ * @property string $subject_id Идентификатор подписи
+ * @property string $template_name Имя шаблона
+ * @property string $template_id Идентификатор шаблона
+ * @property string $callback_status_url URL приема колбеков статусов сообщений
*/
class UsersMessageManagement extends \yii\db\ActiveRecord
{
Yii::error("Текст сообщения для WhatsApp не передан.");
return null;
}
- $channel = self::getChannelByName('WABA');
- $subjectId = $channel['subjectId'] ?? 11374;
+ $messagesSettings = UsersMessageManagement::find()->one();
+
+ $subjectId = $messagesSettings ? $messagesSettings->cascade_id : 5686;
$requestId = $requestId ?? DataHelper::uuid();
// Формируем фильтр получателя по номеру телефона
];
$buttons = [
- 'rows' => [
+ 'row' => [
'buttons' => [
[
'text' => 'Наш сайт',
];
if (!$isTest) {
- // $whatsappContent['keyboard'] = $buttons;
- $whatsappContent['messageMatcherId'] = self::getMessageMatcherIdBySubjectId($subjectId, 'kogort_message_3') ?? 121254;
+ $whatsappContent['messageMatcherId'] = $messagesSettings ? $messagesSettings->template_id : 121254;
} else {
- $whatsappContent['messageMatcherId'] = self::getMessageMatcherIdBySubjectId($subjectId, 'podrobnosti') ?? 117216;
+ $whatsappContent['keyboard'] = $buttons;
+ $whatsappContent['messageMatcherId'] = self::getMessageMatcherIdBySubjectId(
+ $subjectId,
+ 'kogort_message'
+ ) ?? 120669;
}
-
$payload = [
'requestId' => $requestId,
'cascadeId' => $this->cascadeId,