首页 > API中心 > IVR群呼 > 文档指引

 
 

发送模板消息

POST
https://api.itniotech.com/wa/template/sendMsg
根据创建的模板来发送模板消息
 
请求参数
参数 说明 是否必填 类型
appId 应用Id 是 String
businessPhone 商家号码 是 String
recipient 接收者电话号码/用户BSUID 是 string
channelType 通道类型:0-WhatsApp,默认送0; 是 Integer
template   否 Object
name 模板名称 是 String
language   否 Object
code 模板语言枚举 是 String
components   否 List
type 组件类型:header,body,button,当body或者header有变量时必填。 否 String
sub_type 按钮类型,枚举为url、quick_reply,当type=button时必填 否 String
index 按钮的序号,枚举0-9,当type=button时必填 否 String
parameters 变量,当body或者header有变量时必填 否 List
type 当components下type为header时类型有:text、image、video、document、location;
当为body时类型有text、currency、date_time;
type=button且sub_type=url取值为text;
type=button且sub_type=quick_reply取值为payload。
是 String
text type为text时的文本内容,当变量类型type=text时必填。 否 String
payload type为payload时的文本内容,当变量类型type=payload时必填。 否 String
image 当变量类型type=image时必填 否 Object
link 图片的URL链接 是 String
video 当变量类型type=video时必填 否 Object
link 视频的URL链接 是 String
document 当变量类型type=document时必填 否 Object
link 文档的URL链接 是 String
filename 文件名称 否 String
location 当变量类型type=location时必填 否 Object
latitude 纬度 是 String
longitude 经度 是 String
name 位置名称 否 String
address 地址 否 String
 
请求示例
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": "9.1日开业大吉"
                }
            ]
        }],
        "language": {
            "code": "zh_TW"
        },
        "name": "new_shop"
    }
}
 
轮播模板-请求参数说明
参数 说明 是否必填 类型
appId 应用Id 是 String
businessPhone 商家号码 是 String
recipient 接收者电话号码 是 String
channelType 通道类型:0-WhatsApp 是 Integer
template 模板消息内容 是 Object
name 模板名称 是 String
language 模板语言 是 Object
code 模板语言枚举 是 String
components 模板组件 是 array
type 组件类型,"body" 正文模板有变量必填 String
parameters 变量数组 是 Array
type 变量类型,取值为"text" 是 String
text 变量对应的文本内容,不超过1024个字符 是 String
type 组件类型,"carousel" 是 String
cards 卡片列表 是 Array[object]
card_index 图卡在图卡轮播中应显示的顺序,索引以 0 开始。0 表示第一张图卡,1 表示第二张图卡,以此类推。 必填,取值0-9 Integer
components 轮播卡片模板组件列表 是 Array
卡片页眉媒体参数对象    
type 组件类型,"header" 是 String
parameters 变量数组 是 Array
type 变量类型,取值为image、video 是 String
image 图片媒体对象 当变量类型type=image时必填 Object
link 素材链接 当变量类型type=image时必填 String
卡片正文变量参数对象    
type 组件类型,"body" 卡片正文有变量时必填 String
parameters 变量数组 是 Array
type 变量类型,取值为text 是 String
text 变量对应的文本内容,不超过64个字符 是 Object
卡片按钮变量参数对象    
type 组件类型,"button" 卡片按钮有变量必填 String
sub_type 按钮类型,枚举为url、quick_reply 是 String
index 按钮的序号,枚举0-1 是 String
parameters 变量数组 是 Array
type 变量类型。sub_type=url取值为text;sub_type=quick_reply取值为payload 如果网址按钮的网址使用变量,必填 String
text 变量对应的文本内容 如果网址按钮的网址使用变量必填 String
payload 轻触按钮时会加入到消息 Webhook中的值。 否 String
 
轮播模板-请求示例
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"
  }
}
 
响应参数
参数 说明 类型
status 状态码,0成功,其他失败参见响应状态码说明 String
reason 成功或失败描述 String
data 发送结果 Object
channelType 通道类型:0-WhatsApp,默认0; Integer
messageId 消息id String
timestamp 时间戳(秒级) Long
 
响应状态码
status 状态说明
-44 发送模板消息失败,模板语言未找到
-68 余额不足
-69 费率不存在
-70 接收人号码长度不能大于14位
-71 接收人号码长度不能小于7位
-72 接收人号码格式错误,它必须为数字
-93 商家号码长度不能大于14位
-94 商家号码长度不能小于7位
-95 商家号码必须为纯数字
-96 商家号码不存在
-97 商家号码状态错误
-99 通道类型错误
-110 body变量个数与模板定义不匹配。请检查模板中的变量设置
-111 header变量个数与模板定义不匹配。请检查模板中的变量设置
-112 channelType在参数中只能为0
-259 轮播模板主消息不支持标头、页脚和按钮组件
-300 轮播模板卡片顺序参数不能为空,取值为0-9
-301 轮播模板卡片页眉组件类型必须为image或video,且需与模板配置一致
-302 轮播模板卡片只能包含正文、页眉、按钮组件参数
-303 轮播模板卡片按钮索引不能为空
-323 轮播模板卡片按钮类型必须与模板按钮类型一致
-324 轮播模板卡片正文变量值不能超过64个字符
-325 轮播消息卡片列表不能为空
-326 轮播消息卡片components不能为空
-327 轮播消息卡片中页眉不能为空
-328 轮播消息卡片中页眉媒体参数不能为空
-332 轮播消息卡片数量需与模板一致
-333 轮播消息卡片索引不能重复,并且需要从0开始且连续
 

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 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 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" => "测试文本"
    ),
);
$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": "91856321432",
    "businessPhone": "91856321412",
    "template": 
    {
        "components": [{
            "type": "body",
            "parameters": [{
                "type": "text",
                "text": "测试文本"
            }]
        }],
        "language": {
            "code": "zh_TW"
        },
        "name": "new_shop"
    }
} 

Telegram

微信扫码

立即咨询

客户服务热线

400-999-8488