Home > API Center > API interface guide

 

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"
    }
}
 
Carousel Template - Description of request parameters
Parameter 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   Yes Object
name template name Yes String
language template language Yes Object
code template language enumeration Yes String
components   Yes array
type Component type:body The body section is required if it contains variables. String
parameters variable Yes Array
type Variable type:text Yes String
text The text content corresponding to the variable,No more than 1024 characters. Yes String
type Component Type:carousel Yes String
cards Card List Yes Array[object]
card_index The order in which the cards should be displayed in the card carousel is indexed starting with 0. 0 represents the first card, 1 represents the second card, and so on. Yes, the value ranges from 0 to 9. Integer
components Carousel Card Template Component List Yes Array
Card Header Media Parameter Object    
type Component Type:header Yes String
parameters Variable array Yes Array
type The variable type can take the values image or video. Yes String
image Image media object Required when the variable type is image Object
link Material link Required when the variable type is image String
video Video media object Required when the variable type is video Object
link Material link Required when the variable type is video String
Card Body Variable Parameter Object    
type Component type:body Required fields when there are variables in the card text. String
parameters Variable array Yes Array
type Variable type:text Yes String
text The text content corresponding to the variable,No more than 64 characters. Yes Object
Card Button Variable Parameter Object    
type Component Type:button If the card button has a variable, then it is required. String
sub_type Button type, enumerated is url、quick_reply Yes String
index button index 0-1 Yes String
parameters Variable array Yes Array
type Variable type. "sub_type" is the URL value, which is text. If the URL for the URL button is a variable, it is required. String
text The text content corresponding to the variable If the URL for the URL button is a variable, it is required. String
payload The value is added to the message webhook when the button is tapped. String
 
Carousel Template - Request Example
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": "lYOxMDYC",
  "channelType": 0,
  "recipient": "8615986688129",
  "businessPhone": "12026693133",
  "template": {
    "components": [
      {
        "type": "body",
        "parameters": [
          {
            "type": "text",
            "text": "9.1日开业大吉"
          }
        ]
      },
      {
        "type": "carousel",
        "cards": [
          {
            "card_index": 0,
            "components": [
              {
                "type": "body",
                "parameters": [
                  {
                    "text": "打折",
                    "type": "text"
                  }
                ]
              },
              {
                "type": "header",
                "parameters": [
                  {
                    "type": "image",
                    "image": {
                      "link": "https://www.luckyshrub.com/assets/blue-echeveria.jpeg"
                    }
                  }
                ]
              },
              {
                "type": "button",
                "sub_type": "url",
                "index": "0",
                "parameters": [
                  {
                    "type": "text",
                    "text": "urlVal"
                  }
                ]
              },
              {
                "type": "button",
                "sub_type": "quick_reply",
                "index": "1",
                "parameters": [
                  {
                    "type": "payload",
                    "payload": "more-aloes"
                  }
                ]
              }
            ]
          },
          {
            "card_index": 1,
            "components": [
              {
                "type": "body",
                "parameters": [
                  {
                    "text": "2打折",
                    "type": "text"
                  }
                ]
              },
              {
                "type": "header",
                "parameters": [
                  {
                    "type": "image",
                    "image": {
                      "link": "https://www.luckyshrub.com/assets/blue-echeveria.jpeg"
                    }
                  }
                ]
              },
              {
                "type": "button",
                "sub_type": "url",
                "index": "0",
                "parameters": [
                  {
                    "type": "text",
                    "text": "2urlVal"
                  }
                ]
              },
              {
                "type": "button",
                "sub_type": "quick_reply",
                "index": "1",
                "parameters": [
                  {
                    "type": "payload",
                    "payload": "2more-aloes"
                  }
                ]
              }
            ]
          }
        ]
      }
    ],
    "language": {
      "code": "en"
    },
    "name": "template_marketing_api_20"
  }
}
 
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
-259 The main message of the carousel card template does not support headers, footers and button components.
-300 template message carousel card index cannot be empty, and must be 0-9
-301 The header component type of the carousel template card must be either "image" or "video", and it must be consistent with the template configuration.
-302 carousel component.type must be one of the types header、body、button
-303 carousel button template message index cannot be empty
-323 The buttons in the carousel card message are inconsistent with those in the corresponding template
-324 variable value cannot exceed 64 characters
-325 carousel message cards cannot be empty
-326 carousel message card components cannot be empty
-327 carousel message card header cannot be empty
-328 carousel message card header media parameters cannot be empty
-332 carousel message card count must be equal to template
-333 carousel message card_index cannot be repeated, and needs to start from 0 and be continuous
 

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"
    }
}