1
0
Эх сурвалжийг харах

增加Pushplus消息通道

Thund1r 3 жил өмнө
parent
commit
f59574ab5f

+ 8 - 0
.env.example

@@ -121,6 +121,14 @@ BARK_SOUND='gotosleep'
 BARK_ENABLE=0
 ######################  end Bark 送信  #########################
 
+######################  Pushplus  #########################
+# Pushplus的 token
+PUSHPLUS_KEY=''
+
+# 是否启用 Pushplus 送信功能 1:启用 0:不启用 Whether to enable the Pushplus message function 1: enabled 0: not enabled
+PUSHPLUS_ENABLE=0
+######################  end Pushplus  #########################
+
 # 通知频率 0:仅当有续期操作的时候 1:每次执行 Notification frequency 0: Only when there is a renewal operation 1: Each execution
 NOTICE_FREQ=1
 

+ 11 - 0
config.php

@@ -87,6 +87,17 @@ return [
             'class' => \Luolongfei\Libs\MessageServices\Bark::class,
             'name' => lang('100068'),
         ],
+
+        /**
+         * Pushplus
+         */
+        'pushplus' => [
+            'pushplus_key' => env('PUSHPLUS_KEY'), // SendKey
+            'enable' => (int)env('PUSHPLUS_ENABLE'), // 是否启用,默认不启用
+            'not_enabled_tips' => (bool)env('PUSHPLUS_KEY'), // 提醒未启用
+            'class' => \Luolongfei\Libs\MessageServices\Pushplus::class,
+            'name' => lang('100136'),
+        ],
     ],
     'custom_language' => env('CUSTOM_LANGUAGE', 'zh'),
     'notice_freq' => (int)env('NOTICE_FREQ', 1), // 通知频率 0:仅当有续期操作的时候 1:每次执行

+ 211 - 0
libs/MessageServices/Pushplus.php

