Skip to content

Commit

Permalink
feat: 增加card
Browse files Browse the repository at this point in the history
  • Loading branch information
linkunyuan committed Feb 19, 2024
1 parent 0552041 commit 36fd603
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 31 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"easyswoole/wechat": "^2.0",
"ext-json": "*",
"ext-iconv": "*",
"ext-mbstring": "*"
"ext-mbstring": "*",
"easyswoole/http-client": "^1.5"
},
"autoload": {
"psr-4": {
Expand Down
37 changes: 16 additions & 21 deletions src/Notify/Feishu/Message/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
55 changes: 55 additions & 0 deletions src/Notify/Feishu/Message/Card.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace WonderGame\EsUtility\Notify\Feishu\Message;

class Card extends Base
{
protected $content = '';

public function fullData()
{
$data = [
'msg_type' => '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;
}
}
10 changes: 2 additions & 8 deletions src/Notify/Feishu/Message/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Notify/Feishu/Notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
167 changes: 167 additions & 0 deletions src/Notify/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
## 简介

可方便快捷的实现通知服务,目前已接入的有钉钉和微信公众号(含测试号)



## 使用场景

- 程序异常通知
- 数据上报到钉钉群
- 敏感操作通知、操作状态通知

## 目录结构

src
├── DingTalk 钉钉实现
├── Interface 接口定义
├── Wechat 微信实现
└── Feishu 飞书实现

## 开始

> composer require wonder-game/es-notify
#### 配置

钉钉

```php
<?php
$DingTalkConfig = new \WonderGame\EsNotify\DingTalk\Config([
// 钉钉WebHook url
'url' => 'your dingtalk url',
// 密钥
'signKey' => 'your dingtalk sign_key',

// ... 也可以配置一些自定义属性, 获取方式 Config->getProperty('xx')
], true);
```

微信

```php
<?php
$WeChatConfig = new \WonderGame\EsNotify\WeChat\Config([
// 微信公众平台后台的 appid
'appId' => '',
// 微信公众平台后台配置的 AppSecret
'appSecret' => '',
// 微信公众平台后台配置的 Token
'token' => '',
// 点击后跳转地址
'url' => 'https://github.com/Joyboo',
// 发送给谁, openid[]
'toOpenid' => [],
// 注册WeChat实例时追加的配置( 可选参数 )
'append' => []

// ... 也可以配置一些自定义属性, 获取方式 Config->getProperty('xx')
], true);
```

飞书

```php
<?php
$FeishuConfig = new \WonderGame\EsNotify\Feishu\Config([
// 钉钉WebHook url
'url' => 'your feishu url',
// 密钥
'signKey' => 'your feishu sign_key',

// ... 也可以配置一些自定义属性, 获取方式 Config->getProperty('xx')
], true);
```

### 注册

可注册多个,不限数量,key需要保证唯一,调用通知时需声明此key

```php
<?php
// Config为配置类
\WonderGame\EsNotify\EsNotify::getInstance()->register('dingtalk', $Config);
```



### 调用通知

1. 调用doesOne时第一个参数为注册时的key,第二个参数为消息类
2. 钉钉支持的消息类型是固定的,都在DingTalk\Message目录
3. 微信公众号模板消息的每个模板,理论上格式都不同,请自行继承src/WeChat/Message/Base.php 实现struct抽象方法传递模板结构,可参考Warning.php和Notice.php

```php
<?php

// 这是一个钉钉Markdown消息示例
$message = new \WonderGame\EsNotify\DingTalk\Message\Markdown([
//消息标题
'title' => '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
<?php
// 第一步: 构造配置类
$DingTalkConfig = new \WonderGame\EsNotify\DingTalk\Config([
// 动态传入你的配置
'url' => '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)

0 comments on commit 36fd603

Please sign in to comment.