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:
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.
# 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
1 2 3 4 5 6 7 8 9 10 11 |
|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
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
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!
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
1 2 3 4 5 6 7 8 |
|
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
1 2 3 4 5 6 7 8 |
|
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!