access_token = $str; return $this; } /** * 发送文本消息 * @param string $content 消息内容 * @return bool 发送结果 * @throws DtaException */ public function text(string $content): bool { $this->msg_type = 'text'; return $this->sendMsg([ 'text' => [ 'content' => $content, ], ]); } /** * 组装发送消息 * @param array $data 消息内容数组 * @return bool 发送结果 * @throws DtaException */ private function sendMsg(array $data): bool { if (empty($this->access_token)) { throw new DtaException("请检查access_token"); } if (empty($data['msgtype'])) { $data['msgtype'] = $this->msg_type; } $result = HttpService::instance() ->url("{$this->oapi_url}robot/send?access_token=" . $this->access_token) ->data($data) ->post() ->toArray(); if ($result['errcode'] == 0) { return $result['errmsg']; } throw new HttpException(404, json_encode($result, JSON_UNESCAPED_UNICODE)); } }