注意:
需事先再钉钉开放后台,新建好应用,并获取 AgentId , AppKey 和 AppSecret 。并且在应用设置离添加你的请求ip,白名单。
用到的请求接口如下
1.获取token
1 |
https://open.dingtalk.com/document/orgapp-server/obtain-orgapp-token |
2.获取相关部门列表
1 2 3 4 5 |
#获取企业下所有部门列表 https://open.dingtalk.com/document/orgapp-server/obtains-the-list-of-all-departments-of-an-enterprise #获取部门列表 https://open.dingtalk.com/document/orgapp-server/obtain-the-department-list-v2 |
3.获取部门下所有员工信息
1 |
https://open.dingtalk.com/document/orgapp-server/queries-the-simple-information-of-a-department-user |
获取角色列表(本文暂未用到)
1 |
https://open.dingtalk.com/document/orgapp-server/obtains-a-list-of-enterprise-roles |
4.发送消息
1 |
https://open.dingtalk.com/document/orgapp-server/message-types-and-data-format |
本人完整脚本如下(python 依此稍作改动即可)
dingtalk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
<?php date_default_timezone_set(PRC); header("Content-type:text/html;charset=utf-8"); $val = array(); if($_POST){ $val['msgtype']= $_POST['msgtype']; $val['title']= $_POST['title']; $val['message']= $_POST['message']; $val['dumpurl']= $_POST['dumpurl']; }else{ $val['msgtype']= $_GET['msgtype']; $val['title']= $_GET['title']; $val['message']= $_GET['message']; $val['dumpurl']= $_GET['dumpurl']; } function request_by_curl($webhook,$post_string,$time) { $url = $webhook; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_string); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($curl); curl_close($curl); return $res; } function https_request ($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $out = curl_exec($ch); curl_close($ch); return json_decode($out,true); } //获取access_token function getNewToken($corpid,$corpsecret){ $url = "https://oapi.dingtalk.com/gettoken?appkey={$corpid}&appsecret={$corpsecret}"; $access_token_Arr = https_request($url); return $access_token_Arr['access_token']; } function getToken(){ $corpid='dingu5zirnw'; $corpsecret='BzZf7gMAShakc8FJ-CkQSnX9pDet4WVcZ8RSWu46eMXW'; // 读取access_token include './access_token.php'; // 判断是否过期 if (time() > $access_token['expires']){ //如果已经过期就得重新获取并缓存 $access_token = array(); $access_token['access_token'] = getNewToken($corpid,$corpsecret); $access_token['expires']=time()+7000; $arr = '<?php'.PHP_EOL.'$access_token = '.var_export($access_token,true).';'.PHP_EOL.'?>'; $arrfile = fopen("./access_token.php","w"); fwrite($arrfile,$arr); fclose($arrfile); // 返回当前的access_token return $access_token['access_token']; }else{ return $access_token['access_token']; } } function send_web_dingtalk($message="详细内容!",$dumpurl="https://gitlab.xiaoziyixue.com",$msgtype="news",$title="CI/CD消息提醒"){ $time = time(); $webhook = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=".getToken(); if($msgtype == "action_card"){ $data= array( "userid_list" => "0936422037967", "agent_id" => 26762545, "msg" => array ( "msgtype" => "action_card", "action_card" => array ( "title" => $title, "markdown" => $message, "single_title" => "查看详情", "single_url" => $dumpurl ) ) ); }elseif($msgtype == "markdown"){ $data= array( "userid_list" => "0936422937967", "agent_id" => 26762545, "msg" => array ( "msgtype" => "markdown", "markdown" => array ( "title" => $title, "text" => $message ) ) ); }else{ $data= array( "userid_list" => "0936422937967", "agent_id" => 26762545, "msg" => array ( "msgtype" => "text", "markdown" => array ( "title" => $title, "text" => $message ) ) ); } $data_string = json_encode($data); print_r($data_string); $result = request_by_curl($webhook, $data_string,$time); echo $result; } if(!empty($val)){ $msgtype = $val['msgtype']; $title = $val['title']; $message = $val['message']; $dumpurl = $val['dumpurl']; if(!empty($msgtype)){ send_web_dingtalk($message,$dumpurl,$msgtype); }else{ send_web_dingtalk($message,$dumpurl); } } ?> |
access_token.php (给写权限)
1 2 3 4 5 6 7 |
<?php $access_token = array ( 'access_token' => 'e4b8d6b63b5fbea325aeeb62', 'expires' => 1655107091, ); ?> |
测试(postman):
结果:
other :新建应用参考如下
1 |
https://wenku.baidu.com/view/4f44e1d6f51fb7360b4c2e3f5727a5e9856a27f6.html |
- 本文固定链接: https://www.yoyoask.com/?p=8632
- 转载请注明: shooter 于 SHOOTER 发表