-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwechat.py
53 lines (48 loc) · 1.61 KB
/
wechat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import requests
import ast
import json
from config import wechat_conf
import simplejson
class WeChat(object):
def __init__(self):
self.corpid = wechat_conf["corpid"]
self.corpsecret = wechat_conf["corpsecret"]
self.party = wechat_conf["party"]
self.agentid = wechat_conf["agentid"]
self.token_url = wechat_conf["token_url"]
self.msg_url = wechat_conf["msg_url"]
def send(self, subject, msg, url):
msg = self.normalize(msg)
payload = {
"touser": "",
"toparty": self.party,
"msgtype": "news",
"agentid": self.agentid,
"news": {
"articles":
[
{
"title": subject,
"description": msg,
"url": url,
"picurl": ""
}
]
},
"safe": "0"
}
r = requests.post(self.msg_url + "?access_token=" + self.get_token(),
data=simplejson.dumps(payload, ensure_ascii=False).encode('utf8'),
headers={'Content-type':'application/json', 'charset': 'utf-8'})
return r
def get_token(self):
payload = {
"corpid": self.corpid,
"corpsecret": self.corpsecret
}
r = requests.get(self.token_url, params=payload)
data = ast.literal_eval(r.text)
return data["access_token"]
def normalize(self, broker_message):
msg = broker_message
return msg