added trigger for chat sms
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ChatWebhookService
|
||||
{
|
||||
public function ensureRegistered(): bool
|
||||
{
|
||||
$chatApi = Controller::Config('chat_api');
|
||||
$chatApiKey = Controller::Config('chat_api_key');
|
||||
|
||||
if (!$chatApi || !$chatApiKey) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$host = preg_replace('#^https?://#', '', rtrim((string) $chatApi, '/'));
|
||||
$secret = $this->ensureSecret();
|
||||
if ($host === '' || $secret === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$webhookUrl = rtrim((string) config('app.url'), '/').'/api/chat/support-message-notify';
|
||||
|
||||
try {
|
||||
$response = Http::timeout(8)->post('https://'.$host.'/api/projects/set-webhook', [
|
||||
'api_key' => $chatApiKey,
|
||||
'webhook_url' => $webhookUrl,
|
||||
'webhook_secret' => $secret,
|
||||
]);
|
||||
|
||||
return $response->successful();
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('chat webhook register failed', [
|
||||
'host' => $host,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function ensureSecret(): string
|
||||
{
|
||||
$existing = trim((string) Controller::Config('chat_webhook_secret'));
|
||||
if ($existing !== '') {
|
||||
return $existing;
|
||||
}
|
||||
|
||||
$secret = Str::random(48);
|
||||
|
||||
$row = Setting::where('key', 'chat_webhook_secret')->first();
|
||||
if ($row) {
|
||||
if (trim((string) $row->value) === '') {
|
||||
$row->value = $secret;
|
||||
$row->save();
|
||||
} else {
|
||||
return (string) $row->value;
|
||||
}
|
||||
} else {
|
||||
Setting::create([
|
||||
'key' => 'chat_webhook_secret',
|
||||
'value' => $secret,
|
||||
'title_fa' => 'رمز وبهوک چت',
|
||||
'is_active' => true,
|
||||
'can_edit' => 0,
|
||||
'description' => 'رمز مشترک بین لاراول و سرور چت برای اعلان پیام پشتیبانی. فقط سرور؛ در صورت خالی بودن بهصورت خودکار ساخته میشود.',
|
||||
'type' => 'text',
|
||||
]);
|
||||
}
|
||||
|
||||
return $secret;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user