SoFunction
Updated on 2024-10-30

python development wechat service number message push example

preamble

Today talk about the WeChat service number ofpush message functionBecause recently there happens to be such a need to push messages to users through WeChat, personal number or business personal number did not go to the detailed study, looked at a roughly need to log in first in the function of sending messages, slightly troublesome, over. Just have a certified service number, directly on the hand!

Message push is currently divided into two kinds, one is a template message, one is a subscription notification. Both have experienced, subscription notification is more pitiful, because he is required to subscribe to the user to click once to send a time, I do not have much use (more for the kind of class notification lottery notification what)

My demand is in the user needs, and then can push a daily exchange rate notification, after testing the template message can achieve this function, but the disadvantage is that you can not customize the title and keywords, if you need to customize the words have to be with the official application to allow them to audit before they are allowed, the following picture is the audit notification was rejected at that time.

Since it can not be customized, you can only find a similar template, but also want to spit is that WeChat on the screening template this piece of work is not satisfactory, can only be turned over page by page, or according to the title of the search, but the relevant industry and there is no corresponding, and finally reluctantly find a such a

So what does the final finish look like? Pictures

main body (of a book)

The overall idea is quite clear, the data are transmitted through the appropriate API, according to the official documents related to

Get Access token from APPID and APPSECRET of the service number.

Use the Access token to use the corresponding template, fill in the information you want to send, and then send it to the corresponding user.

Step-by-step introduction to pictures

# Get access_token
def get_access_token():
    ## Since I'm reading from a file, you guys can just fill in the two in curly brackets in the link if you don't mind, and remove the .format (xxx) that follows it
    # appId
    app_id = config["app_id"]
    # appSecret
    app_secret = config["app_secret"]
    post_url = ("/cgi-bin/token?grant_type=client_credential&appid={}&secret={}"
                .format(app_id, app_secret))
    ## Try to get and convert to json using get, and take the value of the
    try:
        access_token = get(post_url).json()['access_token']
    except KeyError:
        print("Failed to get access_token, please check if app_id and app_secret are correct")
        ("pause")
        (1)
    print(access_token)
    return access_toke
## Send a message
def send_message(to_user, access_token, money):
    # This url is a link to a link that contains an access_token for subsequent posts.
    url = "/cgi-bin/message/template/send?access_token={}".format(access_token)
    now = ()
    date = ("%m month %d day")
    # Here's the data that needs to be sent
    data = {
        "touser": to_user,
        "template_id": config["template_id"],
        # This url is the jump after clicking on the details, here jump to Baidu exchange rate history
        "url": "/foreign/global-CNYTWD",
        "data": {
            # where thing15 these follow the parameters in the template details
            "thing15": {
                "value": "Exchange rate notification"
            },
            "thing16": {
                "value": "1 CNY = {} TWD".format(money)
            },
            "time9": {
                "value": date
            },
            "thing2": {
                "value": "Click on details to see historical exchange rates"
            }
        }
    }
    # Here's the push message
    response = post(url, json=data).json()
    if response["errcode"] == 40037:
        print("Push message failed, please check if the template id is correct")
    elif response["errcode"] == 40036:
        print("Push message failed, please check if template id is empty")
    elif response["errcode"] == 40003:
        print("Push message failed, please check if the micro-signal is correct")
    elif response["errcode"] == 0:
        print("Push message successful.")
    else:
        print(response)

The overall idea is this way, then I just need to realize that it is timed to push, or the subsequent increase in the lower than a certain value push, etc., but this is the subsequent increase in the other needs, but there is a problem is that only their own plus to notify which users, in fact, there should be a page to allow the user to choose whether or not they need this push. However, in this regard, I feel that it is necessary to build a platform or a simple record, slightly troublesome, temporarily over.

postscript

Although the function is simple, but it is also a way through, and then on this basis can add more functions, only this record, learning is never-ending.

consultation/doc/offiaccount/Basic_Information/Get_access_token.html

Above is the details of python development wechat service number message push example, more information about python wechat service number message push please pay attention to my other related articles!