推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
NxnXgpuPSfsIT
V2EX  ›  Python

itchatmp:基于 tornado 的协程微信公众、企业号框架

  •  1
     
  •   NxnXgpuPSfsIT ·
    littlecodersh · Dec 16, 2016 · 4108 views
    This topic created in 3573 days ago, the information mentioned may be changed or developed.

    itchatmp 是我近期的一个新的项目,尝试将协程和公众平台的配置尽量简化给开发者,希望你喜欢。

    本项目基于 tornado 框架,轻松满足效率需求。支持普通使用、 nginx 反向代理与 wsgi 。

    同样的命令,支持同步与协程调用(详见进阶使用一章),适合各层次开发者使用。

    与个人号接口itchat共享类似的操作方式,学习一次掌握两个工具。

    安装

    可以通过本命令安装 itchatmp :

    pip install itchatmp
    

    快速入门

    有了 itchatmp ,如果你想要回复发给自己的文本消息,只需要这样:

    import itchatmp
    
    itchatmp.update_config(itchatmp.WechatConfig(
        token='yourToken',
        appId = 'yourAppId',
        appSecret = 'yourAppSecret'))
    
    @itchatmp.msg_register(itchatmp.content.TEXT)
    def text_reply(msg):
        return msg['content']
    
    itchatmp.run()
    

    一些进阶应用可以在 Advanced uses 中看到,或者你也可以阅览文档。

    进阶使用

    企业号配置

    在配置时设置 copId 而非 appId 即可。

    另,由于企业号没有明文模式,所以必须将加密模式设置为安全。

    具体的设置可以看这里。

    协程使用

    如果你需要使用协程版本的 itchatmp ,你需要另外安装一个组件:

    pip install itchatmphttp
    

    这样,你的 itchatmp 就变成协程版本了。同样,删除以后就变回了线程池版本。

    例如回复信息的操作,协程也只需要这样写:

    import itchatmp
    from tornado import gen
    
    itchatmp.update_config(itchatmp.WechatConfig(
        token='yourToken',
        appId = 'yourAppId',
        appSecret = 'yourAppSecret'))
    
    @itchatmp.msg_register(itchatmp.content.TEXT)
    def text_reply(msg):
        yield gen.sleep(3)
        r = yield itchatmp.send('First message', msg['FromUserName'])
        print('First message sent: %s' % r)
        yield gen.sleep(3)
        r = yield itchatmp.send('First message', msg['FromUserName'])
        print('Second message sent: %s' % r)
    
    itchatmp.run()
    

    itchatmp 里面所有的方法都变成了协程方法,如果你不熟悉协程建议不要使用,线程池也足够满足普通需求。

    如果你问出类似为什么time.sleep阻塞了协程的问题,我会很困扰的。

    WSGI 使用

    如果你需要生成一个能够在类似 SAE 的平台上包装的应用,你可以这样生成:

    app = itchatmp.run(isWsgi=True)
    

    如果你还是无法配置,请阅读文档一栏的部署部分。

    意见与建议

    如果有什么问题或者建议都可以在这个Issue和我讨论

    当然也可以加入我们新建的 QQ 群讨论: 438747166

    5 replies  •  2016-12-20 18:48:52 +08:00
    NxnXgpuPSfsIT
        1
    NxnXgpuPSfsIT  
    OP
       Dec 16, 2016 via Android
    代码放在了 github 上面: https://github.com/littlecodersh/itchatmp
    tempdban
        2
    tempdban  
       Dec 16, 2016 via Android
    楼主的另一个大作 itchat 现在可能是为数不多的在维护的 web 微信接口。
    基于我平时对楼主的了解,我相信 itchatmp 一定会是一个开放且易于使用的公众号接口。
    墙裂推荐。
    Chyroc
        3
    Chyroc  
       Dec 17, 2016
    +1
    NxnXgpuPSfsIT
        4
    NxnXgpuPSfsIT  
    OP
       Dec 17, 2016 via Android
    谢谢两位的支持!
    saxon
        5
    saxon  
       Dec 20, 2016
    精品支持! +1 强烈推荐 在使用 itchat 时候楼主给出了很多指导 非常感谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3726 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 05:20 · PVG 13:20 · LAX 22:20 · JFK 01:20
    ♥ Do have faith in what you're doing.