当前位置: 首页 > 后端技术 > PHP

yii2-dingtalk钉钉群机器人

时间:2023-03-29 18:13:10 PHP

说明群机器人是钉钉群的高级扩展功能。群聊机器人可以将第三方服务的信息聚合到群聊中,实现信息的自动化同步。目前大部分机器人添加后,都需要配置Webhook才能正常使用(配置说明见操作过程中的帮助链接)。例如:通过聚合GitHub、GitLab等源码管理服务,实现源码更新同步。通过聚合Trello和JIRA等项目协调服务来同步项目信息。另外,群机器人支持自定义接入Webhook协议,支持更多的可能性。比如可以通过自定义机器人将运维告警聚合到钉钉群,实现提醒功能。安装$composerrequiresaviorlv/yii2-dingtalk-vvvconfiguration在使用这个扩展之前,你需要去SwarmRobots获取相关信息。使用config/main.php配置文件中定义的组件配置信息'components'=>[.....'robot'=>['class'=>'Saviorlv\Dingtalk\Robot','accessToken'=>'xxxxxxxxx'],....]发送文本信息方法:publicfunctionsendTextMsg($content,array$atMobiles=[],$isAtAll=false){}参数:参数类型必须表明contentString是内容messageatMobilesArrayisnot@Theperson'smobilenumberisAtAllboolNo@Everyone:true,otherwise:falseExample:$response=Yii::$app->robot->sendTextMsg("上下班需要忘记登录",[136*****134,136*****132],假);SendLink链接方法:publicfunctionsendLinkMsg($title,$text,$picUrl='',$messageUrl){}参数:参数参数类型必须注明titleString为消息标题textString为消息内容。如果太长,只会部分显示messageUrlString是点击消息跳转到的URL。picUrlString不是图片URL示例:$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");发送MarkDown消息方法:publicfunctionsendMarkdownMsg($title,$content,array$atMobiles=[],$isAtAll=false){}参数:参数必填类型描述titletruestring首屏会话contenttruestringmarkdown格式消息显示的显示内容,otherwise:falseExample:$response=Yii::$app->robot->sendMarkdownMsg("上班签到","###有些同志就是上班不签到,`QAQ`",[136*****134,136*****132],假);整体跳转ActionCard类型方法:publicfunctionsendActionCardMsg($title,$contentt,$singleURL,$hideAvatar=0,$btnOrientation=0,$singleTitle='Readtheoriginaltext'){}parameter:parametermandatorytypedescriptiontitletruestring首屏会话显示的显示内容contenttruestringmarkdown格式消息singleTitletruestringscheme一个按钮(btns设置此项和singleURL后无效)singleURLtruestring点击singleTitle按钮触发URLbtnOrientationfalsestring0-按钮竖排,1-按钮横排hideAvatarfalsestring0-正常发件人头像,1-隐藏发件人头像例子:$response=Yii::$app->robot->sendActionCardMsg("上班签到","有些同志就是上班不签到,QAQ","https://github.com/saviorlv?tab=repositories",0,0,"你看不见");独立跳转ActionCard类型方法:publicfunctionsendSingleActionCardMsg($title,$content,$hideAvatar=0,$btnOrientation=0,array$btns=[]){}参数:参数必填类型说明titletruestring显示首屏会话显示的内容contenttruestringmarkdown格式消息btnstruearray按钮信息:title-buttonscheme,actionURL-点击按钮触发的URLbtnOrientationfalsestring0-button垂直排列,1-button水平排列hideAvatarfalsestring0-普通发件人头像,1-隐藏发件人头像示例:$response=Yii::$app->robot->sendSingleActionCardMsg("上班签到","有些同志上班不签到,QAQ",0,1,[["title"=>"好内容","actionURL"=>"https://www.dingtalk.com/"],["title"=>"不感兴趣","actionURL"=>"https://www.dingtalk.com/"]]);FeedCard类型方法:publicfunctionsendFeedCardMsg(array$links=[]){}参数:参数必填类型描述titletruestring单条消息正文messageURLtruestring点击单条消息跳转到链接picURLtruestring单条消息后面图片的URL实例:$response=Yii::$app->robot->sendFeedCardMsg([["title"=>"时代的火车向前开","messageURL"=>"https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97=2&rccene09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI","picURL"=>"http://b.hiphotos.baidu.com/image/pic/item/f603918fa0ec08fa98d87c8654ee3"d8054ee3,[“标题”=>"时代列车前行","messageURL"=>"https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRinsEltJAndltis1&srcid=09189AnRinsEltJAndltis1=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI","picURL"=>"http://b.hiphotos.baidu.com/image/pic/item/f603918fa0ec08fa98d87c8054ee3d6d55fbda39.jpg"],]);参考钉钉定制机器人