本文最后更新于 1299 天前,其中的信息可能已经有所发展或是发生改变。
from bs4 import BeautifulSoup
from qywx import *
import requests
import time
import sys
import json
class WeChat:
def __init__(self):
self.CORPID = 'XXX' #企业ID,在管理后台获取
self.CORPSECRET = 'XXX'#自建应用的Secret,每个自建应用里都有单独的secret
self.AGENTID = '1000005' #应用ID,在后台应用中获取
self.TOUSER = "XXX" # 接收者用户名,多个用户用|分割
def _get_access_token(self):
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
values = {'corpid': self.CORPID,
'corpsecret': self.CORPSECRET,
}
req = requests.post(url, params=values)
data = json.loads(req.text)
return data["access_token"]
def get_access_token(self):
try:
with open('F:\Auto Baidu\Project code\pythonProject\wechat_config.json', 'r') as f:
t, access_token = f.read().split()
except:
with open('F:\Auto Baidu\Project code\pythonProject\wechat_config.json', 'w') as f:
access_token = self._get_access_token()
cur_time = time.time()
f.write('\t'.join([str(cur_time), access_token]))
return access_token
else:
cur_time = time.time()
if 0 < cur_time - float(t) < 7260:
return access_token
else:
with open('F:\Auto Baidu\Project code\pythonProject\wechat_config.json', 'w') as f:
access_token = self._get_access_token()
f.write('\t'.join([str(cur_time), access_token]))
return access_token
def send_data(self, message):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.get_access_token()
send_values = {
"touser": self.TOUSER,
"msgtype": "text",
"agentid": self.AGENTID,
"text": {
"content": message
},
"safe": "0"
}
send_msges=(bytes(json.dumps(send_values), 'utf-8'))
respone = requests.post(send_url, send_msges)
respone = respone.json() #当返回的数据是json串的时候直接用.json即可将respone转换成字典
return respone["errmsg"]
if __name__ == '__main__':
wx = WeChat()
wx.send_data("这是程序发送的第1条消息!\n Python程序调用企业微信API,从自建应用“告警测试应用”发送给管理员的消息!")
def fc168():
target168 = 'https://xxx.com'
req = requests.get(url=target168)
fc168 = req.text
fc168_class = BeautifulSoup(fc168,features="html.parser")
fc168_div = fc168_class('span',class_='text-red-700')
fc168_div_cuowu = fc168_div[0].text
if fc168_div_cuowu == ' 1 faults, ':
print(time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime()) + fc168_div_cuowu,'168节点,没有出错!!!')
else:
print(time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime()) + fc168_div_cuowu + "168节点出错了,快快查看!")
# 下面为企业微信推送
wx = WeChat()
wx.send_data(time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime()) + fc168_div_cuowu + " 168节点好像出问题了")