Skip to main content

又一个微信公众号开发工具箱 Yet Another WeiXin(wechat) Tookit

Project description

build-status pypi-status pypi-pyversions Documentation Status

Yet Another WeiXin(wechat) Toolkit

github:https://github.com/lspvic/yawxt/

文档: https://yawxt.readthedocs.io/

实体对象

  1. 消息对象 yawxt.Message

  2. 用户对象 yawxt.User

  3. 位置对象 yawxt.Location

使用数据对象可以很好的管理和访问微信数据资源,如:

user.nickname, user.headimgurl, user.city
location.latitude, location.longitude, location.time
message.msg_type, message.msg_id, message.create_time

公众号API

yawxt.WxClient 封装公众号API

>>> from yawxt import WxClient
>>> client = WxClient("appid", "appsecret")
>>> it = client.get_openid_iter()
>>> openid = next(it)
>>> openid
'o9KLls70ReakhjebmHUYxjbz9K8c'
>>> user = client.get_user(openid)
>>> user
{"openid": "o9KLls70ReakhjebmHUYxjbz9K8c", "nickname": "yawxt", ...}
>>> user.city
'杭州'

消息对话

yawxt.MessageHandler 处理接收消息事件

from yawxt import MessageHandler, WxClient, check_signature

# 定义消息回复内容
class Handler(MessageHandler):

    # 当收到一条文本消息时
    def on_text(self, text):
        # 回复一条文本消息
        # 可以使用已定义的user对象
        self.reply_text("你好:%s" % self.user.nickname)

    # 当收到地理位置上报事件时
    def event_location(self,location):
        # 保存地理位置
        redis.hset("location::%s" % self.openid,
            {"lat": location.latitude, "lon": location.longitude})

    # 当收到一条图片消息时
    def on_image(self, media_id, pic_url):
        # 可以调用公众号API,下载到本地
        r = self.client.download_image(media_id)
        with open("/path/to/images/%s.jpg" % media_id, "rb") as f:
            f.write(r.content)
        # 回复同样的图片
        self.reply_image(media_id)

client = WxClient(appid, secret, token)

# 在web框架中回复消息,以Flask为例
app = Flask(__name__)
session_maker = sessionmaker(bind=engine)
token = "token"  # 公众号后台配置Token
@app.route('/wechat', methods=["GET", "POST"])
def wechat():
    signature = request.args.get('signature')
    timestamp = request.args.get('timestamp')
    nonce = request.args.get('nonce')
    if not check_signature(token, timestamp, nonce, signature):
        return "Messages not From Wechat"
    if request.method == "GET":
        return request.args.get('echostr')

    msg = PersistMessageHandler(request.data, wechat_account,
                            db_session_maker=session_maker,
                            debug_to_wechat=app.debug)
    return msg.reply()

消息持久化

使用类 yawxt.persistence.PersistMessageHandler ,不做任何处理就能够直接将接收的消息、 用户信息、上报位置信息保存到数据库中:

from yawxt.persistence import PersistMessageHandler

Session = session_maker(bind=engine)
message = PersistMessageHandler(content, client=client,
    db_session_maker=Session, debug_to_wechat=True)
return_str = message.reply()

继承 PersistMessageHandler ,只关注自己的处理逻辑,所有消息的接收 与发送都持久化到数据库中了。

更多的例子在 examples 文件夹下面

安装

使用pip安装yawxt:

pip install yawxt

如果要使用消息持久化,还需要安装sqlalchemy及数据库驱动,如mysql的PyMySQL:

pip install sqlalchemy PyMySQL

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yawxt-0.1.2.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

yawxt-0.1.2-py2.py3-none-any.whl (21.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file yawxt-0.1.2.tar.gz.

File metadata

  • Download URL: yawxt-0.1.2.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

File hashes

Hashes for yawxt-0.1.2.tar.gz
Algorithm Hash digest
SHA256 810fae7d753670a9cadb405b9560cc04ed3a118445002bb617b8edae0d2d865e
MD5 02ad2576bfd1ce791dd652bd1aa5cc3c
BLAKE2b-256 39d2f9f984675212872abc0461426db3ee47fc038b35a79371cbe26f9bffea3c

See more details on using hashes here.

File details

Details for the file yawxt-0.1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: yawxt-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 21.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

File hashes

Hashes for yawxt-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 24e216dd8fc75a0c629403f48c6aa8f1dc49d88db948f885420acf98bf27dbf0
MD5 f7e3ccb1e48ff50f97fbdfa78fece2f7
BLAKE2b-256 684db6326cf70b07932bef497c55c05c771c64041f18db47c7fefb4816987d5e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page