Skip to content

KEDYY/pyweipi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

WXAPI

WXApi 是一个基于微信公众号平台API文档开发的一个 Python服务接口

用于将微信服务号请求的消息(事件)转换为一个类对象, 更简单的属性,更容易的操作

版本支持

  • 2.6
  • 2.7
  • 3.x

状态

Build PyPI release Licence

安装

$pip install pyweipi

案例

一个基于flask的 请求处理demo

# -*- coding: utf-8 -*-
from WXApi import *
from flask import Flask, Response, request
app = Flask(__name__)

BASE_URL = '/wechatrequest'
BASE_TOKEN = 'wechatrequest'


def service_route(ask):
    # type: (WeChatObject) -> ReplyObject
    if isinstance(ask, ImageMsg):
        return ImageReply(ask, ask.media_id)

    if isinstance(ask, VoiceMsg):
        ques = "{0}".format(ask.recognition)
        return VoiceReply(ask, ask.media_id)

    if isinstance(ask, TextMsg):
        msg = ask.content
        return TextMsg(ask)

    if isinstance(ask, LinkMsg):
        return NewsReply(ask, title=ask.title, desc=ask.description, 
        picture_url='',link=ask.link)

    if isinstance(ask, SubEvent):
        return TextReply(ask, "欢迎关注")
    
    if isinstance(ask, UnSubEvent):
        return EmptyReply(ask)

    return TextReply(ask, "我不懂你在说什么才是最大的痛吧 😪")


# 用于处理微信的数据请求
@app.route(BASE_URL, methods=['GET', 'POST'])
def app_main():
    query = url_decode(request.query_string)
    # 认证是否是微信的合法请求
    if auth_signature(BASE_TOKEN, query):
        if 'echostr' in query:
            return Response(query.get('echostr'), 200)
    else:
        return Response(status=401)
    if 'POST' != request.method:
        return Response(status=400)
    # 认证成功,处理POST数据
    try:
        json_req = xml2event(request.data)
        res_data = service_route(json_req).create_xml() + ""
    except Exception as e:
        return Response(status=200)
    else:
        return Response(res_data, 200)

许可

The MIT License

About

基于微信公开接口做的python封装

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages