Comments (1)
from flask.views import View, request
from flask import abort, current_app
from wechatpy import parse_message
from wechatpy.utils import check_signature
from wechatpy.exceptions import InvalidSignatureException, InvalidAppIdException
from wechatpy.crypto import WeChatCrypto
from wechatpy.replies import BaseReply
class WechatServerView(View):
methods = ['GET', 'POST']
@property
def appid(self):
raise NotImplemented
@property
def secret(self):
raise NotImplemented
@property
def token(self):
raise NotImplemented
@property
def aes_key(self):
return None
def dispatch_request(self):
signature, timestamp, nonce = [request.args.get(k) for k in ('signature', 'timestamp', 'nonce')]
# check signature
try:
check_signature(self.token, signature, timestamp, nonce)
except InvalidSignatureException as ex:
current_app.logger.warning('signature failed.')
return abort(403)
if request.method == 'GET':
return request.args.get('echostr')
raw_msg = request.data
current_app.logger.debug(raw_msg)
if self.aes_key:
crypto = WeChatCrypto(self.token, self.aes_key, self.appid)
try:
raw_msg = crypto.decrypt_message(
raw_msg,
signature,
timestamp,
nonce
)
except (InvalidAppIdException, InvalidSignatureException):
current_app.logger.warning('decode message failed.')
return abort(403)
wechat_msg = parse_message(raw_msg)
res = self.process_wechat_msg(wechat_msg)
xml = ''
if isinstance(res, BaseReply):
xml = res.render()
if self.aes_key:
crypto = WeChatCrypto(
self.token,
self.aes_key,
self.appid
)
xml = crypto.encrypt_message(xml, nonce, timestamp)
return xml
def process_wechat_msg(self, msg):
raise NotImplemented
from flask-wechatpy.
Related Issues (4)
- 貌似WeChatComponent没有封进去?
- 安全加密模式下有2个bug HOT 2
- 老哥,readme中redis配置错了呀 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flask-wechatpy.