參考文檔:https://work.weixin.qq.com/api/doc/90000/90135/90250
-
進入https://work.weixin.qq.com,點擊【立即注冊】,填寫信息注冊企業
-
點擊通訊錄,添加一個子部門
我在這里注冊了一個叫“微信告警”的企業,下面有一個子部門叫“測試”
記住部門“測試”的部門id是2,之后要用到 -
點擊【應用管理】——》【創建一個應用】
填寫好相關內容,創建應用,記住AgentId和Secret
如果需要讓其他人也看到,需要添加可見范圍
-
點擊我的企業,記住企業ID
-
測試連通性
打開企業微信的接口調試頁面https://open.work.weixin.qq.com/wwopen/devtool/interface/combine
填寫企業id和Secret
能夠正確獲取到token就是正常的
6. 編寫shell腳本
#!/bin/bash
# 應用ID
AgentID=1000002
# 企業ID
CropID=xxxxxxxxxxxxxx
# 密鑰
Secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# 獲取token
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F \" '{print $10}')
# 請求內容,注意是json格式
body='{
"touser": "@all",
"toparty": "2",
"msgtype": "text",
"agentid": 1000002,
"text": {
"content": "床前明月光\n疑是地上霜\n舉頭望明月\n低頭思故鄉"
},
"safe": 0,
"enable_id_trans": 0,
"enable_duplicate_check": 0
}'
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
# 發送消息
/usr/bin/curl --data-ascii "$body" $PURL
tourser處填寫收信人id,即企業微信賬號
多個收信人用|
隔開,@all
表示部門所有人。
toparty處填寫部門id,多個部門用|
隔開。