Robot.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace app\common\library\dtalk;
  3. use app\common\exception\BaseException;
  4. use GuzzleHttp\Command\Guzzle\GuzzleClient;
  5. use GuzzleHttp\Psr7\Request;
  6. use GuzzleHttp\Client;
  7. /**
  8. * 钉钉机器人
  9. *
  10. * @Author Mark
  11. * @DateTime 2024-08-09
  12. */
  13. class Robot
  14. {
  15. /**
  16. * webhook 地址
  17. *
  18. * @var string
  19. * @Author Mark
  20. * @DateTime 2024-08-09
  21. */
  22. private $webhookURL = '';
  23. /**
  24. * 机器人密钥
  25. *
  26. * @var string
  27. * @Author Mark
  28. * @DateTime 2024-08-09
  29. */
  30. private $secret = '';
  31. /**
  32. * 发送的消息
  33. *
  34. * @var string
  35. * @Author Mark
  36. * @DateTime 2024-08-09
  37. */
  38. private $message = '';
  39. /**
  40. * at @ 的手机号
  41. *
  42. * @var array
  43. * @Author Mark
  44. * @DateTime 2024-08-09
  45. */
  46. private $atPhones = [];
  47. /**
  48. * 错误信息
  49. *
  50. * @var string
  51. * @Author Mark
  52. * @DateTime 2024-08-09
  53. */
  54. public $error = '';
  55. public function __construct()
  56. {
  57. }
  58. /**
  59. * 设置错误信息
  60. *
  61. * @param string $error
  62. * @return boolean
  63. * @Author Mark
  64. * @DateTime 2024-08-09
  65. */
  66. private function setError(string $error)
  67. {
  68. $this->error = $error;
  69. return false;
  70. }
  71. /**
  72. * 设置机器人accessTokne和secret
  73. *
  74. * @param string $webhookURL
  75. * @param string $secret
  76. * @return static
  77. * @Author Mark
  78. * @DateTime 2024-08-09
  79. */
  80. public function setWebhookAndSecret(string $webhookURL, string $secret)
  81. {
  82. $this->webhookURL = $webhookURL;
  83. $this->secret = $secret;
  84. return $this;
  85. }
  86. /**
  87. * 设置消息
  88. *
  89. * @param string $message
  90. * @return static
  91. * @Author Mark
  92. * @DateTime 2024-08-09
  93. */
  94. public function setMessage(string $message)
  95. {
  96. $this->message = $message;
  97. return $this;
  98. }
  99. /**
  100. * 设置 @的账号
  101. *
  102. * @param array $phones
  103. * @return static
  104. * @Author Mark
  105. * @DateTime 2024-08-09
  106. */
  107. public function setAtPhones(array $phones)
  108. {
  109. $this->atPhones = $phones;
  110. return $this;
  111. }
  112. /**
  113. * 获取签名
  114. *
  115. * @param integer $timestamp
  116. * @return string
  117. * @Author Mark
  118. * @DateTime 2024-08-09
  119. */
  120. private function getSign(int $timestamp): string
  121. {
  122. $string = hash_hmac('sha256', $timestamp . "\n" . $this->secret, $this->secret, true);
  123. $sign = urlencode(base64_encode($string));
  124. return $sign;
  125. }
  126. /**
  127. * 发送消息
  128. *
  129. * @return boolean
  130. * @Author Mark
  131. * @DateTime 2024-08-09
  132. */
  133. public function sendMessage(): bool
  134. {
  135. $timestamp = time() * 1000;
  136. $sign = $this->getSign($timestamp);
  137. //发送内容
  138. $content = [
  139. 'at' => [
  140. 'atMobiles' => $this->atPhones,
  141. ],
  142. 'text' => [
  143. 'content' => $this->message,
  144. ],
  145. 'msgtype' => 'text',
  146. ];
  147. $url = $this->webhookURL . "&timestamp={$timestamp}&sign={$sign}";
  148. //发送请求
  149. $client = new Client();
  150. $options = [
  151. 'headers' => ['Content-Type' => 'application/json;charset=UTF-8'],
  152. 'json' => $content,
  153. 'verify' => false
  154. ];
  155. $response = $client->request('POST', $url, $options);
  156. //判断响应状态
  157. $body = $response->getBody()->getContents();
  158. $res = json_encode($body, true);
  159. if (empty($res['errcode'])) {
  160. return $this->setError('未知错误');
  161. }
  162. if ($res['errcode'] != 0) {
  163. return $this->setError($res['errmsg']);
  164. }
  165. return true;
  166. }
  167. }