1
|
https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx
|
获取到Webhook地址后,用户可以使用任何方式向这个地址发起HTTP POST 请求,即可实现给该群组发送消息。注意,发起POST请求时,必须将字符集编码设置成UTF-8。
当前自定义机器人支持文本(text)、连接(link)、markdown(markdown)三种消息类型,大家可以根据自己的使用场景选择合适的消息类型,达到最好的展示样式。具体的消息类型参考下一节内容。
自定义机器人发送消息时,可以通过手机号码指定“被@人列表”。在“被@人列表”里面的人员,在收到该消息时,会有@消息提醒(免打扰会话仍然通知提醒,首屏出现“有人@你”)
1
2
3
4
5
6
7
8
9
10
11
12
|
{
"msgtype":"text",
"text": {
"content":"我就是我, @1825718XXXX 是不一样的烟火"
},
"at": {
"atMobiles": [
"1825718XXXX"
],
"isAtAll":false
}
}
|
参数 | 必选 | 类型 | 说明 |
---|---|---|---|
msgtype | true | string | 此消息类型为固定text |
content | true | string | 消息内容 |
atMobiles | false | string | 被@人的手机号 |
isAtAll | false | bool | @所有人时:true,否则为:false |
通过下面方法,可以快速验证自定义机器人是否可以正常工作:
1
2
3
4
5
6
7
8
|
curl' https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx'\
-H'Content-Type: application/json'\
-d '
{"msgtype":"text",
"text": {
"content":"我就是我, 是不一样的烟火"
}
}'
|
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
|
importorg.apache.http.HttpResponse;
importorg.apache.http.HttpStatus;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.methods.HttpPost;
importorg.apache.http.entity.StringEntity;
importorg.apache.http.impl.client.HttpClients;
importorg.apache.http.util.EntityUtils;
publicclassChatbotSend {
publicstaticString WEBHOOK_TOKEN =" https://oapi.dingtalk.com/robot/send?access_token=xxxxxx";
publicstaticvoidmain(String args[])throwsException{
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost =newHttpPost(WEBHOOK_TOKEN);
httppost.addHeader("Content-Type","application/json; charset=utf-8");
String textMsg ="{ "msgtype": "text", "text": {"content": "我就是我, 是不一样的烟火"}}";
StringEntity se =newStringEntity(textMsg,"utf-8");
httppost.setEntity(se);
HttpResponse response = httpclient.e x e cute(httppost);
if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
String result= EntityUtils.toString(response.getEntity(),"utf-8");
s y s t e m.out.println(result);
}
}
}
|
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
|
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_e x e c($ch);
curl_close($ch);
return$data;
}
$webhook =" https://oapi.dingtalk.com/robot/send?access_token=xxxxxx";
$message="我就是我, 是不一样的烟火";
$data = array ('msgtype'=>'text','text'=> array ('content'=> $message));
$data_string = json_encode($data);
$result = request_by_curl($webhook, $data_string);
echo $result;
?>
|
编写shell脚本
vi notice.sh
chmod 777 notice.sh
./notice.sh 测试脚本是否正确
curl 'https://oapi.dingtalk.com/robot/send?access_token=79803a12eb673181731459d305b2a42f081f20371b1c14' \
-H 'Content-Type: application/json' \
-d '
{"msgtype": "text",
"text": {
"content": "项目组成员请下班前提交代码和文档到SVN"
}
}'
在linux中 yum install crontabs
crontab -e写入
0 17 * * 1-5 /home/tanlx/Dnotice.sh 每天17点运行
然后crontab -l查看
0 17 * * 1-5 /home/tanlx/Dnotice.sh