註冊企業號:html
https://qy.weixin.qq.com/cgi-bin/loginpage node
註冊成功後記住CORPID:
python
到通信錄添加一個成員json
個人企業,通信裏面添加一個管理員用戶api
到權限管理添加一個管理員微信
個人企業—-》權限管理—-》添加一個管理員,就是本身dom
而後建立一個應用python2.7
建立以後,咱們就有了:Agentid和Secret的兩個值,後面會用到 。curl
點擊微信插件,掃碼二維碼關注這個企業:ide
經過corpid和secret獲取token,發送get請求:
[root@bogon]#curl 「https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww4e1e91944d4fa553&corpsecret=6JFbRgVN_SwQifqBxpyEBQ-ZxnMEa3HB3tOEIcyaowA」
##注:ww4e1e91944d4fa553是CORPID值;6JFbRgVN_SwQifqBxpyEBQ-ZxnMEa3HB3tOEIcyaowA是Secret值。
迴應的值:
{"errcode":0,"errmsg":"ok","access_token":"2SgMy55--z_jPZqNT0GZ87QjhfrZm9OTrfdPzwinPuOOvSfm3xnxGOtO-GlNm_kK1z_b1Qzse6cic3OHlFLgz5AEDu90LD2Q-IX_3XtphH8hD32pNLyRxOswGGKcfVjA8D7X2j2vN2b7oD3_FSRLvTAfzJNlQVSVsgzsSY849cecVmRGFp8SFhJcc7RPmCKPlqAjotE7Ux6fVJy1dN_9Y6mDYEC3dCm00_finYoUMM3_RGF9VUfMJRGfEtkb_MnNy8eOlBiBVCtAaei1XAnQLoOcDsMtEgSo5eoE1nLOWk","expires_in":7200}
##注:咱們記住2SgMy55--z_jPZqNT0GZ87QjhfrZm9OTrfdPzwinPuOOvSfm3xnxGOtO-GlNm_kK1z_b1Qzse6cic3OHlFLgz5AEDu90LD2Q-IX_3XtphH8hD32pNLyRxOswGGKcfVjA8D7X2j2vN2b7oD3_FSRLvTAfzJNlQVSVsgzsSY849cecVmRGFp8SFhJcc7RPmCKPlqAjotE7Ux6fVJy1dN_9Y6mDYEC3dCm00_finYoUMM3_RGF9VUfMJRGfEtkb_MnNy8eOlBiBVCtAaei1XAnQLoOcDsMtEgSo5eoE1nLOWk
接下來咱們測試一下這個用戶接口,獲取一下他有哪一個用戶或者組方便下面發送:
[root@bogon]#
##注 agentid是本身企業號的;&agentid以前access_token=以後是發get的迴應值。
迴應值:
{"errcode":0,"errmsg":"ok","agentid":1000002,"name":"告警","square_logo_url":"http://p.qpic.cn/qqmail_pic/3156855361/a2a402aa1925038f30ced69ca5c5d5620f66ca9d308e1b03/0","description":"","allow_userinfos":{"user":[{"userid":"LiMingGui"}]},"allow_partys":{"partyid":[]},"close":0,"redirect_domain":"","report_location_flag":0,"isreportenter":0,"home_url":""}
##迴應了一個userid爲LiMingGui的用戶。咱們能夠知道發給的用戶ID是LiMingGui這個用戶。
咱們寫一個測試腳本:
[root@node1 ~]# cat post_data.py
#!/usr/bin/env python
import requests
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=2SgMy55--z_jPZqNT0GZ87QjhfrZm9OTrfdPzwinPuOOvSfm3xnxGOtO-GlNm_kK1z_b1Qzse6cic3OHlFLgz5AEDu90LD2Q-IX_3XtphH8hD32pNLyRxOswGGKcfVjA8D7X2j2vN2b7oD3_FSRLvTAfzJNlQVSVsgzsSY849cecVmRGFp8SFhJcc7RPmCKPlqAjotE7Ux6fVJy1dN_9Y6mDYEC3dCm00_finYoUMM3__RGF9VUfMJRGfEtkb_MnNy8eOlBiBVCtAaei1XAnQLoOcDsMtEgSo5eoE1nLOWk"
data = {
"touser": "LiMingGui",
"msgtype": "text",
"agentid": 1000002,
"text": {
"content": "hello ,lmg"
},
"safe":"0"
}
r = requests.post(url,json=data,verify=False)
print r.status_code
print r.content
若是沒法運行py,而且提示requests。
安裝requests就能夠了
返回值:
[root@bogon alertscripts]# ./post_data.py
/usr/lib/python2.7/site-packages/urllib3/connectionpool.py:769: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
InsecureRequestWarning)
200
{"errcode":0,"errmsg":"ok","invaliduser":""}
##而且微信企業號會收到消息.
接下來咱們就修改一下這個微信腳本.
[root@node1 ~]# cat post_data.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import sys
import urllib3
class weChat:
def __init__(self,Corpid,Secret):
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (Corpid,Secret)
res = self.url_req(url)
self.token = res["access_token"]
def url_req(self,url):
requests.packages.urllib3.disable_warnings()
req = requests.get(url, verify=False)
res = json.loads(req.text)
return res
def send_message(self,user,content):
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % self.token
data = {
"touser": user,
"msgtype": "text",
"agentid": 1000002,
"text": {
"content": content
},
"safe":"0"
}
requests.packages.urllib3.disable_warnings()
res = requests.post(url,json=data, verify=False)
if json.loads(res.content)['errmsg'] == 'ok':
return "send message sucessed"
else:
return res
if __name__ == '__main__':
user = sys.argv[1]
content = sys.argv[2]
get_token = weChat('ww4e1e91944d4fa553','6JFbRgVN_SwQifqBxpyEBQ-ZxnMEa3HB3tOEIcyaowA')
print get_token.send_message(user, content)
##注:weChat換成大家本身的agentid corpid和secert:
先測試一下腳本是否可以發送
[root@bogon alertscripts]# python post_data.py "LiMingGui" "nihao"
/usr/lib/python2.7/site-packages/urllib3/connectionpool.py:769: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
InsecureRequestWarning)
send message sucessed
zabbix添加報警媒介類型
## 用戶 添加報警媒介中,收件人填LiMingGui 。 即userid: LiMingGui。
添加動做
添加媒介