django-wechat-api

This project is the python/django API for wechat official account development.

wechat is the django project used for test django applications.

wechat_api is a django application for wechat subscribe and service account.

enterprise_api is a django application for wechat enterprise account.

application_api is a django application for wechat application account.

The User Guid

wechat

wechat is a django project created for test wechat API applications.

Include:

[Wechat subscribe&service Interface]

[Enterprise Interface]

[Application Interface]

And some other functions:

How to use

  1. Create django project wechat:

    $django-admin startproject wechat
    
  2. Install this django application:

    $pip install django-wechat-api
    
  3. Configuration the django project in settings.py:

    INSTALLED_APPS = (
    ...
    # Just choose which you need
    'wechat_api',
    'enterprise_api',
    'application_api'
    )
    
    # SAE/BAE:True means on server, False means on local.
    DEPLOY = 'SERVER_SOFTWARE' in os.environ
    
    # For SAE
    if DEPLOY:
        import sae.const
        MYSQL_DB = sae.const.MYSQL_DB
        MYSQL_USER = sae.const.MYSQL_USER
        MYSQL_PASS = sae.const.MYSQL_PASS
        MYSQL_HOST = sae.const.MYSQL_HOST
        MYSQL_PORT = sae.const.MYSQL_PORT
    # For BAE
    #if DEPLOY:
    #    MYSQL_DB = '******'
    #    MYSQL_USER = '******'
    #    MYSQL_PASS = '******'
    #    MYSQL_HOST = 'sqld.duapp.com'
    #    MYSQL_PORT = '4050'
    # For Local
    else:
         MYSQL_DB = 'wechat'
         MYSQL_USER = '******'
         MYSQL_PASS = '******'
         MYSQL_HOST = 'localhost'
         MYSQL_PORT = '3306'
    
    # For wechat subscribe and service account.
    WECHAT_TOKEN = u'your_token'
    WECHAT_APP_ID = u'your_app_id'
    WECHAT_APP_SECRET = u'your_app_secret'
    
    # For wechat enterprise account.
    CORPID = "your_corp_id"
    TOKEN = "your_token"
    ENCODINGAESKEY = "your_encoding_aeskey"
    SECRET = "your_secret"
    AGENTID = 0
    SAFE = 0
    
  4. Configuration the django project in urls.py:

    urlpatterns = [
        url(r'^wechat/', include('wechat_api.urls', namespace='wechat_api')),
        url(r'^enterprise/', include('enterprise_api.urls', namespace='enterprise_api')),
    ]
    
  5. Deploy your project on SAE or BAE to test:

    # Export data from local mysql and import to SAE/BAE
    $mysqldump -u <username> -p <databasename> > <filename>.sql
    
  6. Register your wechat official account and fill in your SAE/BAE url and your tocken:

    URL(SAE): http://<project>.applinzi.com/<wechat/enterprise>/
    URL(BAE): http://<project>.duapp.com/<wechat/enterprise>/
    TOKEN: yourtoken
    

wechat_api

This django application is the API for wechat subscribe and service account.

微信订阅号和服务号的接口是一样的.

已实现的功能:

  • 接收用户发送的文本消息,通过图灵机器人回复
  • 接收用户发送的图片消息,通过微软的how-old回复年龄和性别

待实现的功能:

  • 接收用户发送的其它类型的消息的回复
  • 接收用户发送的事件类型的消息并回复
  • 定制菜单

不实现的功能:

  • 主动给用户推送消息
  • 其它高级功能

Release:

  • receive message format
  • response message format
  • response text message with tuling123.com
  • response image message with how-old.net

Todo:

  • response message like voice, video/shortvideo, location, link
  • receive event format
  • response event
  • customise menu

enterprise_api

This django application is the API for wechat enterprise account.

微信企业号有两种调用模式:

  • 主动调用(可用于主动给用户发送消息)
  • 回调模式(类似于订阅号和服务号的被动响应用户发送的消息和事件)

已实现的功能:

  • 回调模式接收用户发送的文本消息,用图灵机器人回复.
  • 回调模式接收用户发送的图片消息,用微软的how-old回复性别和年龄.

待实现的功能:

  • 回调模式接收用户发送的其它类型消息并回复.
  • 回调模式接收用户发送的事件并回复.
  • 主动调用模式给用户推送消息.

暂不实现的功能:

  • 认证接口
  • 资源接口
  • 其它能力接口的高级功能

Release:

  • receive message format
  • response message format
  • send message format

Todo:

  • response text message with tuling123.com
  • response image message with how-old.net
  • send message
  • receive event format
  • response event message

python开发微信公众号

  1. 注册微信公众号(订阅号/服务号,企业号, 小程序)
  2. 在微信公众平台(开发->基本配置)修改服务器配置,URL添加你的代码的URL,Token添加你代码中的Token。
  3. 用git管理代码提交到URL,或者部署到云服务器。

微信开发文档:

[wechat docs]

流程:

wechat user <=> send/receive message <=> wechat server <=> POST XML message <=> your server

云平台部署django项目

[SAE]

[BAE]

SAE添加第三方依赖:

[SAE vendor]

BAE添加第三方依赖:

$vim requirements.txt
django==1.8.2
...

使用mysql:

[SAE mysql]

SAE平台需要config.yaml和index.wsgi两个文件.

SAE的入口就是index.wsgi文件中名叫application的可调用对象。

BAE平台需要app.conf,favicon.ico和index.py三个文件。

BAE的入口就是index.py文件中名叫application的可调用对象。

创建django项目wechat

app.conf或conf.yaml添加配置文件。

index.wsgi或index.py添加云平台入口。

wechat/settings.py添加mysql数据库信息。

在GAE/BAE/SAE设置mysql,在项目添加mysql的参数。

用migrate同步本地数据库后,用下面命令导出本地数据为sql文件:

$ python manage.py migrate
$ mysqldump -u <username> -p <databasename> > <filename>.sql

在GAE/BAE/SAE上传sql文件把数据同步到GAE/BAE/SAE的mysql。

创建django的应用django-wechat-api

在wechat/settings.py中添加应用。

在wechat/urls.py中添加应用的url。

在django-wechat-api/views.py添加微信接口。

修改wechat公众平台配置

配置:

SAE_URL: http://<project>.applinzi.com/wechat/
BAE_URL: http://<project>.duapp.com/wechat/
TOKEN: yourtoken

django开发的wechat接口开源项目

源码参考:

[github]

Indices and tables