You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.0 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from WXBizMsgCrypt import WXBizMsgCrypt
import os
import json
from bottle import route, run, request
import requests
from urlparse import urlparse, parse_qs
token = ""
appid = ""
encodingAESKey = ""
def getToken():
url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=123123&secret=123123"
res = requests.get(url)
resp = res.json()
return resp["access_token"]
def sendCustomMessage(access_token, postJson):
url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s" % (access_token)
res = requests.post(url, data=postJson, headers={
'Content-Type': 'application/json'})
resp = res.json()
print("resp = ", resp)
@route('/game/wxcheck', method=["GET","POST"])
def wxcheck():
signature = request.query.signature
timestamp = request.query.timestamp
nonce = request.query.nonce
echostr = request.query.echostr
if echostr and echostr != None:
wx = WXBizMsgCrypt(token,encodingAESKey,appid)
ret = wx.CheckToken(timestamp, nonce)
print "ret = ", ret
if ret:
return echostr
pass
return "failed"
else:
timestamp = request.query.timestamp
nonce = request.query.nonce
msg_signature = request.query.msg_signature
signature = request.query.signature
data = request.json
wx = WXBizMsgCrypt(token,encodingAESKey,appid)
ret ,decryp_data = wx.DecryptJsonMsg(data, msg_signature, timestamp, nonce)
if ret == 0:
response = json.loads(decryp_data)
openid = response["FromUserName"]
toUserName = response['ToUserName'];
MsgType = response["MsgType"]
if MsgType == "event":
Event = response["Event"]
if Event == "user_enter_tempsession":
print "welcome~"
elif MsgType == "miniprogrampage":
PagePath = response["PagePath"]
parsed_url = urlparse(PagePath)
query_params = parse_qs(parsed_url.query)
resp = {
"touser":openid,
"msgtype":"link",
"link": {
"title": "点我进行充值",
"description": "点我" + response["Title"],
"thumb_url": "https://leshusanguo-xcx-mini.oss-cn-beijing.aliyuncs.com/remote/bundle/logo.png",
"url": "http://h5pay.brsg.top?order_no=" + query_params["order_no"][0]
}
}
access_token = getToken()
sendCustomMessage(access_token, json.dumps(resp, ensure_ascii=False).encode('utf-8'))
elif MsgType == "text":
content = response['text']["content"]
print "content = ", content
else:
print "not found~"
return "success"
return ""
run(host="0.0.0.0", port=8900)