error = $error; return false; } /** * 设置机器人accessTokne和secret * * @param string $webhookURL * @param string $secret * @return static * @Author Mark * @DateTime 2024-08-09 */ public function setWebhookAndSecret(string $webhookURL, string $secret) { $this->webhookURL = $webhookURL; $this->secret = $secret; return $this; } /** * 设置消息 * * @param string $message * @return static * @Author Mark * @DateTime 2024-08-09 */ public function setMessage(string $message) { $this->message = $message; return $this; } /** * 设置 @的账号 * * @param array $phones * @return static * @Author Mark * @DateTime 2024-08-09 */ public function setAtPhones(array $phones) { $this->atPhones = $phones; return $this; } /** * 获取签名 * * @param integer $timestamp * @return string * @Author Mark * @DateTime 2024-08-09 */ private function getSign(int $timestamp): string { $string = hash_hmac('sha256', $timestamp . "\n" . $this->secret, $this->secret, true); $sign = urlencode(base64_encode($string)); return $sign; } /** * 发送消息 * * @return boolean * @Author Mark * @DateTime 2024-08-09 */ public function sendMessage(): bool { $timestamp = time() * 1000; $sign = $this->getSign($timestamp); //发送内容 $content = [ 'at' => [ 'atMobiles' => $this->atPhones, ], 'text' => [ 'content' => $this->message, ], 'msgtype' => 'text', ]; $url = $this->webhookURL . "×tamp={$timestamp}&sign={$sign}"; //发送请求 $client = new Client(); $options = [ 'headers' => ['Content-Type' => 'application/json;charset=UTF-8'], 'json' => $content, 'verify' => false ]; $response = $client->request('POST', $url, $options); //判断响应状态 $body = $response->getBody()->getContents(); $res = json_encode($body, true); if (empty($res['errcode'])) { return $this->setError('未知错误'); } if ($res['errcode'] != 0) { return $this->setError($res['errmsg']); } return true; } }