diff --git a/app/app/Jobs/Bot/ProcessChatMessageJob.php b/app/app/Jobs/Bot/ProcessChatMessageJob.php index 70a3d2fe..75bf1514 100644 --- a/app/app/Jobs/Bot/ProcessChatMessageJob.php +++ b/app/app/Jobs/Bot/ProcessChatMessageJob.php @@ -107,11 +107,8 @@ class ProcessChatMessageJob implements ShouldQueue $rows = BotDialog::query() ->where('chat_id', $this->chatId) - ->orderByDesc('id') - ->limit(8) - ->get(['direction', 'message']) - ->reverse() - ->values(); + ->orderBy('id') + ->get(['direction', 'message', 'source', 'user_id']); $question = $rows ->filter(fn (BotDialog $d) => $d->direction === 'in') @@ -122,12 +119,16 @@ class ProcessChatMessageJob implements ShouldQueue ->map(fn (BotDialog $d) => ($d->direction === 'in' ? 'Клиент: ' : 'Бот: ').$d->message) ->all(); + $client = $rows->firstWhere(fn (BotDialog $d) => $d->direction === 'in'); + $source = (string) ($client->source ?? 'portal'); + $userId = $client->user_id !== null ? (int) $client->user_id : null; + $to = (string) config('services.support.email'); if ($to !== '') { // Письмо — В ОЧЕРЕДЬ. Прогон 12.07.2026: почта не ответила → упала вся задача // (tries=1), и клиент, оставивший номер, не получил НИЧЕГО. try { - Mail::to($to)->queue(new BotContactRequestMail($phone, $question, $this->chatId, $transcript)); + Mail::to($to)->queue(new BotContactRequestMail($phone, $question, $this->chatId, $transcript, $source, $userId)); } catch (\Throwable $e) { Log::error('Не удалось поставить в очередь письмо с контактом', [ 'chat_id' => $this->chatId, diff --git a/app/app/Mail/BotContactRequestMail.php b/app/app/Mail/BotContactRequestMail.php index 0ea8f84f..22dd4188 100644 --- a/app/app/Mail/BotContactRequestMail.php +++ b/app/app/Mail/BotContactRequestMail.php @@ -23,13 +23,17 @@ class BotContactRequestMail extends Mailable /** * @param string $phone нормализованный номер (79161234567) * @param string $question вопрос, на котором бот спасовал - * @param list $transcript последние реплики чата («Клиент: …», «Бот: …») + * @param list $transcript вся переписка чата («Клиент: …», «Бот: …») + * @param string $source 'portal' (личный кабинет) или 'landing' (гость) + * @param int|null $userId ID клиента, если он вошёл в кабинет; null для гостя */ public function __construct( public readonly string $phone, public readonly string $question, public readonly string $chatId, public readonly array $transcript = [], + public readonly string $source = 'portal', + public readonly ?int $userId = null, ) {} public function envelope(): Envelope @@ -48,6 +52,8 @@ class BotContactRequestMail extends Mailable 'question' => $this->question, 'chatId' => $this->chatId, 'transcript' => $this->transcript, + 'source' => $this->source, + 'userId' => $this->userId, ], ); } diff --git a/app/resources/views/emails/bot_contact_request.blade.php b/app/resources/views/emails/bot_contact_request.blade.php index 0d92caeb..6e4479ef 100644 --- a/app/resources/views/emails/bot_contact_request.blade.php +++ b/app/resources/views/emails/bot_contact_request.blade.php @@ -1,6 +1,15 @@

Клиент писал в чат Лидерры, бот не смог ответить сам и попросил телефон для связи.