首先:发送企业微信告警需要四个参数:corpid(公司id)secret agentid和token, corpid(公司id)secret agentid三个参数均由管理员提供,员工无法获取,首先问管理员获取这三个数据,token由corpid(公司id和secret获取
其次,告警需要发给谁,可以由管理员在网页版企业微信后台增加对应的人员
代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47class WeChat(object): def __init__(self, corpid, secret, agentid): self.url = "https://qyapi.weixin.qq.com" self.corpid = corpid self.secret = secret self.agentid = agentid # 获取企业微信的 access_token def access_token(self): url_arg = '/cgi-bin/gettoken?corpid={id}&corpsecret={crt}'.format( id=self.corpid, crt=self.secret) url = self.url + url_arg response = requests.get(url=url) text = response.text self.token = json.loads(text)['access_token'] # 构建消息格式 def messages(self, msg): values = { "touser": '@all', "msgtype": 'text', "agentid": self.agentid, "text": {'content': msg}, "safe": 0 } # python 3 self.msg = (bytes(json.dumps(values), 'utf-8')) # python 2 #self.msg = json.dumps(values) # 发送信息 def send_message(self, msg): self.access_token() self.messages(msg) send_url = '{url}/cgi-bin/message/send?access_token={token}'.format( url=self.url, token=self.token) response = requests.post(url=send_url, data=self.msg) errcode = json.loads(response.text)['errcode'] if errcode == 0: print('send Succesfully') else: print('send Failed') corpid = "" secret = "" agentid = "" wechat = WeChat(corpid, secret, agentid) wechat.access_token() msg="hello world" wechat.send_message(msg)
代码逻辑:获取token->组建消息->发送消息
最后
以上就是魁梧羽毛最近收集整理的关于使用python 发送企业微信告警的全部内容,更多相关使用python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复