WeChat public account template message push test Python version without server - nanny level tutorial

There is a project in hand, the server is hung with automated crawlers, but the IP agent I used did not run out of alarms, resulting in several IP runs out, the program could not crawl the data, and the process ended in an endless loop. I thought about sending an email to remind me before, but the email was not timely, and I always forgot to read it, because I rarely use QQ, and the most used one is WeChat. I will open WeChat to read every day no matter what, so I thought of the official account to remind. After looking at the document, I found that the template message push of the WeChat official account met my needs, so I started working. Follow the steps of the full text step by step, and it will definitely be done.
insert image description here

This article is only used as a test, and a full tutorial on the automatic push of the Django version of the server will be released later

Apply for a test number

The first is to register a WeChat official account, just register casually, but if you want to use it online, please remember to apply for a service account. As for the difference between the service account and the subscription account, I will not go into details here. If you have relevant needs, you can go to WeChat official view.

After the registration is complete, click on 设置与开发the bottom of the left side 接口权限to simply see the categories of official accounts required for these related functions. Generally, what we apply for is an individual number, which basically has no functions, so we can only post articles. In addition, if you want to officially go online to do automatic push, you need official account certification. This thing costs 300 yuan, but I think tb has that kind of a few cents. I don’t know the risks. I can understand message. If the project is serious, the company will pay for it. Making tools yourself can save money and find that kind of certification.
insert image description here


After registering the official account, start to enter the test account
location: 开发者工具under the left navigation bar公众平台测试账号

insert image description here

After entering, you will be given an appID and an appsecret , which are key parameters and will be used for testing later. Don’t worry about the configuration information of this interface. It’s a local test for the time being, and then configure this thing after going online.

insert image description here


New test template


Click to add a test template, the following is the correct format

提示前缀+ { {xxx.DATA}}
Then you remember to change the line after typing one. When the time comes to send the notification template, the typesetting will be the same as yours now
insert image description here

After submitting, you will get a template ID (the code will be used later)
insert image description here


Finally, scan the QR code to follow the test official account.
After you follow, you will see your WeChat nickname and a WeChat account. This WeChat account is the only proof of your WeChat account in this test official account. Also known as openId
insert image description here






So far there are 4 values, namely appID, appsecret, 模板ID,openId



Analysis of implementation steps: (key point)
First of all, you need to clarify a point. To interact with WeChat api, you need to obtain a temporary token,
and then you can access whatever interface you want to do, and see if you want to clear the document. POST or GET request, where is the request Add the token, and then what parameters should be carried when requesting this URL,
and finally send this request directly, and the WeChat server will do what you want to do for you

Attach the key API documentation of this article (you can read it if you want to delve deeper):

Get access token:
https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html

Send a template message:
https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#%E5%8F%91%E9%80%81%E6%A8%A1%E6%9D%BF%E6%B6%88%E6%81%AF



Full code:

import requests
import json

appID = "wx8ac3ded9236efe2a"
appSecret = "131b8d9d874af6f7dbf11d3b751ce6b2"
openId = "oHB4K6oJqzBh9FoM2J87KEfpS3Ro"


def get_access_token():
    # 获取access token的url
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}' \
        .format(appID, appSecret)
    response = requests.get(url).json()
    print(response)
    access_token = response.get('access_token')
    return access_token


def send_msg(access_token):
    # touser 就是 openID
    # template_id 就是模板ID
    # url 就是点击模板跳转的url
    # data就按这种格式写,time和text就是之前{
    
    {time.DATA}}中的那个time,先试试你就知道了,value就是你要替换DATA的值
    body = {
    
    
        "touser": openId,
        "template_id": "JHzQALjvNyjbqKehXZbtBYS-l_e1oowCeZrpTHQmN7U",
        "url": "http://weixin.qq.com/download",
        "data": {
    
    
            "time": {
    
    
                "value": "今天是8月11号",
                "color": "#173177"
            },
            "text": {
    
    
                "value": "微信官方文档写的真的不清晰",
                "color": "#173177"
            }
        }
    }
    url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}'.format(access_token)
    print(requests.post(url, json.dumps(body)).text)

if __name__ == '__main__':
    # 1.获取access_token
    access_token = get_access_token()
    # 2.发送模板消息
    send_msg(access_token)

Code analysis:
In fact, the comments are also written very clearly, and the four values ​​all play a key role. If you want to use it online, it is basically the process. If you want more usage methods of template messages and more sao operations, please refer to the document I posted above. Write it like this first, after the test is successful, you can try to change here and there to see what changes, and you will understand. If you don’t understand, leave a message, CSDN opened the mailbox to remind you to see it soon


Effect display:
computer terminal
insert image description here

Guess you like

Origin blog.csdn.net/qq_44718932/article/details/132223216