diff --git a/app/app/Services/Bot/JivoBotClient.php b/app/app/Services/Bot/JivoBotClient.php new file mode 100644 index 00000000..d28bfe9e --- /dev/null +++ b/app/app/Services/Bot/JivoBotClient.php @@ -0,0 +1,56 @@ +post([ + 'event' => 'BOT_MESSAGE', + 'id' => (string) Str::uuid(), + 'chat_id' => $chatId, + 'client_id' => $clientId, + 'message' => ['type' => 'TEXT', 'text' => $text, 'timestamp' => now()->getTimestamp()], + ]); + } + + public function inviteAgent(string $chatId, string $clientId): void + { + $this->post([ + 'event' => 'INVITE_AGENT', + 'id' => (string) Str::uuid(), + 'chat_id' => $chatId, + 'client_id' => $clientId, + ]); + } + + /** @param array $payload */ + private function post(array $payload): void + { + $url = (string) config('services.jivo_bot.outbound_url'); + if ($url === '') { + Log::info('JivoBot outbound skipped (no outbound_url)', ['event' => $payload['event']]); + + return; + } + + try { + Http::timeout(5)->post($url, $payload)->throw(); + } catch (\Throwable $e) { + Log::warning('JivoBot outbound failure', ['event' => $payload['event'], 'error' => $e->getMessage()]); + } + } +} diff --git a/app/tests/Unit/Bot/JivoBotClientTest.php b/app/tests/Unit/Bot/JivoBotClientTest.php new file mode 100644 index 00000000..04d042fa --- /dev/null +++ b/app/tests/Unit/Bot/JivoBotClientTest.php @@ -0,0 +1,44 @@ +set('services.jivo_bot.outbound_url', 'https://bot.jivosite.com/webhooks/prov-1/tok-1'); +}); + +it('BOT_MESSAGE уходит с chat_id/client_id и текстом', function () { + Http::fake(['bot.jivosite.com/*' => Http::response(['ok' => true])]); + + app(JivoBotClient::class)->sendMessage('chat-1', 'client-1', 'Проект — это…'); + + Http::assertSent(function ($request) { + return $request->url() === 'https://bot.jivosite.com/webhooks/prov-1/tok-1' + && $request['event'] === 'BOT_MESSAGE' + && $request['chat_id'] === 'chat-1' + && $request['client_id'] === 'client-1' + && $request['message']['type'] === 'TEXT' + && $request['message']['text'] === 'Проект — это…'; + }); +}); + +it('INVITE_AGENT уходит без message', function () { + Http::fake(['bot.jivosite.com/*' => Http::response(['ok' => true])]); + + app(JivoBotClient::class)->inviteAgent('chat-1', 'client-1'); + + Http::assertSent(fn ($r) => $r['event'] === 'INVITE_AGENT' && $r['chat_id'] === 'chat-1'); +}); + +it('пустой outbound_url (dev/CI) → ничего не шлёт и не падает', function () { + config()->set('services.jivo_bot.outbound_url', ''); + Http::fake(); + + app(JivoBotClient::class)->sendMessage('chat-1', 'client-1', 'x'); + + Http::assertNothingSent(); +});