码迷,mamicode.com
首页 > 微信 > 详细

python实现调用企业微信报警

时间:2018-01-05 16:53:04      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:api   load   log   elf   sel   企业微信   from   ext   time   

代码部分

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实现调用企业微信报警

标签:api   load   log   elf   sel   企业微信   from   ext   time   

原文地址:https://www.cnblogs.com/houxingbin/p/8205342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!