@@ -0,0 +1,211 @@
+<?php
+/**
+ * Pushplus
+ *
+ * @author thund1r <[email protected]>
+ * @date 2022/10/13
+ * @time 21:00
+ */
+
+namespace Luolongfei\Libs\MessageServices;
+
+use GuzzleHttp\Client;
+use Luolongfei\Libs\Connector\MessageGateway;
+
+class Pushplus extends MessageGateway
+{
+    const TIMEOUT = 33;
+
+    /**
+     * @var string SendKey
+     */
+    protected $sendKey;
+
+    /**
+     * @var Client
+     */
+    protected $client;
+
+    public function __construct()
+    {
+        $this->sendKey = config('message.pushplus.pushplus_key');
+
+        $this->client = new Client([
+            'cookies' => false,
+            'timeout' => self::TIMEOUT,
+            'verify' => config('verify_ssl'),
+            'debug' => config('debug'),
+        ]);
+    }
+
+    /**
+     * 生成域名状态 MarkDown 完整文本
+     *
+     * @param string $username
+     * @param array $domainStatus
+     *
+     * @return string
+     */
+    public function genDomainStatusFullMarkDownText(string $username, array $domainStatus)
+    {
+        $markDownText = sprintf(lang('100090'), $username);
+
+        $markDownText .= $this->genDomainStatusMarkDownText($domainStatus);
+
+        $markDownText .= $this->getMarkDownFooter();
+
+        return $markDownText;
+    }
+
+    /**
+     * 获取 MarkDown 页脚
+     *
+     * @return string
+     */
+    public function getMarkDownFooter()
+    {
+        $footer = '';
+
+        $footer .= lang('100091');
+
+        return $footer;
+    }
+
+    /**
+     * 生成域名状态 MarkDown 文本
+     *
+     * @param array $domainStatus
+     *
+     * @return string
+     */
+    public function genDomainStatusMarkDownText(array $domainStatus)
+    {
+        if (empty($domainStatus)) {
+            return lang('100093');
+        }
+
+        $domainStatusMarkDownText = '';
+
+        foreach ($domainStatus as $domain => $daysLeft) {
+            $domainStatusMarkDownText .= sprintf(lang('100094'), $domain, $domain, $daysLeft);
+        }
+
+        $domainStatusMarkDownText = rtrim(rtrim($domainStatusMarkDownText, ' '), ',,') . lang('100095');
+
+        return $domainStatusMarkDownText;
+    }
+
+    /**
+     * 生成域名续期结果 MarkDown 文本
+     *
+     * @param string $username
+     * @param array $renewalSuccessArr
+     * @param array $renewalFailuresArr
+     * @param array $domainStatus
+     *
+     * @return string
+     */
+    public function genDomainRenewalResultsMarkDownText(string $username, array $renewalSuccessArr, array $renewalFailuresArr, array $domainStatus)
+    {
+        $text = sprintf(lang('100096'), $username);
+
+        if ($renewalSuccessArr) {
+            $text .= lang('100097');
+            $text .= $this->genDomainsMarkDownText($renewalSuccessArr);
+        }
+
+        if ($renewalFailuresArr) {
+            $text .= lang('100098');
+            $text .= $this->genDomainsMarkDownText($renewalFailuresArr);
+        }
+
+        $text .= lang('100099');
+        $text .= $this->genDomainStatusMarkDownText($domainStatus);
+
+        $text .= $this->getMarkDownFooter();
+
+        return $text;
+    }
+
+    /**
+     * 生成域名 MarkDown 文本
+     *
+     * @param array $domains
+     *
+     * @return string
+     */
+    public function genDomainsMarkDownText(array $domains)
+    {
+        $domainsMarkDownText = '';
+
+        foreach ($domains as $domain) {
+            $domainsMarkDownText .= sprintf("[%s](http://%s) ", $domain, $domain);
+        }
+
+        $domainsMarkDownText = trim($domainsMarkDownText, ' ') . "\n";
+
+        return $domainsMarkDownText;
+    }
+
+    /**
+     * 送信
+     *
+     * @param string $content
+     * @param string $subject
+     * @param int $type
+     * @param array $data
+     * @param string|null $recipient
+     * @param mixed ...$params
+     *
+     * @return bool
+     * @throws \Exception
+     */
+    public function send(string $content, string $subject = '', int $type = 1, array $data = [], ?string $recipient = null, ...$params)
+    {
+        $this->check($content, $data);
+
+        $commonFooter = '';
+
+        if ($type === 1 || $type === 4) {
+            $this->setCommonFooter($commonFooter, "\n", false);
+        } else if ($type === 2) {
+            $this->setCommonFooter($commonFooter, "\n", false);
+            $content = $this->genDomainRenewalResultsMarkDownText($data['username'], $data['renewalSuccessArr'], $data['renewalFailuresArr'], $data['domainStatusArr']);
+        } else if ($type === 3) {
+            $this->setCommonFooter($commonFooter);
+            $content = $this->genDomainStatusFullMarkDownText($data['username'], $data['domainStatusArr']);
+        } else {
+            throw new \Exception(lang('100003'));
+        }
+
+        $content .= $commonFooter;
+
+        $subject = $subject === '' ? mb_substr($content, 0, 12) . '...' : $subject;
+
+        try {
+            $resp = $this->client->post(
+                'http://www.pushplus.plus/send',
+                [
+                    'form_params' => [
+                        'token' => $this->sendKey,
+                        'template' => "markdown",
+                        'title' => $subject,
+                        'content' => $content,
+                    ],
+                ]
+            );
+
+            $resp = json_decode((string)$resp->getBody(), true);
+
+            if (isset($resp['code']) && $resp['code'] === 200) {
+                return true;
+            }
+
+            throw new \Exception($resp['msg'] ?? lang('100100'));
+        } catch (\Exception $e) {
+            system_log(sprintf(lang('100137'), $e->getMessage()));
+
+            return false;
+        }
+    }
+}

+ 2 - 0
resources/lang/en.php

@@ -153,5 +153,7 @@ return [
         '100133' => "(If you don't want to get a push every time you execute, adjust the value of NOTICE_FREQ in the .env file to 0 so that the application only pushes when there is a renewal operation.)",
         '100134' => '[Server Info]',
         '100135' => 'It is detected that the Data directory does not have write permission and cannot record the version number. To avoid repeated pushing of upgrade messages, the program will ignore the version check. Please go to https://github.com/luolongfei/freenom/releases to check if there is a new version by yourself',
+        '100136' => 'Pushplus',
+        '100137' => 'Pushplus Failed to send message: <red>%s</red>',
     ],
 ];

+ 2 - 0
resources/lang/zh.php

@@ -153,5 +153,7 @@ return [
         '100133' => '(如果你不想每次执行都收到推送,请将 .env 中 NOTICE_FREQ 的值设为 0,使程序只在有续期操作时才推送)',
         '100134' => '【服务器信息】',
         '100135' => '检测到 Data 目录没有写权限,无法记录版本号,为了避免重复推送升级消息,程序将忽略版本检查。请自行前往 https://github.com/luolongfei/freenom/releases 查看是否有新版本',
+        '100136' => 'Pushplus',
+        '100137' => 'Pushplus 消息发送失败:<red>%s</red>',
     ],
 ];