From 36fd6035f776c630a006bc32ba10e7cdd53aab91 Mon Sep 17 00:00:00 2001 From: lamson <171004297@qq.com> Date: Mon, 19 Feb 2024 22:10:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0card?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 3 +- src/Notify/Feishu/Message/Base.php | 37 +++--- src/Notify/Feishu/Message/Card.php | 55 ++++++++ src/Notify/Feishu/Message/Textarea.php | 10 +- src/Notify/Feishu/Notify.php | 2 +- src/Notify/README.md | 167 +++++++++++++++++++++++++ 6 files changed, 243 insertions(+), 31 deletions(-) create mode 100644 src/Notify/Feishu/Message/Card.php create mode 100644 src/Notify/README.md diff --git a/composer.json b/composer.json index d07995e..777e515 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "easyswoole/wechat": "^2.0", "ext-json": "*", "ext-iconv": "*", - "ext-mbstring": "*" + "ext-mbstring": "*", + "easyswoole/http-client": "^1.5" }, "autoload": { "psr-4": { diff --git a/src/Notify/Feishu/Message/Base.php b/src/Notify/Feishu/Message/Base.php index 09d40f3..fcfc459 100644 --- a/src/Notify/Feishu/Message/Base.php +++ b/src/Notify/Feishu/Message/Base.php @@ -41,28 +41,23 @@ public function getAtText($text = '') public function getAtArray() { $at = []; - switch ($this->isAtAll) { - case true: - $at = [ - [ - 'tag' => 'at', - 'user_id' => 'all', - 'user_name' => '所有人', - ] + if ($this->isAtAll === true) { + $at = [ + [ + 'tag' => 'at', + 'user_id' => 'all', + 'user_name' => '所有人', + ] + ]; + } else if (is_array($this->isAtAll)) { + foreach ($this->atUserID as $key => $value) { + if (! in_array($key, $this->isAtAll)) continue; + $at[] = [ + 'tag' => 'at', + 'user_id' => $value['id'], + 'user_name' => $value['name'], ]; - break; - case false: - break; - default: - foreach (['atOpenID', 'atUserID'] as $item) { - foreach ($this->{$item} as $id => $name) { - $at[] = [ - 'tag' => 'at', - 'user_id' => $id, - 'user_name' => $name, - ]; - } - } + } } return $at; diff --git a/src/Notify/Feishu/Message/Card.php b/src/Notify/Feishu/Message/Card.php new file mode 100644 index 0000000..8bbc181 --- /dev/null +++ b/src/Notify/Feishu/Message/Card.php @@ -0,0 +1,55 @@ + 'interactive', + 'card' => [ + 'elements' => [ + [ + 'tag' => 'div', + 'text' => [ + 'tag' => 'lark_md', + 'content' => $this->getServerText($this->content), + ], + ], + [ + 'tag' => 'action', + 'actions' => [ + [ + 'tag' => 'button', + 'text' => [ + 'tag' => 'lark_md', + 'content' => $this->getServerText($this->content), + ], + 'url' => 'https://www.baidu.com', + 'type' => 'default', + 'value' => [], + ], + ], + ], + [ + 'tag' => 'div', + 'text' => [ + 'tag' => 'lark_md', + 'content' => $this->getAtText(), + ], + ], + ], + 'header' => [ + 'title' => [ + 'tag' => 'plain_text', + 'content' => '程序异常', + ], + ], + ], + ]; + return $data; + } +} diff --git a/src/Notify/Feishu/Message/Textarea.php b/src/Notify/Feishu/Message/Textarea.php index ffc29e2..b63c8dd 100644 --- a/src/Notify/Feishu/Message/Textarea.php +++ b/src/Notify/Feishu/Message/Textarea.php @@ -6,7 +6,6 @@ class Textarea extends Base { protected $content = ''; - // https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot#%E6%94%AF%E6%8C%81%E5%8F%91%E9%80%81%E7%9A%84%E6%B6%88%E6%81%AF%E7%B1%BB%E5%9E%8B%E8%AF%B4%E6%98%8E public function fullData() { $data = [ @@ -21,19 +20,14 @@ public function fullData() 'tag' => 'text', 'text' => $this->getServerText($this->content), ], - [ - 'tag' => 'at', - 'user_id' => 'all', - 'user_name' => '所有人', - ], ], ], ], ], ], ]; - // $at = $this->getAtArray(); - // $data['content']['post']['zh_cn']['content'][0] = array_merge($at); + $at = $this->getAtArray(); + $data['content']['post']['zh_cn']['content'][0] = array_merge($data['content']['post']['zh_cn']['content'][0], $at); return $data; } } diff --git a/src/Notify/Feishu/Notify.php b/src/Notify/Feishu/Notify.php index c256225..ce726c6 100644 --- a/src/Notify/Feishu/Notify.php +++ b/src/Notify/Feishu/Notify.php @@ -20,7 +20,7 @@ public function __construct(ConfigInterface $Config) } /** - * @document https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot + * @document https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot#%E6%94%AF%E6%8C%81%E5%8F%91%E9%80%81%E7%9A%84%E6%B6%88%E6%81%AF%E7%B1%BB%E5%9E%8B%E8%AF%B4%E6%98%8E * 自定义机器人的频率控制和普通应用不同,为 100 次/分钟,5 次/秒 * @param MessageInterface $message * @return void diff --git a/src/Notify/README.md b/src/Notify/README.md new file mode 100644 index 0000000..3b92450 --- /dev/null +++ b/src/Notify/README.md @@ -0,0 +1,167 @@ +## 简介 + + 可方便快捷的实现通知服务,目前已接入的有钉钉和微信公众号(含测试号) + + + +## 使用场景 + +- 程序异常通知 +- 数据上报到钉钉群 +- 敏感操作通知、操作状态通知 + +## 目录结构 + + src + ├── DingTalk 钉钉实现 + ├── Interface 接口定义 + ├── Wechat 微信实现 + └── Feishu 飞书实现 + +## 开始 + +> composer require wonder-game/es-notify + +#### 配置 + +钉钉 + +```php + 'your dingtalk url', + // 密钥 + 'signKey' => 'your dingtalk sign_key', + + // ... 也可以配置一些自定义属性, 获取方式 Config->getProperty('xx') + ], true); +``` + +微信 + +```php + '', + // 微信公众平台后台配置的 AppSecret + 'appSecret' => '', + // 微信公众平台后台配置的 Token + 'token' => '', + // 点击后跳转地址 + 'url' => 'https://github.com/Joyboo', + // 发送给谁, openid[] + 'toOpenid' => [], + // 注册WeChat实例时追加的配置( 可选参数 ) + 'append' => [] + + // ... 也可以配置一些自定义属性, 获取方式 Config->getProperty('xx') + ], true); +``` + +飞书 + +```php + 'your feishu url', + // 密钥 + 'signKey' => 'your feishu sign_key', + + // ... 也可以配置一些自定义属性, 获取方式 Config->getProperty('xx') + ], true); +``` + +### 注册 + +可注册多个,不限数量,key需要保证唯一,调用通知时需声明此key + +```php +register('dingtalk', $Config); +``` + + + +### 调用通知 + +1. 调用doesOne时第一个参数为注册时的key,第二个参数为消息类 +2. 钉钉支持的消息类型是固定的,都在DingTalk\Message目录 +3. 微信公众号模板消息的每个模板,理论上格式都不同,请自行继承src/WeChat/Message/Base.php 实现struct抽象方法传递模板结构,可参考Warning.php和Notice.php + +```php + 'Joyboo', + // 内容 + 'text' => '真帅', + // @的手机号(可选) + 'atMobiles' => [], + // @的userid(可选) + 'atUserIds' => [], + // 是否@所有人(可选, 默认false) + 'isAtAll' => true + ]); +// 开始发送钉钉消息,key是注册时传入的key +\WonderGame\EsNotify\EsNotify::getInstance()->doesOne('dingtalk', $message); + + + +// 这是一个程序异常的消息示例 +$message = new \WonderGame\EsNotify\WeChat\Message\Warning([ + 'templateId' => '微信模板消息id', + 'file' => '发生异常的文件', + 'line' => '第几行', + 'servername' => '服务器名', + 'message' => 'mesage', + // 微信文本颜色,默认红色 + //'color' => '' +]); +// 开始发送微信消息 +\WonderGame\EsNotify\EsNotify::getInstance()->doesOne('wechat', $message); + +``` + +### 常见问题 + +1. 我想在没注册的情况下,按指定配置实例化然后调用通知可以吗? + +- 当然可以,Show Code + +```php + 'your dingtalk WebHook url', + 'signKey' => 'your dingtalk sign key' + ], true); + +// 第二步: 构造消息类 +$DingTalkMessage = new \WonderGame\EsNotify\DingTalk\Message\Markdown([ + 'title' => '魔镜魔镜,谁是世界上最帅的人?', + 'text' => 'Joyboo无疑', + ]); + +// 然后就可以愉快的调用了 +$DingTalkConfig->getNotifyClass()->does($DingTalkMessage); + +``` + +- 微信同理 + +## TODO + +- [ ] 异常处理 +- [ ] 目前钉钉和微信Config和Message不能混用,所以无法实现doesAll方法,需解决 + +## 相关文档 + +- [钉钉-自定义机器人接入](https://open.dingtalk.com/document/group/custom-robot-access) +- [EasySwoole-微信SDK-模板消息](http://www.easyswoole.com/Components/WeChat2.x/officialAccount/templateMessage.html)