In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the process of developing a lottery program based on the official account with Python. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
This article assumes that you already have your own official account, and those who do not have an official account can register one on the platform at the address: http://mp.weixin.qq.com.
First log in to the official account platform and fill in the server configuration
URL can not be randomly filled in, this is your server address, such as: http://123.33.44.55/wx, IP address is the server's public network IP, so you must have another server.
You can choose a domestic web hosting service provider, such as Aliyun, Tencent Cloud or Ucloud (remember to give me advertising fees).
After filling in, direct submission will fail verification, because the server has not yet been connected.
So first verify the validity of the server address.
Verifying the validity of the server address is the following processing logic. I am using the flask framework here.
When a Wechat server requests URL, it will send you four parameters, namely
The so-called verification of the validity of the server address is to confirm whether the request comes from the Wechat server, because the request may also be initiated by a hacker. We can verify the request by verifying signature. If we confirm that the GET request is from Wechat server and return the echostr parameter content as is, the connection takes effect, which means the developer succeeds, otherwise the connection fails.
The encryption / verification process is as follows:
1) sort the three parameters token, timestamp and nonce in lexicographic order
2) concatenate three parameter strings into one string for sha1 encryption
3) the encrypted string obtained by the developer can be compared with signature to indicate that the request originated from Wechat.
After deploying the code to your own server, submit it at this time, which will prompt you to submit successfully. After the validation is successful, you can begin to implement the business logic.
Realize the lottery logic
When the user sends a message on the official account, Wechat will forward the message to the business server in the form of POST request (that is, our own program that handles the business logic), and then we will carry out the corresponding logical processing according to the content sent by the user.
Wechat will send it to our business server through POST request in the format of xml, including the content, time and open_id of the message sent, which is the only corresponding user of open_id. Although you don't know the nickname of the user, as long as you know open_id, you can only determine which user it is.
Define the data model
To realize the lottery system, we first define the data model.
Only three fields are enough. Number is the lottery number. Open_id indicates who won the lottery number, which is initially empty, and valid indicates whether the number is the winning number. When initializing the system, we will randomly set valid with True for some data. For example, if you want to draw 3 prizes, you will randomly set 3 data as True.
After the model is established, you can then deal with the specific business logic. When the user sends the raffle keyword, we have three things to do.
1. Judge whether the current user is still eligible for the lucky draw.
2. Generate lucky draw numbers
3. Query the remaining lucky draw times.
When replying to another keyword to query whether it has won the lottery, we just need to determine whether it has a matching valid of True in its lucky draw number. All this logic is encapsulated in models, so it doesn't look like a lot of code.
The above is the whole train of thought of the whole lucky draw Mini Program. All that's left is to deploy the code to the server and achieve the effect at the beginning of the article.
This is how the editor shared the process of developing a lottery program based on the official account with Python. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.