Python開源:itchatmp - 一個開源的微信公眾號、企業號接口
介紹
itchatmp是一個開源的微信公眾號、企業號接口,使用python調用微信公眾號從未如此簡單。
基于tornado框架,滿足效率需求。支持普通使用、nginx反向代理與wsgi。
同樣的命令,支持同步與協程調用,適合各層次開發者使用。
安裝
可以通過本命令安裝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()</code></pre>
一些進階應用可以在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()</code></pre>
itchatmp里面所有的方法都變成了協程方法,如果你不熟悉協程 建議不要使用 ,線程池也足夠滿足普通需求。
如果你問出類似為什么 time.sleep 阻塞了協程的問題,我會很困擾的。
WSGI使用
如果你需要生成一個能夠在類似SAE的平臺上包裝的應用,你可以這樣生成:
app = itchatmp.run(isWsgi=True)
如果你還是無法配置,請閱讀文檔一欄的 部署 部分。
意見與建議
如果有什么問題或者建議都可以在這個Issue和我討論