This article mainly describes how to use Python to develop the WeChat public platform. Note: If you are a Python novice or a web crawler novice, and find the code in this course obscure and difficult to understand, it doesn't matter. You don't need to understand it. Just follow the steps step by step. In this section, we mainly build a framework, and there is almost no real web crawler code. You may only use these codes and operations once in your life. In the next section, we will talk about how to expand our web crawler program based on this framework. Required Action:
Knowledge points to master:
How automatic reply worksFirst, let's understand what kind of mechanism can realize the automatic reply function of WeChat? (Not the automatic reply of the WeChat system) The principle is that the WeChat platform sends the text entered by the user to the cloud platform, and then the program running on the cloud platform captures this text information and returns a result, and then the cloud platform returns the result to the WeChat platform. Finally, the WeChat platform displays the returned result to the user. Let's use a picture to show it: WeChat developer mode and SAE settingsI will try to explain this section in detail. If there is still something unclear, you can send me a private message. First of all, we need support from two major platforms:
Specific steps: Application for WeChat public account.As long as you have an email address, you can apply for a personal subscription account for free. No more details. Application and setup of SAEAfter registering and logging in to SAE, select SAE Create a new project. SAE currently only supports Python 2.7, and Python 3 cannot be used for the time being. If the project is small, it is recommended to fill in SVN, because it can be edited online. If the project is large, then Git is the best choice. Here, SVN is selected. Creating the first version You can start editing now~ Write config.yaml and index.wsgi files. WSGI is Python Web Server Gateway Interface. We use the web.py framework. Other powerful frameworks of the same type include Django, Flask, etc. Why choose web.py? Because it is lightweight and has good XML parsing capabilities. As an aside, the developer of web.py, Aaron H. Swartz, was a genius, but unfortunately died young. There is a documentary about him that I recommend watching: The Internet's Son. Okay, let's get back to the topic. Let's first write config.yaml
Here we introduce the web.py framework and lxml module, and then we write the index.wsgi file.
Here is some simple knowledge of Python web development using web.py. Set up the root directory, template directory, /weixin route, and start the application. In order to make the page look neater, we created a new py file weixinInterface.py (weixinInterface.py and index.wsgi are in the same directory, see the screenshot below).
Let's briefly explain the code. def __init__(self) tells us where the template file is loaded. def GET(self) is a token verification required by the WeChat public platform. The verification here uses a hash algorithm. For details, please refer to the official WeChat interface access instructions: WeChat public platform access guide. There is a PHP example in it. This article uses Python implementation. WeChat developer mode settingsBasic Settings Modify the configuration The URL must be filled in carefully and checked carefully. For example, view url application information: Token must be the same as the one you just filled in Sina SAE. EncodingAESKey can be randomly generated. After filling in, click Submit. If it prompts "Submission successful". Congratulations, the most critical step has been completed. This stage may take a long time. After completion, be sure to enable the developer mode!!!! Remember!!! WeChat robot implementationAfter the previous step is completed, we can do something interesting: WeChat robot. But before that, there is still a small step to complete: template creation. Since WeChat development is in the form of XML. In order to realize automatic reply in text form first (later you can realize reply in the form of audio, text and pictures, etc.), first create a new template folder templates, and then create a reply_text.xml file in the templates folder (see the screenshot below for the file location). According to the passive reply of WeChat message, fill in the following code:
Then, write the POST function after def GET(self) in weixinInterface.py. This function is used to obtain the user's ID, the type of message sent, the time of sending, etc. Determine the type of message sent by the user. If it is a plain text type, if mstype == 'text', then you can proceed to the next step.
In order to implement WeChat robot, we need to implement automatic reply content. There are two ways to do this.
Here I choose the second method, which uses the Turing Robot API. This method is convenient and fast, and is generally not blocked. However, it has low freedom and poor scalability. Register a Turing robot account. Note that you should use Turing's web API instead of authorization. Get the key for Turing robot replies. You can get automatic replies from WeChat robots with just a few lines of code~ Source code displayindex.wsgi source code
config.yaml source code
reply_text.xml source code under templates
weixinInterface.py source code
PostscriptThis tutorial uses Python to develop a WeChat public platform that can automatically reply to user input text. It includes the settings of the WeChat public platform, the settings of SAE, and the writing of related codes. |
<<: It’s 2020 now. Which is better, fingerprint recognition or facial recognition?
As an information flow person in the automotive i...
With the arrival of winter vacation and Spring Fe...
There has always been a dispute over charging and...
The purpose of marketing activities is mainly to ...
This article was reviewed by Zhang Zhaomin, Maste...
Our classmates at Alibaba have always had a hat o...
A Brief Discussion on iOS Crash (Part 1) 1. Zombi...
Screen adaptation in front-end development is act...
"Best seller" and "heavy" are...
Tesla may really be facing a rival. Just days aft...
Some people say that online education is the most...
For all "idealistic" developers, succes...
Qualcomm, which was overshadowed by Huawei's ...
The mini program provides convenience for publici...
Author: Xue Qingxin, registered dietitian Reviewe...