-
Notifications
You must be signed in to change notification settings - Fork 2
/
wx
55 lines (48 loc) · 1.7 KB
/
wx
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
54
55
# coding:utf-8
# 发送消息到微信小助手
import json
from bottle import get, request
import requests
# 微信小程序登录参数
#WXKEY = 'XXXXXXX' # 18位的key
#WXSECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' # 64位字符长度的的Secret
def get_token():
url='https://qyapi.weixin.qq.com/cgi-bin/gettoken'
values = {'corpid' : WXKEY ,
'corpsecret': WXSECRET,
}
req = requests.post(url, params=values)
data = json.loads(req.text)
return data["access_token"]
@get('/api/wx/sendmsg/')
def send_wxmsg():
_user = (request.query.user or '').strip()
_part = (request.query.part or '').strip()
_encrypt = (request.query.encrypt or '').strip()
if _encrypt not in ['1','0']:
_encrypt = '0'
_content = (request.query.content or '').strip()
if _user == '' and _part == '':
return {"resid":-1,"resmsg":"用户或组不能都为空"}
if _content == '':
return {"resid":-2,"resmsg":"内容为空"}
url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+get_token()
values = """{"touser" : "%s" ,
"toparty":"%s",
"msgtype":"text",
"agentid":"1000005",
"text":{
"content": "%s"
},
"safe":"%s"
}""" % (_user,_part,_content,_encrypt)
headers = {"Content-Type":"application/json; charset=UTF-8"}
req = requests.post(url, values.encode())
_json = json.loads(req.text)
_json['resmsg']=0
_json['resmsg']='发送完成'
return _json
if __name__ == '__main__':
from bottle import default_app,run
app = default_app()
run(app, host='0.0.0.0', port=7080, debug=True, reloader=True)