header('X-Chat-Webhook-Secret', ''); if ($secret === '' || ! hash_equals($secret, $incoming)) { return response()->json(['message' => 'unauthorized'], 401); } if (! self::isSmsNotifEnabled('sms_notif_support_admin')) { return response()->json(['ok' => true, 'skipped' => 'disabled']); } $roomId = trim((string) $request->input('room_id', '')); if ($roomId === '') { return response()->json(['message' => 'room_id required'], 422); } if (Cache::has(self::FOCUS_CACHE_PREFIX.$roomId)) { return response()->json(['ok' => true, 'skipped' => 'admin_focused']); } $customer = Customer::query() ->where('chat_room_id', $roomId) ->first(); $name = $customer ? trim((string) ($customer->name ?: $customer->username ?: 'مشتری')) : 'مشتری'; if ($name === '') { $name = 'مشتری'; } $message = 'شما از '.$name.' پیام دارید'; try { User::where('send_sms', 1)->get()->each(function ($admin) use ($message) { if (! empty($admin->mobile)) { $this->send($admin->mobile, $message); } }); } catch (\Throwable $e) { Log::warning('support chat SMS notify failed', [ 'room_id' => $roomId, 'error' => $e->getMessage(), ]); } return response()->json(['ok' => true]); } /** * Heartbeat تمرکز ادمین روی یک اتاق پشتیبانی (یا ترک تمرکز). */ public function focus(Request $request) { $roomId = trim((string) $request->input('room_id', '')); $previousRoomId = trim((string) $request->input('previous_room_id', '')); if ($previousRoomId !== '' && $previousRoomId !== $roomId) { Cache::forget(self::FOCUS_CACHE_PREFIX.$previousRoomId); } if ($roomId === '') { return response()->json(['ok' => true]); } Cache::put(self::FOCUS_CACHE_PREFIX.$roomId, true, self::FOCUS_TTL_SECONDS); return response()->json(['ok' => true, 'room_id' => $roomId]); } /** * ترک تمرکز روی یک اتاق مشخص. */ public function unfocus(Request $request) { $roomId = trim((string) $request->input('room_id', '')); if ($roomId !== '') { Cache::forget(self::FOCUS_CACHE_PREFIX.$roomId); } return response()->json(['ok' => true]); } /** * ثبت/تازه‌سازی وب‌هوک روی ah_chat (از پنل ادمین). */ public function ensureWebhook() { $ok = app(ChatWebhookService::class)->ensureRegistered(); return response()->json([ 'ok' => $ok, 'message' => $ok ? 'webhook registered' : 'webhook register skipped or failed', ], $ok ? 200 : 422); } }