注册企业号:
https://qy.weixin.qq.com/cgi-bin/loginpage
注册成功后记住CORPID:
到通讯录添加一个成员
我的企业,通讯里面添加一个管理员用户
到权限管理添加一个管理员
我的企业—-》权限管理—-》添加一个管理员,就是自己
然后创建一个应用
创建之后,我们就有了:Agentid和Secret的两个值,后面会用到 。
点击微信插件,扫码二维码关注这个企业:
通过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。
添加动作
添加媒介