appKey = $config['appKey']; $this->appSecret = $config['appSecret']; $this->taskId = $config['taskId']; } } /** * 生成sign签名 * @param $requestQuery GET请求时的url参数数组 * @param $requestBody POST请求时的body参数Json字符串 * @param $timestamp 时间戳(毫秒级) * @return string */ public function getSign($requestQuery,$requestBody,$timestamp) { // 拼接值 $queryStr = ""; ksort($requestQuery); foreach($requestQuery as $value) { $queryStr .= $value; } // 得到签名值 $needSign = $timestamp. $queryStr. $requestBody; $sign = hash_hmac('sha256', $needSign, $this->appSecret); return $sign; } /* * 向外呼任务导入客户 */ public function importUser($customerList){ if (empty($this->appKey) || empty($this->appSecret) || empty($this->taskId)) { return false; } $requestBody = [ 'taskId' => $this->taskId, 'removeRepeat' => false, 'encryptedPhone' => false, 'customerList' => $customerList ]; $requestBody = json_encode($requestBody); $timestamp = $this->get_millisecond_timestamp(); $sign = $this->getSign([],$requestBody,$timestamp); //请求导入客户信息接口 $header = [ 'Content-Type: application/json', 'X-YS-APIKEY: '.$this->appKey, 'X-YS-TIME: '.$timestamp, 'X-YS-SIGNATURE: '.$sign, ]; $response = http_request('https://b.163.com/open/api/wecall/v1/task/importCustomer', $requestBody, $header); return $response; } /* * 开启外呼任务(仅手动任务可调用) */ public function startTask(){ if (empty($this->appKey) || empty($this->appSecret) || empty($this->taskId)) { return false; } $requestBody = [ 'taskId' => $this->taskId ]; $requestBody = json_encode($requestBody); $timestamp = $this->get_millisecond_timestamp(); $sign = $this->getSign([],$requestBody,$timestamp); //请求导入客户信息接口 $header = [ 'Content-Type: application/json', 'X-YS-APIKEY: '.$this->appKey, 'X-YS-TIME: '.$timestamp, 'X-YS-SIGNATURE: '.$sign, ]; $response = http_request('https://b.163.com/open/api/wecall/v1/task/start', $requestBody, $header); return $response; } /* * 停止外呼任务(仅手动任务可调用) */ public function stopTask(){ if (empty($this->appKey) || empty($this->appSecret) || empty($this->taskId)) { return false; } $requestBody = [ 'taskId' => $this->taskId ]; $requestBody = json_encode($requestBody); $timestamp = $this->get_millisecond_timestamp(); $sign = $this->getSign([],$requestBody,$timestamp); //请求导入客户信息接口 $header = [ 'Content-Type: application/json', 'X-YS-APIKEY: '.$this->appKey, 'X-YS-TIME: '.$timestamp, 'X-YS-SIGNATURE: '.$sign, ]; $response = http_request('https://b.163.com/open/api/wecall/v1/task/stop', $requestBody, $header); return $response; } /** * 数据回调接口 */ public function notify($body) { $body = json_decode($body, true); if ($body && isset($body['dataType'])) { if ($body['dataType'] == 'ROBOT_TASK_STATUS_CHANGE') { $this->taskStatusChange($body['data']); } } } private function taskStatusChange($data) { $taskId = $data['taskId']; $status = $data['status']; $updateTime = $data['updateTime']; if ($taskId == $this->taskId && $status == 'COMPLETE') { // TODO 任务状态改变 } } /** * 获取时间戳(毫秒级) */ public function get_millisecond_timestamp() { $microtime = microtime(true); $timestamp = floor($microtime * 1000); return $timestamp; } }