python實現調用企業微信報警
阿新 • • 發佈:2018-01-05
cgi title import bsp sage python實現 argv ken read
代碼部分
vim wechat.py #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hou Xingbin import urllib import json import sys import time from wechat_conf import CorpID, Agentid, Secret localtime = time.strftime("[%H:%M:%S]", time.localtime()) dl="\n-------------------------------------\n" class Tencent(object):def __init__(self,user,title,msg):
#格式化輸出內容:標題+內容 self.MSG = localtime+title+dl+msg self.User = user self.url = ‘https://qyapi.weixin.qq.com‘ self.send_msg = json.dumps({ "touser": self.User, "msgtype": ‘text‘, "agentid": Agentid,"text": {‘content‘: self.MSG}, "safe": 0 }) #獲取tokent def get_token(self): token_url = ‘%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s‘ % (self.url, CorpID, Secret) token = json.loads(urllib.urlopen(token_url).read())[‘access_token‘] return token
#發送信息def send_message(self): send_url = ‘%s/cgi-bin/message/send?access_token=%s‘ % (self.url,self.get_token()) respone = urllib.urlopen(url=send_url, data=self.send_msg).read() x = json.loads(respone.decode())[‘errcode‘] if x == 0: print (‘Succesfully‘) else: print (‘Failed‘)
#創建對象 send_obj = Tencent(sys.argv[1],sys.argv[2],sys.argv[3])
#調用發送函數 send_obj.send_message()
配置文件
vim wechat_conf.py #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hou Xingbin
#此為企業的ID號 CorpID = ‘***************‘
#應用的ID Agentid = 1000002
#認證信息,企業ID+認證信息可獲取tokent,獲取之後向此tokent發送內容 Secret = ‘********************************‘
python實現調用企業微信報警