Send Template Message

POST

https://api.itniotech.com/wa/template/sendMsg

Send a template message according to the created template
 
Request Parameters
Parameters Description Required Type
appId Application id Yes String
businessPhone Business number Yes String
recipient Recipient phone number Yes string
channelType Channel type: 0-WhatsApp, send 0 by default Yes Integer
template   No Object
name Template name Yes String
language   No Object
code Template language enumeration Yes String
components   No List
type Component type: header,body,button. Required when the body or header has variables No  
parameters Variable. Required when the body or header has variables No List
type When the type under components is header, the types are: text, image, video, document, location; When it is body, the types include text, currency, date_time. Yes String
text The text content corresponding to the variable. Required when variable type=text No String
payload The payload content corresponding to the variable. Required when variable type=payload No String
image Required when variable type=image No Object
link Image link Yes String
video Required when variable type=video No Object
link Video link Yes String
document Required when variable type=document No Object
link Document link Yes String
filename Filename No String
location Required when variable type=location No Object
latitude Latitude Yes String
longitude Longitude Yes String
name Location name Yes String
address Address description Yes String
 
Request Sample
Request URL:
    https://api.itniotech.com/wa/template/sendMsg
Request Method:
    POST
Request Headers:
    Content-Type: application/json;charset=UTF-8
    Sign: 05d7a50893e22a5c4bb3216ae3396c7c
    Timestamp: 1630468800
    Api-Key: bDqJFiq9
Request Body:
{
    "appId": "xJdRmKR3",
    "channelType": 0,
    "recipient": "91856321432",
    "businessPhone": "91856321412",
    "template":
    {
        "components": [{
            "type": "body",
            "parameters": [
                {
                    "type": "text",
                    "text": "hello world"
                }
            ]
        }],
        "language": {
            "code": "zh_TW"
        },
        "name": "new_shop"
    }
}
 
Response Parameters
Parameters Description Type
status Status code, 0 is successful, other failures refer to the interface response code String
reason Success or failure description String
data Send result Object
channelType Channel type: 0-WhatsApp, default 0 Integer
messageId Message id String
timestamp Timestamp (second level) Long
 
Response Status Code
status Description
-44 Failed to send template message,currently approved template language was not found.
-68 Insufficient balance
-69 The rate does not exist
-70 The recipient number is too long
-71 The recipient number is too short
-72 The recipient number format is incorrect. It must be a number
-93 The businessPhone number is too long
-94 The businessPhone number is too short
-95 The businessPhone number format is incorrect. It must be a number
-96 Business number does not exist
-97 Business number status is abnormal
-99 The channelType error !
-110 The body number of variables requested does not match the template definition. Please check the variable settings in the template
-111 he header number of variables requested does not match the template definition. Please check the variable settings in the template
-112 The channelType only can be 0 in param
 

language

Java

PHP

REQUEST

package com.itniotech.api.demo.im;

import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

private static void sendTemplateMsg() {
    final String baseUrl = "https://api.itniotech.com/wa/";
    final String apiKey = "your api key";
    final String apiPwd = "your api secret";
    final String appId = "your appid";

    final String url = baseUrl.concat("template/sendMsg");

    HttpRequest request = HttpRequest.post(url);

    final String datetime = String.valueOf(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());

    final String sign = SecureUtil.md5(apiKey.concat(apiPwd).concat(datetime));
    request.header(Header.CONNECTION, "Keep-Alive")
            .header(Header.CONTENT_TYPE, "application/json;charset=UTF-8")
            .header("Sign", sign)
            .header("Timestamp", datetime)
            .header("Api-Key", apiKey);

    Map<string,string> parametersMap = MapUtil.builder("type", "text").put("text", "测试文本").build();
    JSONObject jsonObject = new JSONObject();
    jsonObject.putOpt("type", "body");
    jsonObject.putOpt("parameters", Arrays.asList(parametersMap));


    final String templateName = "template name"; //the template name
    final String language = "language code"; // the template support language

    Map<string, object> templateMap = new HashMap<>();
    templateMap.put("name", templateName);
    templateMap.put("language", MapUtil.builder("code", language).build());
    templateMap.put("components", Arrays.asList(jsonObject));

    final int channelType = 0;
    final String businessPhone = "business phone";
    final String recipient = "accept phone";

    String body = JSONUtil.createObj()
            .set("appId", appId)
            .set("businessPhone", businessPhone)
            .set("channelType", channelType)
            .set("recipient", recipient)
            .set("template", templateMap)
            .toString();

    HttpResponse response = request.body(body).execute();
    if (response.isOk()) {
        String result = response.body();
        System.out.println(result);
    }
}        
                

REQUEST

header('content-type:text/html;charset=utf8');

$apiKey = "your api key";
$apiSecret = "your api secret";
$appId = "your appid";
$timeStamp = time();
$sign = md5($apiKey.$apiSecret.$timeStamp);
$headers = array('Content-Type:application/json;charset=UTF-8',"Sign:$sign","Timestamp:$timeStamp","Api-Key:$apiKey");

$url = "https://api.itniotech.com/wa/template/sendMsg";

$dataArr["appId"] = $appId;
$dataArr["businessPhone"] = "business phone";
$dataArr["recipient"] = "accept phone";
$dataArr["channelType"] = 0;

$componentsArray = array(
    "type" => "body",
    "parameters" => array(
        "type" => "text",
        "text" => "test words"
    ),
);
$templateArray = array(
    "name" => "template name",
    "language" => array(
        "code" => "language code"
    ),
    "components" => $componentsArray
);

$dataArr["template"] = $templateArray;

$data = json_encode($dataArr);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 600);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

$output = curl_exec($ch);
curl_close($ch);
var_dump($output);        
                
 

RESPONSEEXAMPLE

{
    "appId": "xJdRmKR3",
    "channelType": 0,
    "recipient": "91856321412",
    "businessPhone": "631111200999",
    "template": {
    "components": [
            {
                "type": "body",
                "parameters": [
                    {
                        "type": "text",
                        "text": "test words"
                    }
                ]
            }
        ],
        "language": {
        "code": "zh_TW"
        },
        "name": "new_shop"
    }
}