#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author : zhibo.wang
# E-mail : d_1206@qq.com
# Date : 18/02/25 19:09:44
# Desc : 发送企业微信应用通知
import json
import requests
class Weixin:
touser = '@all'
agentid = ''
corpid = ''
corpsecret = ''
url = 'https://qyapi.weixin.qq.com'
def __init__(self):
token_url = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (self.url, self.corpid, self.corpsecret)
self.token = requests.get(token_url).json()['access_token']
def send_message(self, url, data):
send_url = '%s/cgi-bin/message/send?access_token=%s' % (url, self.token)
self.respone = requests.post(url=send_url, data=data).json()
x = self.respone['errcode']
if x == 42001:
self.__init__()
send_url = '%s/cgi-bin/message/send?access_token=%s' % (url, self.token)
self.respone = requests.post(url=send_url, data=data).json()
x = self.respone['errcode']
if x == 0:
return 'Succesfully'
else:
return 'Failed'
def messages(self, subject, message):
values = {
"touser": self.touser,
"msgtype": 'text',
"agentid": self.agentid,
"text": {'content': subject + message},
"safe": 0
}
return self.send_message(self.url, bytes(json.dumps(values), 'utf-8'))
if __name__ == '__main__':
W = Weixin()
subject = "test"
message = "...."
ret = W.messages(subject, message)