Home  >  Article  >  WeChat Applet  >  How to build WeChat public platform using python

How to build WeChat public platform using python

高洛峰
高洛峰Original
2017-03-19 17:57:261652browse

This article mainly introduces the relevant information and techniques for building a WeChat public platform in Python. The article gives detailed steps for building a WeChat public platform in Python. Interested friends can refer to it.

This article is mainly about Teach you step by step how to use python to build a WeChat public platform. Friends who are interested can refer to the tools used in

, python Sina SAE platform, WeChat’s public platform

You need to first log in to WeChat There are various registrations on the public platform and Sina SAE platform. When registering on the WeChat platform, you need to take a photo of holding your ID card, and there is still a few days of review period

WeChat public platform: http://mp.weixin .qq.com

Sina SAE: http://sae.sina.com.cn/

Wait for the WeChat public review to pass, log in to the public platform, and click Advanced Functions. You will see that you need to provide access information:

How to build WeChat public platform using python

WeChat interface configuration

Then we need a URL as the interface (this When you need to build a Python application on SAE), Token is equivalent to the "password" agreed between us and WeChat. You can fill in English or numbers here, but in actual testing, there are sometimes problems when entering pure numbers, so it is still characters. The string is relatively reliable.

The first step, Build a python application on SAE, select the python application in the application in the picture below.

How to build WeChat public platform using python

# Fill in the second-level domain name and application name, etc., and select the language. Here we use Python to develop selected web applications. After creating the application, create a new version in code management. We can then choose to edit the code. Online editing can be achieved without configuring the local environment, SVN, etc. Of course, a lightweight online editor like this will suffice. SVN is not as easy to use as online editing.

The second step is to write index.wsgi

Because we are using the web.py framework because of its good xml parsing.

First write config.yaml

name: yangyanxing
version: 1
 
libraries:
- name: webpy 
 version: "0.36"
 
- name: lxml
 version: "2.3.4"
 
...

Pay attention to strict indentation, if you miss one space, it will be useless! And it is difficult to find problems during debugging. . .

Then we continue to write index.wsgi

# coding: UTF-8
import os
 
import sae
import web
 
from weixinInterface import WeixinInterface
 
urls = (
'/weixin','WeixinInterface'
)
 
app_root = os.path.dirname(__file__)
templates_root = os.path.join(app_root, 'templates')
render = web.template.render(templates_root)
 
app = web.application(urls, globals()).wsgifunc()  
application = sae.create_wsgi_app(app)

A brief explanation,

from weixinInterface import WeixinInterface
Here we need to create another py file of weixinInterface. You can also write this class in the index.wsgi file, but it will look messy

Create a new weixinInterface.py file, pay attention to capitalization, and write the following code

# -*- coding: utf-8 -*-
import hashlib
import web
import lxml
import time
import os
import urllib2,json
from lxml import etree
 
class WeixinInterface:
 
 def __init__(self):
  self.app_root = os.path.dirname(__file__)
  self.templates_root = os.path.join(self.app_root, 'templates')
  self.render = web.template.render(self.templates_root)
 
 def GET(self):
  #获取输入参数
  data = web.input()
  signature=data.signature
  timestamp=data.timestamp
  nonce=data.nonce
  echostr=data.echostr
  #自己的token
  token="yangyanxing" #这里改写你在微信公众平台里输入的token
  #字典序排序
  list=[token,timestamp,nonce]
  list.sort()
  sha1=hashlib.sha1()
  map(sha1.update,list)
  hashcode=sha1.hexdigest()
  #sha1加密算法  
 
  #如果是来自微信的请求,则回复echostr
  if hashcode == signature:
   return echostr

A GET method is defined here, which is based on WeChat The public platform requires token verification. Because here we define templates_root as the templates in the root directory, we also need to create a directory of templates in the root directory

How to build WeChat public platform using python

Because WeChat sends the verification information GET, so the GET method is used here to obtain the value and return the corresponding application value

Save everything, and now return to WeChat’s public platform advanced management interface

WeChat interface configuration

Fill in the name of your application in Sina SAE in the url and add /weixin, such as: http://XXXX.sinaapp.com/weixin token Enter whatever you want, just pay attention to changing weixinInterface.py Just enter the token in. After entering it, click Submit. If there are no problems, it will pass the verification!

How to build WeChat public platform using python

The third step, create a simple automatic reply method, parroting it, is the user Whatever you say, it will reply something, it’s of no use, just for fun!

Continue to add code in weixinInterface.py

def POST(self):  
  str_xml = web.data() #获得post来的数据
  xml = etree.fromstring(str_xml)#进行XML解析
  content=xml.find("Content").text#获得用户所输入的内容
  msgType=xml.find("MsgType").text
  fromUser=xml.find("FromUserName").text
  toUser=xml.find("ToUserName").text
  return self.render.reply_text(fromUser,toUser,int(time.time()),u"我现在还在开发中,还没有什么功能,您刚才说的是:"+content)

This def is at the same level as the previous GET, pay attention to the indentation

Then we create the reply_text.xml template file in the templates directory and write the following code

$def with (toUser,fromUser,createTime,content)
<xml>
<ToUserName><![CDATA[$toUser]]></ToUserName>
<FromUserName><![CDATA[$fromUser]]></FromUserName>
<CreateTime>$createTime</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[$content]]></Content>
</xml>

Note that toUser and fromUser here are opposite to what was posted just now, because toUser here is fromUser in the POST function, fromUser here is also toUser in the POST function, msgType is text

all Save, now use your personal WeChat to follow the public WeChat account you created, and then enter some content. If there are no questions, you will receive a parrot reply!

The above is all the content of building WeChat public platform in python. You can build it according to the above steps.

The above is the detailed content of How to build WeChat public platform using python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn