参考链接:
- https://www.abcdocker.com/abcdocker/2472
- https://www.zabbix.com/documentation/3.4/manual/quickstart/
#!/usr/bin/python2.7
#_*_coding:utf-8 _*_
#author:SunTao
import requests,sys,json
import urllib3
import urllib2
import urllib
urllib3.disable_warnings()
reload(sys)
sys.setdefaultencoding('utf-8')
def GetToken(Corpid,Secret):
Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
Data = {
"corpid":Corpid,
"corpsecret":Secret
}
#r = requests.get(url=Url,params=Data,verify=False)
#r = requests.get(url=Url,params=Data)
r = urllib2.urlopen(Url + '?corpid=' + Corpid + '&corpsecret=' + Secret)
res = r.read()
print type(res)
print res
Token = json.loads(res)['access_token']
return Token
def SendMessage(Token,User,Agentid,Subject,Content):
Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token
Data = {
"touser": User,
"msgtype": "text", # 消息类型。
"agentid": Agentid, # 企业号中的应用id。
"text": {
"content": Subject + '\n' + Content
},
"safe": "0"
}
#data = urllib.urlencode(Data)
request = urllib2.Request(Url,data = json.dumps(Data))
response = urllib2.urlopen(request)
#r = requests.post(url=Url,data=json.dumps(Data),verify=False)
print response.read()
return response.getcode()
if __name__ == '__main__':
User = sys.argv[1] # zabbix传过来的第一个参数
Subject = sys.argv[2] # zabbix传过来的第二个参数
Content = sys.argv[3] # zabbix传过来的第三个参数
Corpid = "######" # CorpID是企业号的标识
Secret = "#####" # Secret是管理组凭证密钥
#Tagid = "1" # 通讯录标签ID
Agentid = "1000002" # 应用ID
Token = GetToken(Corpid, Secret)
print Token
Status = SendMessage(Token,User,Agentid,Subject,Content)
print Status