yii2-dingtalk 钉钉群机器人

时间:2022-01-09 08:51:57

说明

群机器人是钉钉群的高级扩展功能。群机器人可以将第三方服务的信息聚合到群聊中,实现自动化的信息同步。目前,大部分机器人在添加后,还需要进行Webhook配置,才可正常使用(配置说明详见操作流程中的帮助链接)。

例如:通过聚合GitHub,GitLab等源码管理服务,实现源码更新同步。通过聚合Trello,JIRA等项目协调服务,实现项目信息同步。

另外,群机器人支持Webhook协议的自定义接入,支持更多可能性,例如:你可将运维报警通过自定义机器人聚合到钉钉群实现提醒功能。

安装

$ composer require saviorlv/yii2-dingtalk -vvv


<h2>配置</h2>
<p>在使用本扩展之前,你需要去 <a href="https://open-doc.dingtalk.com/microapp/serverapi2/nr29om" rel="nofollow noreferrer">群机器人</a> 获取相关信息。</p>
<h2>使用</h2>
<blockquote>在config/main.php配置文件中定义component配置信息</blockquote>

'components' => [
.....
'robot' => [
'class' => 'Saviorlv\Dingtalk\Robot',
'accessToken' => 'xxxxxxxxx'
],
....]


<h3>发送 <code>Text</code> 信息</h3>
<p>方法:</p>

public function sendTextMsg($content, array $atMobiles = [], $isAtAll = false){}


<p>参数:</p>
<table>
<thead><tr>
<th>参数</th>
<th>参数类型</th>
<th>必须</th>
<th>说明</th>
</tr></thead>
<tbody>
<tr>
<td>content</td>
<td>String</td>
<td>是</td>
<td>消息内容</td>
</tr>
<tr>
<td>atMobiles</td>
<td>Array</td>
<td>否</td>
<td>被@人的手机号</td>
</tr>
<tr>
<td>isAtAll</td>
<td>bool</td>
<td>否</td>
<td>@所有人时:true,否则为:false</td>
</tr>
</tbody>
</table>
<p>实例:</p>

$response = Yii::$app->robot->sendTextMsg(
"必要忘记上下班打卡",
[
136*****134,
136*****132
],
false
);


<h3>发送 <code>Link</code> 链接</h3>
<p>方法:</p>

public function sendLinkMsg($title, $text, $picUrl = '', $messageUrl){}


<p>参数:</p>
<table>
<thead><tr>
<th>参数</th>
<th>参数类型</th>
<th>必须</th>
<th>说明</th>
</tr></thead>
<tbody>
<tr>
<td>title</td>
<td>String</td>
<td>是</td>
<td>消息标题</td>
</tr>
<tr>
<td>text</td>
<td>String</td>
<td>是</td>
<td>消息内容。如果太长只会部分展示</td>
</tr>
<tr>
<td>messageUrl</td>
<td>String</td>
<td>是</td>
<td>点击消息跳转的URL</td>
</tr>
<tr>
<td>picUrl</td>
<td>String</td>
<td>否</td>
<td>图片URL</td>
</tr>
</tbody>
</table>
<p>实例:</p>

$response = Yii::$app->robot->sendLinkMsg(
"上下班打卡",
"有些同志上下班就是不打卡,QAQ",
"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1536321778370&di=46a7abc85f6fe1de8df8fbdc5b95d68d&imgtype=0&src=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201407%2F21%2F20140721224026_KU3GA.thumb.700_0.jpeg",
"https://github.com/saviorlv?tab=repositories"
);


<h3>发送 <code>MarkDown</code> 消息</h3>
<p>方法:</p>

public function sendMarkdownMsg($title, $content, array $atMobiles = [], $isAtAll = false){}


<p>参数:</p>
<table>
<thead><tr>
<th>参数</th>
<th>必选</th>
<th>类型</th>
<th>说明</th>
</tr></thead>
<tbody>
<tr>
<td>title</td>
<td>true</td>
<td>string</td>
<td>首屏会话透出的展示内容</td>
</tr>
<tr>
<td>content</td>
<td>true</td>
<td>string</td>
<td>markdown格式的消息</td>
</tr>
<tr>
<td>atMobiles</td>
<td>Array</td>
<td>否</td>
<td>被@人的手机号(在text内容里要有@手机号)</td>
</tr>
<tr>
<td>isAtAll</td>
<td>bool</td>
<td>否</td>
<td>@所有人时:true,否则为:false</td>
</tr>
</tbody>
</table>
<p>实例:</p>

$response = Yii::$app->robot->sendMarkdownMsg(
"上下班打卡",
" ###有些同志上下班就是不打卡,QAQ",
[
136*****134,
136*****132
],
false
);


