import json
import datetime
import requests
CORP_ID = "ww4b68328c070c8598"
SECRET = "S425KlOLuuXPKopUl5fftDwYKBjMPgugb9CorY7wrkI"
agentid = 1000003
class WeChatPub:
s = requests.session()
def __init__(self):
self.token = self.get_token()
def get_token(self):
url = f"https://qyapi.weixin.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.weixin.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": agentid,
"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)
wechat = WeChatPub()
now = datetime.datetime.now()
timenow = now.strftime('%Y年%m月%d日 %H:%M:%S')
message = wechat.send_msg(f"<div class=\"gray\">{timenow}</div> <div class=\"normal\">阿里云 cookie 已失效</div><div class=\"highlight\">请尽快更换新的 cookie</div>")
print('发送成功', message)
发送成功 {'errcode': 0, 'errmsg': 'ok', 'msgid': 'mrVtVXE39it1tWVvd57npDYzG4bv9tI6PZqkG4c7gMmDw1xp4QBz8uhDXifigIWmKzzljG7IjXzG2j4Bir43Nw'}