Bark.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * Bark 推送
  4. *
  5. * @author mybsdc <[email protected]>
  6. * @date 2021/11/3
  7. * @time 11:18
  8. */
  9. namespace Luolongfei\Libs\MessageServices;
  10. use GuzzleHttp\Client;
  11. use Luolongfei\Libs\Connector\MessageGateway;
  12. class Bark extends MessageGateway
  13. {
  14. const TIMEOUT = 33;
  15. /**
  16. * @var string Bark Key
  17. */
  18. protected $barkKey;
  19. /**
  20. * @var string Bark 域名
  21. */
  22. protected $barkUrl;
  23. /**
  24. * @var integer|string 指定是否需要保存推送信息到历史记录,1 为保存,其他值为不保存。如果值为空字符串,则推送信息将按照 APP 内设置来决定是否保存
  25. */
  26. protected $isArchive;
  27. /**
  28. * @var string 指定推送消息分组,可在历史记录中按分组查看推送
  29. */
  30. protected $group;
  31. /**
  32. * 可选参数值
  33. * active:不设置时的默认值,系统会立即亮屏显示通知
  34. * timeSensitive:时效性通知,可在专注状态下显示通知
  35. * passive:仅将通知添加到通知列表,不会亮屏提醒
  36. *
  37. * @var string 时效性通知
  38. */
  39. protected $level;
  40. /**
  41. * @var string 指定推送消息图标 (仅 iOS15 或以上支持)http://day.app/assets/images/avatar.jpg
  42. */
  43. protected $icon;
  44. /**
  45. * @var string 点击推送将跳转到url的地址(发送时,URL参数需要编码),GuzzleHttp 库会自动编码
  46. */
  47. protected $jumpUrl;
  48. /**
  49. * @var integer 携带参数 automaticallyCopy=1, 收到推送时,推送内容会自动复制到粘贴板(如发现不能自动复制,可尝试重启一下手机)
  50. */
  51. protected $automaticallyCopy = 1;
  52. /**
  53. * @var string 携带 copy 参数, 则上面两种复制操作,将只复制 copy 参数的值
  54. */
  55. protected $copy = 'https://my.freenom.com/domains.php?a=renewals';
  56. /**
  57. * @var Client
  58. */
  59. protected $client;
  60. public function __construct()
  61. {
  62. $this->barkKey = config('message.bark.bark_key');
  63. $this->barkUrl = rtrim(config('message.bark.bark_url'), '/');
  64. $this->isArchive = config('message.bark.bark_is_archive');
  65. $this->group = config('message.bark.bark_group');
  66. $this->level = config('message.bark.bark_level');
  67. $this->icon = config('message.bark.bark_icon');
  68. $this->jumpUrl = config('message.bark.bark_jump_url');
  69. $this->client = new Client([
  70. 'cookies' => false,
  71. 'timeout' => self::TIMEOUT,
  72. 'verify' => config('verify_ssl'),
  73. 'debug' => config('debug'),
  74. ]);
  75. }
  76. /**
  77. * 生成域名文本
  78. *
  79. * @param array $domains
  80. *
  81. * @return string
  82. */
  83. public function genDomainsText(array $domains)
  84. {
  85. $domainsText = '';
  86. foreach ($domains as $domain) {
  87. $domainsText .= sprintf('%s ', $domain);
  88. }
  89. $domainsText = trim($domainsText, ' ') . "\n";
  90. return $domainsText;
  91. }
  92. /**
  93. * 获取页脚
  94. *
  95. * @return string
  96. */
  97. public function getFooter()
  98. {
  99. $footer = '';
  100. $footer .= "\n更多信息可以参考:https://my.freenom.com/domains.php?a=renewals(点击“复制内容”即可复制此网址)";
  101. $footer .= "\n\n(如果你不想每次执行都收到推送,请将 .env 中 NOTICE_FREQ 的值设为 0,使程序只在有续期操作时才推送)";
  102. return $footer;
  103. }
  104. /**
  105. * 生成域名状态文本
  106. *
  107. * @param array $domainStatus
  108. *
  109. * @return string
  110. */
  111. public function genDomainStatusText(array $domainStatus)
  112. {
  113. if (empty($domainStatus)) {
  114. return "无数据。\n";
  115. }
  116. $domainStatusText = '';
  117. foreach ($domainStatus as $domain => $daysLeft) {
  118. $domainStatusText .= sprintf('%s 还有 %d 天到期,', $domain, $daysLeft);
  119. }
  120. $domainStatusText = rtrim($domainStatusText, ',') . "。\n";
  121. return $domainStatusText;
  122. }
  123. /**
  124. * 生成域名续期结果文本
  125. *
  126. * @param string $username
  127. * @param array $renewalSuccessArr
  128. * @param array $renewalFailuresArr
  129. * @param array $domainStatus
  130. *
  131. * @return string
  132. */
  133. public function genDomainRenewalResultsText(string $username, array $renewalSuccessArr, array $renewalFailuresArr, array $domainStatus)
  134. {
  135. $text = sprintf("账户 %s 这次续期的结果如下\n\n", $username);
  136. if ($renewalSuccessArr) {
  137. $text .= '续期成功:';
  138. $text .= $this->genDomainsText($renewalSuccessArr);
  139. }
  140. if ($renewalFailuresArr) {
  141. $text .= '续期出错:';
  142. $text .= $this->genDomainsText($renewalFailuresArr);
  143. }
  144. $text .= "\n今次无需续期的域名及其剩余天数如下所示:\n\n";
  145. $text .= $this->genDomainStatusText($domainStatus);
  146. $text .= $this->getFooter();
  147. return $text;
  148. }
  149. /**
  150. * 生成域名状态完整文本
  151. *
  152. * @param string $username
  153. * @param array $domainStatus
  154. *
  155. * @return string
  156. */
  157. public function genDomainStatusFullText(string $username, array $domainStatus)
  158. {
  159. $markDownText = sprintf("我刚刚帮小主看了一下,账户 %s 今天并没有需要续期的域名。所有域名情况如下:\n\n", $username);
  160. $markDownText .= $this->genDomainStatusText($domainStatus);
  161. $markDownText .= $this->getFooter();
  162. return $markDownText;
  163. }
  164. /**
  165. * 送信
  166. *
  167. * @param string $content
  168. * @param string $subject
  169. * @param int $type
  170. * @param array $data
  171. * @param string|null $recipient
  172. * @param mixed ...$params
  173. *
  174. * @return bool|mixed
  175. * @throws \Exception
  176. */
  177. public function send(string $content, string $subject = '', int $type = 1, array $data = [], ?string $recipient = null, ...$params)
  178. {
  179. $this->check($content, $data);
  180. if ($type === 1) {
  181. // Do nothing
  182. } else if ($type === 2) {
  183. $content = $this->genDomainRenewalResultsText($data['username'], $data['renewalSuccessArr'], $data['renewalFailuresArr'], $data['domainStatusArr']);
  184. } else if ($type === 3) {
  185. $content = $this->genDomainStatusFullText($data['username'], $data['domainStatusArr']);
  186. } else {
  187. throw new \Exception(lang('error_msg.100003'));
  188. }
  189. $query = [
  190. 'level' => $this->level,
  191. 'automaticallyCopy' => $this->automaticallyCopy, // 携带参数 automaticallyCopy=1, 收到推送时,推送内容会自动复制到粘贴板(如发现不能自动复制,可尝试重启一下手机)
  192. 'copy' => $this->copy, // 携带 copy 参数,则上面的复制操作,将只复制 copy 参数的值
  193. ];
  194. if ($this->isArchive !== null) {
  195. $query['isArchive'] = $this->isArchive;
  196. }
  197. if ($this->group !== null) {
  198. $query['group'] = $this->group;
  199. }
  200. if ($this->icon !== null) {
  201. $query['icon'] = $this->icon;
  202. }
  203. if ($this->jumpUrl !== null) {
  204. $query['url'] = $this->jumpUrl;
  205. }
  206. $formParams = [
  207. 'body' => $content, // 推送内容 换行请使用换行符 \n
  208. ];
  209. if ($subject !== '') {
  210. $formParams['title'] = $subject; // 推送标题 比 body 字号粗一点
  211. }
  212. try {
  213. $resp = $this->client->post(
  214. sprintf('%s/%s/', $this->barkUrl, $this->barkKey),
  215. [
  216. 'query' => $query,
  217. 'form_params' => $formParams,
  218. ]
  219. );
  220. $resp = json_decode($resp->getBody()->getContents(), true);
  221. if (isset($resp['code']) && $resp['code'] === 200) {
  222. return true;
  223. }
  224. throw new \Exception($resp['message'] ?? '未知原因');
  225. } catch (\Exception $e) {
  226. system_log('Bark 送信失败:<red>' . $e->getMessage() . '</red>');
  227. return false;
  228. }
  229. }
  230. }