您的位置 首页 php

使用钉钉自定义机器人推送消息(PHP版)

使用钉钉自定义机器人推送消息(PHP版)

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

例如:

  • 通过聚合GitHub, GitLab 源码 管理服务,实现源码更新同步。
  • 通过聚合Trello,JIRA等项目协调服务,实现项目信息同步。
  • 群机器人支持Webhook协议的自定义接入。例如:你可将运维报警通过自定义机器人聚合到钉钉群实现提醒功能。

这里我们采用最后一种来实现

2 机器人发送消息频率限制

消息发送太频繁会严重影响群成员的使用体验,大量发消息的场景(譬如系统监控报警)可以将这些信息进行整合,通过 markdown 消息以摘要的形式发送到群里。

每个机器人每分钟最多发送20条。如果超过20条,会限流10分钟。
 

3 PC客户端配置入口

登录钉钉PC客户端(请升级至最新版),操作入口:

窗口右上角点击【头像】,进入”机器人管理“, 可对所有机器人进行统一管理。

进入一个钉钉群,在群的顶部功能栏中,点击【群设置】,进入菜单可以看到【群机器人】的入口,点击进入“群机器人”的管理面板后,可以进行添加、编辑和删除群机器人的操作。

使用钉钉自定义机器人推送消息(PHP版)

我们这里使用最后一个webhook

4 获取自定义机器人webhook

步骤一 ,在机器人管理页面选择“自定义”机器人,输入机器人名字并选择要发送消息的群。如果需要的话,可以为机器人设置一个头像。点击“完成添加”,完成后会生成Hook 地址 ,如下图:

使用钉钉自定义机器人推送消息(PHP版)

步骤二 ,点击“复制”按钮,即可获得这个机器人对应的Webhook地址,其格式如下:

 

5 使用自定义机器人

  • 获取到Webhook地址后,用户可以向这个地址发起HTTP POST 请求,即可实现给该钉钉群发送消息。注意,发起POST请求时,必须将字符集编码设置成UTF-8。
  • 当前自定义机器人支持文本 (text)、链接 (link)、markdown(markdown)、ActionCard、FeedCard消息类型,大家可以根据自己的使用场景选择合适的消息类型,达到最好的展示样式。
  • 自定义机器人发送消息时,可以通过手机号码指定“被@人列表”。在“被@人列表”里面的人员收到该消息时,会有@消息提醒(免打扰会话仍然通知提醒,首屏出现“有人@你”)。
  • 当前机器人尚不支持应答机制 (该机制指的是群里成员在聊天@机器人的时候,钉钉回调指定的服务地址,即Outgoing机器人)。

6 php 实现代码

​
<?php
/**
 * php 使用钉钉机器人发送消息.
 * User: Administrator
 * Date: 2019/8/29
 * Time: 23:40
 */​
function request_by_curl($remote_server, $post_string)
{
 $ ch  = curl_init();
 curl_setopt($ch, CURLOPT_URL, $remote_server);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 // 不用开启curl证书验证
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 $data = curl_exec($ch);
 //$info = curl_getinfo($ch);
 //var_dump($info);
 curl_close($ch);
 return $data;
}
​
$webhook = "";
​
// text类型
$textString = json_encode([
 'msgtype' => 'text',
 'text' => [
 "content" => "我就是我, 是不一样的烟火@156xxxx8827"
 ],
 'at' => [
 'atMobiles' => [
 "156xxxx8827",
 "189xxxx8325"
 ],
 'isAtAll' => false
​
 ]
]);
​
// link类型
$textString = json_encode([
 "msgtype" => "link",
 "link" => [
 "text" => "这个即将发布的新版本,创始人陈航(花名“无招”)称它为“红树林”。
而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是“红树林”?",
 "title" => "时代的火车向前开",
 "picUrl" => "",
 "messageUrl" => "",
 ]
]);
​
// markdown类型
$textString = json_encode([
 "msgtype" => "markdown",
 "markdown" => [
 "title" => "杭州天气",
 "text" => "#### 杭州天气 @156xxxx8827n" .
 "> 9度,西北风1级,空气良89,相对温度73%nn" .
 "> ![screenshot](" .
 "> ###### 10点20分发布 [天气]( n"
 ],
 "at" => [
 "atMobiles" => [
 "156xxxx8827",
 "189xxxx8325"
 ],
 "isAtAll" => false
 ]
]);
​
// 整体跳转ActionCard类型
$textString = json_encode([
 "actionCard" => [
 "title" => "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",
 "text" => "![screenshot](@lADOpwk3K80C0M0FoA) 
 ### 乔布斯 20 年前想打造的苹果咖啡厅 
 Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
 "hideAvatar" => "0",
 "btnOrientation" => "0",
 "singleTitle" => "阅读全文",
 "singleURL" => ""
 ],
 "msgtype" => "actionCard"
]);
​
​
// 独立跳转ActionCard类型
$textString = json_encode([
 "actionCard" => [
 "title" => "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",
 "text" => "![screenshot](@lADOpwk3K80C0M0FoA) 
 ### 乔布斯 20 年前想打造的苹果咖啡厅 
 Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
 "hideAvatar" => "0",
 "btnOrientation" => "0",
 "btns" => [
 [
 "title" => "内容不错",
 "actionURL" => ""
 ],
 [
 "title" => "不感兴趣",
 "actionURL" => ""
 ]
 ]
 ],
 "msgtype" => "actionCard"
]);
​
// FeedCard类型
$textString = json_encode([
 "feedCard" => [
 "links" => [
 [
 "title" => "时代的火车向前开1",
 "messageURL" => "",
 "picURL" => ""
 ],
 [
 "title" => "时代的火车向前开2",
 "messageURL" => "",
 "picURL" => ""
 ]
 ]
 ],
 "msgtype" => "feedCard"
]);
​
​
$result = request_by_curl($webhook, $textString);
echo $result;
​
> 最终效果
​
 
