企微短剧业务系统

ChatService.php 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\AuthorizeCorp;
  5. class ChatService
  6. {
  7. /**
  8. * 同步客户和客服间的聊天记录
  9. * @param string $corpid 企业ID
  10. * @param string $token 回调事件返回的token字段,10分钟内有效;可不填,如果不填接口有严格的频率限制
  11. * @param int $limit 期望请求的数据量,不允许超过1000
  12. * @param int $voiceFormat 语音消息类型,0-Amr 1-Silk,默认0。可通过该参数控制返回的语音格式
  13. * */
  14. public static function syncChatMsg($corpid, $token, $limit=1000, $cursor=null, $voiceFormat=0)
  15. {
  16. # 获取SuiteAccessToken
  17. $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取客户与客服聊天记录');
  18. if(empty($accessToken)) { // 令牌获取失败,发送报警
  19. Log::logError('【获取外部联系人信息详情】令牌获取失败', [
  20. 'corpid' => $corpid,
  21. 'token' => $token,
  22. 'cursor' => $cursor,
  23. 'limit' => $limit,
  24. 'voice_format' => $voiceFormat,
  25. 'access_token' => $accessToken
  26. ], 'SyncChatMsg');
  27. return false;
  28. }
  29. # 获取外部联系人详情信息API
  30. $requestUri = config('qyWechat.sync_chat_msg');
  31. $requestUri .= $accessToken;
  32. $postData = array(
  33. 'token' => $token,
  34. 'limit' => $limit,
  35. 'voice_format' => $voiceFormat
  36. );
  37. if($cursor) $postData['cursor'] = $cursor;
  38. Log::logInfo('请求参数:', $postData, '05-05Sync');
  39. $response = HttpService::httpPost($requestUri, json_encode($postData));
  40. $responseData = json_decode($response, true);
  41. Log::logInfo('响应结果:', $responseData, '05-05Sync');
  42. return $responseData;
  43. }
  44. }