WeChat development - push template message

WeChat template message push

 

The WeChat template message is only used by the official account to send important service notifications to users, and can only be used in service scenarios that meet its requirements, such as credit card swiping notifications, product purchase success notifications, etc. Marketing messages such as advertisements and all other messages that may cause harassment to users are not supported.

 

So, how to send template message through template message interface. It is roughly divided into three parts:

  1. Apply for a template message function plug-in
  2. Construct template message body
  3. Send template message

 

1. Apply for adding a template message function plug-in

 

Template messages are only available to authenticated service IDs.

In the background of the WeChat public platform, go to "Function->Add Function Plug-in->Template Message" in turn to apply for a template message.

 

 Click to apply


 

When applying, you need to select 2 industries where the official account service is located. You can change the selected industry once a month, and the account can only use the relevant templates in the industry you belong to.


Submit, wait for the application to be approved, you can see the template message list in the template library


Enter the template you want to use and click Add



After adding, it will be stored in the "My Templates" library


 
View the details of the template, you can see the id of the template and the name of each content parameter



Different template messages have different content structures. These IDs and field names will be used in the program.

Of course, if you can't find the template you need in the template library, you can click " Help us improve the template library " on the right side of the template library list page to customize the template.


 

After reading the "Guidelines Before Applying for Adding Template Messages", click OK to enter the Add Custom Template page, you can only customize three templates per month


Click Next to wait for the review. After the review is passed, you can use the customized template

 

Second, construct the template message body

 

Take a credit card payment reminder template as an example, its content is as follows:


And according to the WeChat template message interface, it can be known that the POST request parameter format is as follows:


According to the above content and format, construct the template message body as follows:

def templateData()
{
    def data = [:]
    data.put("first", [value: "Dear Zhang San, under your name: "])
    data.put("OPERATE", [value: "招商银行", color:"#173177"])
    data.put("AMT", [value: "1234", color:"#173177"])
    data.put("STATUS", [value: "13日", color:"#173177"])
    data.put("DATE", [value: "1234元", color:"#173177"])
    data.put("REMARK", [value: "备注:请及时还款,避免逾期"])
    
    def templateData = [:]
    templateData.put("touser", "ovBRNwCc6wigvmThgCEEOh7lRwMY")
    templateData.put("template_id", "hLIrwUMBOq0Er3AmhxUTCp_aOGk-uJnjVYgKSnRSSSg")
    templateData.put("url", "")
    templateData.put("data", data)
    return templateData
}

其中,touser 为要推送者的openid,template_id为所选消息模板的id

 

三、发送模板消息

 

模板消息是使用access_token作为授权来发送

 

获取access_token值,请参照:http://hellolove.iteye.com/blog/2333874

 

def sendMessage()
{
    def accessToken = getAccessToken()
    def templateData = templateData()
    def params = JsonOutput.toJson(templateData)

    String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN"
    requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken)
    URL url = new URL(requestUrl)
        
    HttpURLConnection connection = (HttpURLConnection) url.openConnection()
    connection.setDoOutput(true)
    connection.setRequestMethod("POST")
    connection.setRequestProperty("Content-Type", "application/json")
    connection.outputStream.withWriter { Writer writer -> writer.write params }
    
    if (connection.getResponseCode() == 200)
    {
        def result = connection.inputStream.withReader { Reader reader -> reader.text }
        return result
    }
    else
    {
        return 0
    }
}

 

调用发送方法,实现效果如下如示:



 

 

参考文档:

https://mp.weixin.qq.com/wiki/5/6dde9eaa909f83354e0094dc3ad99e05.html

http://www.cnblogs.com/txw1958/p/wechat-template-message.html
 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326499950&siteId=291194637