*/ public static function accounts(): array { $json = self::get('/api/v2/archive/staff-feed/accounts'); $accounts = []; foreach (is_array($json['accounts'] ?? null) ? $json['accounts'] : [] as $row) { if (!is_array($row)) { continue; } $accounts[] = ['external_id' => (string) ($row['external_id'] ?? ''), 'name' => trim((string) ($row['name'] ?? '')), 'conversations' => (int) ($row['conversations'] ?? 0)]; } return $accounts; } /** @return list */ public static function conversations(string $externalId): array { if ($externalId === '') { return []; } $json = self::get('/api/v2/archive/staff-feed/conversations', ['account' => $externalId]); $items = []; foreach (is_array($json['conversations'] ?? null) ? $json['conversations'] : [] as $row) { if (!is_array($row)) { continue; } $items[] = ['id' => (string) ($row['id'] ?? ''), 'name' => trim((string) ($row['name'] ?? '')), 'conversation_type' => (string) ($row['conversation_type'] ?? ''), 'last_message_at' => (string) ($row['last_message_at'] ?? ''), 'message_count' => (int) ($row['message_count'] ?? 0), 'preview' => trim((string) ($row['preview'] ?? ''))]; } return $items; } /** @return array{account:string,messages:list} */ public static function messages(string $conversationId): array { if ($conversationId === '') { return ['account' => '', 'messages' => []]; } $json = self::get('/api/v2/archive/staff-feed/messages', ['conversation_id' => $conversationId]); $messages = []; foreach (is_array($json['messages'] ?? null) ? $json['messages'] : [] as $row) { if (!is_array($row)) { continue; } $messages[] = ['id' => (string) ($row['id'] ?? ''), 'sent_at' => (string) ($row['sent_at'] ?? ''), 'direction' => (string) ($row['direction'] ?? ''), 'message_type' => (string) ($row['message_type'] ?? ''), 'sender_name' => trim((string) ($row['sender_name'] ?? '')), 'content' => trim((string) ($row['content'] ?? ''))]; } return ['account' => (string) ($json['account'] ?? ''), 'messages' => $messages]; } /** @return array{configured:bool,items:array>,next_cursor:string,has_more:bool} */ public static function page(string $cursor, int $limit): array { $empty = ['configured' => false, 'items' => [], 'next_cursor' => $cursor, 'has_more' => false]; if (!self::configured()) { return $empty; } $limit = max(1, min(200, $limit)); $json = self::get('/api/v2/archive/staff-feed/outbound', ['cursor' => $cursor, 'limit' => (string) $limit]); $rows = is_array($json['items'] ?? null) ? $json['items'] : []; return ['configured' => true, 'items' => WecomArchiveSqlite::identify($rows), 'next_cursor' => (string) ($json['next_cursor'] ?? $cursor), 'has_more' => (bool) ($json['has_more'] ?? false)]; } /** @param array $query @return array */ private static function get(string $path, array $query = []): array { $base = self::base(); $token = self::token(); if ($base === '' || $token === '') { throw new RuntimeException('WECOM_ARCHIVE'); } $url = $base . $path; if ($query) { $url .= '?' . http_build_query($query); } $handle = curl_init($url); if ($handle === false) { throw new RuntimeException('WECOM_ARCHIVE'); } curl_setopt_array($handle, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 15, CURLOPT_FOLLOWLOCATION => false, CURLOPT_HTTPHEADER => ['Accept: application/json', 'X-Archive-Feed-Token: ' . $token]]); $body = curl_exec($handle); $code = (int) curl_getinfo($handle, CURLINFO_RESPONSE_CODE); curl_close($handle); $json = is_string($body) ? json_decode($body, true) : null; if ($code !== 200 || !is_array($json)) { throw new RuntimeException('WECOM_ARCHIVE'); } return $json; } private static function base(): string { return rtrim((string) config('assistant_service.wecom_archive_base_url', ''), '/'); } private static function token(): string { return trim((string) config('assistant_service.wecom_archive_token', '')); } }