<h3>整体跳转 <code>ActionCard</code>类型</h3>
<p>方法:</p>

public function sendActionCardMsg($title, $content, $singleURL, $hideAvatar = 0, $btnOrientation = 0, $singleTitle = '阅读原文'){}


<p>参数:</p>
<table>
<thead><tr>
<th>参数</th>
<th>必选</th>
<th>类型</th>
<th>说明</th>
</tr></thead>
<tbody>
<tr>
<td>title</td>
<td>true</td>
<td>string</td>
<td>首屏会话透出的展示内容</td>
</tr>
<tr>
<td>content</td>
<td>true</td>
<td>string</td>
<td>markdown格式的消息</td>
</tr>
<tr>
<td>singleTitle</td>
<td>true</td>
<td>string</td>
<td>单个按钮的方案。(设置此项和singleURL后btns无效)</td>
</tr>
<tr>
<td>singleURL</td>
<td>true</td>
<td>string</td>
<td>点击singleTitle按钮触发的URL</td>
</tr>
<tr>
<td>btnOrientation</td>
<td>false</td>
<td>string</td>
<td>0-按钮竖直排列,1-按钮横向排列</td>
</tr>
<tr>
<td>hideAvatar</td>
<td>false</td>
<td>string</td>
<td>0-正常发消息者头像,1-隐藏发消息者头像</td>
</tr>
</tbody>
</table>
<p>实例:</p>

$response = Yii::$app->robot->sendActionCardMsg(
"上下班打卡",
" 有些同志上下班就是不打卡,QAQ",
"https://github.com/saviorlv?tab=repositories",
0,
0,
"你看不看"
);


<h3>独立跳转 <code>ActionCard</code> 类型</h3>
<p>方法:</p>

public function sendSingleActionCardMsg($title, $content, $hideAvatar = 0, $btnOrientation = 0, array $btns=[]){}


<p>参数:</p>
<table>
<thead><tr>
<th>参数</th>
<th>必选</th>
<th>类型</th>
<th>说明</th>
</tr></thead>
<tbody>
<tr>
<td>title</td>
<td>true</td>
<td>string</td>
<td>首屏会话透出的展示内容</td>
</tr>
<tr>
<td>content</td>
<td>true</td>
<td>string</td>
<td>markdown格式的消息</td>
</tr>
<tr>
<td>btns</td>
<td>true</td>
<td>array</td>
<td>按钮的信息:title-按钮方案,actionURL-点击按钮触发的URL</td>
</tr>
<tr>
<td>btnOrientation</td>
<td>false</td>
<td>string</td>
<td>0-按钮竖直排列,1-按钮横向排列</td>
</tr>
<tr>
<td>hideAvatar</td>
<td>false</td>
<td>string</td>
<td>0-正常发消息者头像,1-隐藏发消息者头像</td>
</tr>
</tbody>
</table>
<p>实例:</p>

$response = Yii::$app->robot->sendSingleActionCardMsg(
"上下班打卡",
" 有些同志上下班就是不打卡,QAQ",
0,
1,
[
[
"title"=> "内容不错",
"actionURL"=> "https://www.dingtalk.com/"
],
[
"title"=> "不感兴趣",
"actionURL"=> "https://www.dingtalk.com/"
]
]
);


<h3>
<code>FeedCard</code> 类型</h3>
<p>方法:</p>

public function sendFeedCardMsg(array $links=[]){}


<p>参数:</p>
<table>
<thead><tr>
<th>参数</th>
<th>必选</th>
<th>类型</th>
<th>说明</th>
</tr></thead>
<tbody>
<tr>
<td>title</td>
<td>true</td>
<td>string</td>
<td>单条信息文本</td>
</tr>
<tr>
<td>messageURL</td>
<td>true</td>
<td>string</td>
<td>点击单条信息到跳转链接</td>
</tr>
<tr>
<td>picURL</td>
<td>true</td>
<td>string</td>
<td>单条信息后面图片的URL</td>
</tr>
</tbody>
</table>
<p>实例:</p>

$response = Yii::$app->robot->sendFeedCardMsg([
[
"title"=> "时代的火车向前开",
"messageURL"=> "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
"picURL"=> "http://b.hiphotos.baidu.com/image/pic/item/f603918fa0ec08fa98d87c8054ee3d6d55fbda39.jpg"
],[
"title"=> "时代的火车向前开",
"messageURL"=> "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
"picURL"=> "http://b.hiphotos.baidu.com/image/pic/item/f603918fa0ec08fa98d87c8054ee3d6d55fbda39.jpg"
],
]);
```

参考

原文地址:https://segmentfault.com/a/1190000016351840