利用釘釘機器人在專案群裡傳送通知
阿新 • • 發佈:2019-02-17
獲取自定義機器人webhook
- 在機器人管理頁面選擇“自定義”機器人,輸入機器人名字並選擇要傳送訊息的群。如果需要的話,可以為機器人設定一個頭像。點選“完成新增”。
- 點選“複製”按鈕,即可獲得這個機器人對應的Webhook地址,其格式如下
1 | https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx |
使用自定義機器人
獲取到Webhook地址後,使用者可以使用任何方式向這個地址發起HTTP POST 請求,即可實現給該群組傳送訊息。注意,發起POST請求時,必須將字符集編碼設定成UTF-8。
當前自定義機器人支援文字(text)、連線(link)、markdown(markdown)三種訊息型別,大家可以根據自己的使用場景選擇合適的訊息型別,達到最好的展示樣式。具體的訊息型別參考下一節內容。
自定義機器人傳送訊息時,可以通過手機號碼指定“被@人列表”。在“被@人列表”裡面的人員,在收到該訊息時,會有@訊息提醒(免打擾會話仍然通知提醒,首屏出現“有人@你”)
訊息型別及資料格式
文字型別
123456789101112 | { "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 |
測試自定義機器人
通過下面方法,可以快速驗證自定義機器人是否可以正常工作:
- 使用命令列工具curl(版本用最新:7.29.0)
12345678 | -H'Content-Type: application/json'\ -d ' {"msgtype":"text", "text": { "content":"我就是我, 是不一樣的煙火" } }' |
- Java程式測試
12345678910111213141516171819202122232425262728293031 | 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 { 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); } }} |
- PHP程式測試
12345678910111213141516171819202122232425262728 | <?php 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; } $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