使用钉钉自定义机器人推送消息(PHP版)

使用钉钉自定义机器人推送消息(PHP版)

使用钉钉自定义机器人推送消息(PHP版)

使用钉钉自定义机器人推送消息(PHP版)

使用钉钉自定义机器人推送消息(PHP版)

字段格式和含义请参考官方:

#/serverapi3/iydd5h

文章来源:智云一二三科技

文章标题:使用钉钉自定义机器人推送消息(PHP版)

文章地址:https://www.zhihuclub.com/80434.shtml

关于作者: 智云科技

热门文章

评论已关闭

40条评论

  1. Our Pharmacy Coupons Cialis also requires sexual stimulation in order to work for erectile dysfunction

  2. The concentration of TDL and DPX were determined spectrophotometrically at 284 and 291 nm, respectively using a UV-Vis spectrophotometer Jenway 7315, Bibby scientific Limited, Stone, Staffordshire, UK Chi fa was pondering the meaning of this sentence, and was not in a hurry to speak

  3. 5 in men with mild, moderate, and severe LUTS, respectively; severe LUTS resulted in an OR of 7 And if sex doesn t happen quite when you d planned on it, no big deal

  4. 2 ng l 158 E1, E2, E3 and conjugates Natural water SPE Oasis HLB cartridge Column switching HILIC RP HPLC ion trap MS MS LOD E1, E2, E3 0 The plate was then removed and an equal volume of Apo ONE Reagent added to each well

  5. We are all grateful to have this opportunity to meet Carla and that we are able to do something so wonderful for such a wonderful person

  6. Lowering the price lowers the risk for people This finding is in line with the results of the NSABP P1 trial, where smoking was both a risk factor for breast cancer and an inhibitory factor of tamoxifen efficacy compared with former or never smokers 22

  7. In some embodiments, the fusion protein contains a sequence of a transcription repressor, and the fusion protein is capable of repressing transcription of a gene in the nucleus of the eukaryotic cell

  8. Despite survivin being a promising molecular target for anticancer treatment, it is widely accepted that survivin is only a semi druggable target Keep posting such kind of info on your site

  9. anadoil bimatoprosta onde comprar The authors interviewed 536 cigarette smokers in the Australian state of Victoria during November 2012 when plain packs were already available 72

  10. dexamethasone glucophage xr 750 precio pami There may be enough votes in the House for passage of a clean bill, according to some analysts

  11. com 20 E2 AD 90 2011 20Commandements 20Volley 20Viagra 20 20Strong 20Viagra strong viagra But I just don t think motherhood is the be all and end all of being a woman, and suspect that those of us who don t have children may well enjoy a broader and more fulfilled existence

  12. Double blind, placebo controlled, randomised study of single dose effects of ADAPT 232 on cognitive functions

  13. Severe HF NYHA functional class III IV to increase survival, and to reduce the need for HF hospitalization

  14. To our knowledge, this is the first study to test mixtures of pesticides for additive effects not only at the hERО± but also at the hERОІ level

  15. Spongiotic dermatitis Contact dermatitis Dyshidrotic eczema Nummular dermatitis Stasis dermatitis ID reaction Pityriasis rosea Seborrheic dermatitis Spongiotic form of pigmenting purpuric dermatosis

  16. Gheorghiade M, Konstam MA, Burnett JC Jr, Grinfeld L, Maggioni AP, Swedberg K, Udelson JE, Zannad F, Cook T, Ouyang J, Zimmer C, Orlandi C, Efficacy of Vasopressin Antagonism in Heart Failure Outcome Study With Tolvaptan I 2007 Short term clinical effects of tolvaptan, an oral vasopressin antagonist, in patients hospitalized for heart failure the EVEREST Clinical Status Trials azithromycin, ciprofloxacin inhibit specific CYP450 iso enzymes Table 3, altering warfarin pharmacokinetics and increase PT INR values and risk of hemorrhage when combined with warfarin 22, 34

  17. Subsequent dosing intervals should be determined according to need for analgesia and response to previous doses

  18. Adverse events more common in early dialysis group Adverse events occurred in 23 in the accelerated strategy and 16 The most promising candidates for this application are selective estrogen receptor modulators SERM, which exhibit tissue specific estrogen agonist and antagonist effects 6

  19. They were incredibly unorganized and made a lot of mistakes but I truly think think this is because they were overworked and understaffed

  20. For agents reported to be associated with steatohepatitis during the 1990s, causality has been difficult to prove, 369 particularly because NASH is a common disorder among patients with obesity, insulin resistance, or metabolic syndrome see Chapter 85

网站地图