个人zabbix报警使用 写的比较简单 默认会发消息到第一个部门 感兴趣的可以自己查阅api文档修改下 很简单 附上代码
#!/usr/bin/env python # -*- coding: utf-8 -*- """ author = ‘Perling‘ time = ‘2016/11/21‘ """ import sys reload(sys) sys.setdefaultencoding(‘utf8‘) import urllib2 import json CorpID = ‘‘ Secret = ‘‘ def http_get(url): req = urllib2.Request(url) resp = urllib2.urlopen(req) resp_json = json.loads(resp.read()) return resp_json def http_post(url,post_data): post_data = json.dumps(post_data,ensure_ascii=False) req = urllib2.Request(url, post_data) req.add_header(‘Content-Type‘, ‘application/json‘) req.add_header(‘encoding‘, ‘utf-8‘) resp = urllib2.urlopen(req) resp_json = json.loads(resp.read()) return resp_json def get_access_key(): resp = http_get("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s"%(CorpID,Secret)) if resp.has_key("access_token"): return resp["access_token"] else: return False def Send_message(access_key,user,party,message): data = { "touser": user, "toparty": party, "totag": "", "msgtype": "text", "agentid": 0, "text": { "content": message }, "safe":0 } return http_post("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s"%access_key,data) if __name__ == ‘__main__‘: access_key = get_access_key() if access_key: send_user = "" #用户id send_party = "1" #部门id message = sys.argv[3] print Send_message(access_key,send_user,send_party,message)
时间: 2024-12-05 18:41:31