我是靠谱客的博主 魁梧羽毛,最近开发中收集的这篇文章主要介绍使用python 发送企业微信告警,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

首先:发送企业微信告警需要四个参数:corpid(公司id)secret agentid和token, corpid(公司id)secret agentid三个参数均由管理员提供,员工无法获取,首先问管理员获取这三个数据,token由corpid(公司id和secret获取

其次,告警需要发给谁,可以由管理员在网页版企业微信后台增加对应的人员

代码:

class 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 发送企业微信告警所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部