Git Product home page Git Product logo

Comments (1)

aiportal avatar aiportal commented on August 14, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.