概述
#!/usr/bin/env python
#-*- coding: utf-8 -*-import time
import requests
import json
import os
import traceback
import loggingclassWeChat:
def __init__(self):
self.CORPID= 'ww750dfdfde43613c3'#企业ID,在管理后台获取
self.CORPSECRET= 'rCRzAXI-r7KUZ6uL5HZYYLaqQw2volmnhQrdStu6Qrk'#自建应用的Secret,每个自建应用里都有单独的secret
self.AGENTID= '1000011'#应用ID,在后台应用中获取
self.TOUSER= "g1702543|g1702076|g1701677" # 接收者用户名,多个用户用|分割
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,proxies={'https':'https://10.191.131.20:3128'}) ##原因我这边是代理上网的,需要程序里面制定代理地址
data=json.loads(req.text)return data["access_token"]
def get_access_token(self):try:
with open('access_token.conf', 'r') asf:
t, access_token=f.read().split()
except:
with open('access_token.conf', 'w') asf:
access_token=self._get_access_token()
cur_time=time.time()
f.write('t'.join([str(cur_time), access_token]))returnaccess_tokenelse:
cur_time=time.time()if 0 < cur_time - float(t) < 7260:returnaccess_tokenelse:
with open('access_token.conf', 'w') asf:
access_token=self._get_access_token()
f.write('t'.join([str(cur_time), access_token]))returnaccess_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"}
logging.basicConfig(filename='log.txt', level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s') ##输出执行日志try:
send_msges=(bytes(json.dumps(send_values)))respone= requests.post(send_url, send_msges,proxies={'https':'https://10.191.131.20:3128'})respone=respone.json() #当返回的数据是json串的时候直接用.json即可将respone转换成字典return respone["errmsg"]
except:
logging.debug(traceback.format_exc())if __name__ == '__main__':
wx=WeChat()
msg="""服务器10.134.167.126 EVMI JOBS计划任务异常告警提示如下:
TaskName名称 上次执行失败时间"""file= open('reulst_text.txt','r')whileTrue:
line=file.read()
msg+= line.strip()+'n'
ifnot line:breakwx.send_data(msg)
最后
以上就是小巧路人为你收集整理的python企业微信调用_Python 结合企业微信告警脚本的全部内容,希望文章能够帮你解决python企业微信调用_Python 结合企业微信告警脚本所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复