| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- namespace app\common\library\dtalk;
- use app\common\exception\BaseException;
- use GuzzleHttp\Command\Guzzle\GuzzleClient;
- use GuzzleHttp\Psr7\Request;
- use GuzzleHttp\Client;
- /**
- * 钉钉机器人
- *
- * @Author Mark
- * @DateTime 2024-08-09
- */
- class Robot
- {
- /**
- * webhook 地址
- *
- * @var string
- * @Author Mark
- * @DateTime 2024-08-09
- */
- private $webhookURL = '';
- /**
- * 机器人密钥
- *
- * @var string
- * @Author Mark
- * @DateTime 2024-08-09
- */
- private $secret = '';
- /**
- * 发送的消息
- *
- * @var string
- * @Author Mark
- * @DateTime 2024-08-09
- */
- private $message = '';
- /**
- * at @ 的手机号
- *
- * @var array
- * @Author Mark
- * @DateTime 2024-08-09
- */
- private $atPhones = [];
- /**
- * 错误信息
- *
- * @var string
- * @Author Mark
- * @DateTime 2024-08-09
- */
- public $error = '';
- public function __construct()
- {
- }
- /**
- * 设置错误信息
- *
- * @param string $error
- * @return boolean
- * @Author Mark
- * @DateTime 2024-08-09
- */
- private function setError(string $error)
- {
- $this->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;
- }
- }
|