最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python实现发送警告通知到企业微信代码方法
时间:2022-06-25 01:28:16 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下Python实现发送警告通知到企业微信代码方法,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
1. 新建应用
登陆网页版企业微信
点击应用管理->应用->创建应用
上传应用的 logo,输入应用名称,再选择可见范围,成功创建一个告警应用
2. 获取Secret
使用 Python 发送告警请求,其实就只使用到两个接口
获取 Token:
https://qyapi.w**eixin*.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={secret}
发送请求 :
https://qyapi.w**eixin.q*q.com/cgi-bin/message/send?access_token={token}
可以看到,最重要的是 corpid 和 secret:
corpid:唯一标识你的企业
secret:应用级的密钥,有了它程序才知道你要发送该企业的哪个应用
corpid 可以通过我的企业->企业信息获取
而 secret 获取相对麻烦一点,点击前面的创建应用,点击 查看 secret
然后再点击发送就会发送到你的企业微信上
最后将 corpid 和 secret 填入下面的常量中。
import json
import datetime
import requests
CORP_ID = ""
SECRET = ""
class WeChatPub:
s = requests.session()
def __init__(self):
self.token = self.get_token()
def get_token(self):
url = f"https://qyapi.*w**eixin.qq.com/cgi-bin/gettoken?corpid={CORP_ID}&corpsecret={SECRET}"
rep = self.s.get(url)
if rep.status_code != 200:
print("request failed.")
return
return json.loads(rep.content)['access_token']
def send_msg(self, content):
url = "https://qyapi.*we*ixin.*qq.com/cgi-bin/message/send?access_token=" + self.token
header = {
"Content-Type": "application/json"
}
form_data = {
"touser": "@all",
"toparty": " PartyID1 | PartyID2 ",
"totag": " TagID1 | TagID2 ",
"msgtype": "textcard",
"agentid": 1000002,
"textcard": {
"title": "服务异常告警",
"description": content,
"url": "URL",
"btntxt": "更多"
},
"safe": 0
}
rep = self.s.post(url, data=json.dumps(form_data).encode('utf-8'), headers=header)
if rep.status_code != 200:
print("request failed.")
return
return json.loads(rep.content)
然后就可以通过 send_msg 函数发送消息了。
wechat = WeChatPub()
now = datetime.datetime.now()
timenow = now.strftime('%Y年%m月%d日 %H:%M:%S')
wechat.send_msg(f"{timenow} 阿里云 cookie 已失效请尽快更换新的 cookie")
只要你的企业微信没有关闭通知的权限,那你的手机立马就会弹出这个告警信息。
简单几步就对接了企业微信,实现了手机的实时告警功能,推荐有企业微信的同学使用。
当然一定有更多,更好用的实现方法,我只是我选择了其中一种。
相关文章
- 超自然行动组礼包兑换码有哪些 2025最新可用兑换码合集 11-05
- 逃离鸭科夫怎么刷刀 逃离鸭科夫刷刀方法介绍 11-05
- 逃离鸭科夫怎么解锁农场小镇 农场小镇解锁攻略 11-05
- 梦幻消除战座无虚席有什么玩法技巧 11-05
- 适合跨年的句子 11-05
- 忘川风华录周瑜怎么培养 11-05





