创建消息模板

POST
https://api.itniotech.com/wa/template/add
用于消息模板的创建,然后根据模板来发送模板消息
请求参数
参数 说明 是否必填 类型
appId 应用id String
channelType 通道类型:0-WhatsApp,默认送0; Integer
templateName 模板名称:小写字母、数字和下划线、512字符; String
type 模板类型:0-AUTHENTICATION(身份验证);1-MARKETING(营销性);2-UTILITY(交易类); Integer
language 模板语言:如"zh_CN",详见国家语言枚举定义,一个模板名称可存在多种语言,一次只能创建一种语言 String
components 模板组件 Object
header 页眉 Object
type 组件类型:HEADER,默认HEADER String
format 页眉的类型:0-TEXT,默认0,type=HEADER时必填 String
text 最多60个字符,允许插入1个变量,type=HEADER,format=0时必填 String
example 页眉变量示例,当text中有变量时必填 Object
header_text 页眉文本变量示例,页眉有变量时必填 Array
body 组件类型 Object
type 组件类型:BODY,默认BODY String
text 最多1024个字符,允许插入变量,限制最多20个变量,模板类型为交易相关和营销时必填 String
add_security_recommendation 添加安全建议:true、false,模板类型为身份验证时必填 Boolean
example 正文变量示例,当text中有变量时必填 Object
body_text 正文文本变量示例,与text中变量个数一致,正文有变量必填 Array
footer 组件类型 Object
type 组件类型:FOOTER,默认FOOTER String
text 最多60个字符,模板类型为非身份验证时必传 String
code_expiration_minutes 验证码的失效时间,身份验证类模板有此项,值的可填范围是1-90 Integer
button 组件类型 Object
type 组件类型:BUTTONS,默认BUTTONS,模板类型为身份验证时必填 String
buttons 按钮对象 Object
type 按钮类型:3-OTP(模板类型为身份验证时可选)目前仅支持otp类型,默认3 Integer
text 按钮显示的名称,25字符以内 String
otp_type 验证码按钮类型:copy_code(复制验证码),默认copy_code String
请求示例
Request URL:
    https://api.itniotech.com/wa/template/add
Request Method:
    POST
Request Headers:
    Content-Type: application/json;charset=UTF-8
    Sign: 05d7a50893e22a5c4bb3216ae3396c7c
    Timestamp: 1690286812882
    Api-Key: 7Wqzu5rm

//创建营销类、header为text的模板,且按钮为电话和网址,正文一个变量(模板type类型随意搭配)
Request Body:
{
    "appId": "yYgJ5EVD",
    "channelType": 0,
    "templateName": "dd_template",
    "type": 1,
    "language": "zh_CN",
    "components":
    {
        "header":{
            "type": "HEADER",
            "format": 0,
            "text": "智能otp正在{{#}}活动中",
            "example": {
                "header_text": ["打折"]
            }
        },
        "body":{
            "type": "BODY",
            "text": "公司新开业, {{#}}活动火热进行中....",
            "example":{
                "body_text": [
                    ["打折"]
                ]
            }
        },
        "footer": {
            "type": "FOOTER",
            "text": "颂量科技"
        }
    }
}
响应参数
参数 说明 类型
status 状态码,0成功,其他失败参见响应状态码说明 String
reason 失败原因说明 String
data 发送结果 Object
templateId 模板id String
templateName 模板名称 String
type 模板类型:0-AUTHENTICATION(身份验证);1-MARKETING(营销性);2-UTILITY(交易类); String
languageCode 模板语言,详见国家枚举 String
status 模板状态:PENDING(审核中)、REJECTED(审核未通过)、APPROVED(审核通过)、DELETED(已删除)、DISABLED(禁用)、PENDING_DELETION(待删除)、IN_APPEAL(已提出上诉)、PAUSED(暂时停用)、LIMIT_EXCEEDED(超出限制)、FLAGGED(计划禁用)、REINSTATED(恢复) String
响应状态码
status 状态说明
0 成功
-1 身份验证错误
-2 模板状态不正确
-4 时间戳过期
-5 系统异常
-6 IP受限
-7 参数校验异常
-8 参数校验异常
-10 应用不存在
-11 应用名称已存在
-12 网络异常,请稍后重试
-14 账号未认证
-16 消息模板删除后一个月内不可再次使用相同名称创建,请重新定义模板名称
-18 fileId不能为空
-19 fileId不能为空
-21 header消息过长
-23 Button type不能为空
-24 buttons数量太多
-32 当前模板名下语言重复
-33 模板名格式不对
-34 模板两个变量不能同时挨着出现
-37 创建模板失败
-38 当前模板不存在
-40 示例文件太大
-74 语言代码格式错误
-75 模板类型与现有类型不匹配
-76 请输入正确的类型
-77 请输入正确的格式电话号码和区域代码
-78 请输入正确的语言
-82 body示例太长了
-83 请输入正确的format参数
-84 文本内容不能为空
-86 模板正文type不能为空
-87 模板页脚type不能为空
-88 模板页脚文本太长了
-89 模板组件不能为空
-90 Header不能有连续的变量
-91 标题文本中变量的数量与示例的数量不匹配。 或标题文本中的变量数不能大于1
-92 文本长度不能大于25
-99 通道类型错误
-100 头部类型为文本时format不能为空
-101 请输入正确的format值
-102 请输入正确的button类型, 当前只支持otp类型即type:3
-103 当模板类型为身份验证时, body 正文不能为空
-104 body添加的安全提醒参数错误
-105 模板页脚长度不能超过60
-106 过期时间必须在0-90
-107 该应用没有应用别名
-108 按钮参数otp_type不能为空
-109 当模板类型为身份验证时, 按钮不能为空

意见反馈

文档内容是否对您有帮助?

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 createTemplate() {
    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 templateName = "template name"; //the template name
    final String language = "language code"; // the template support language
    final int channelType = 0; //channelType: 0-WhatsApp, default is 0
    final int type = 1;

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

    HttpRequest request = HttpRequest.post(url);

    // currentTime
    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);

    String headerJson = "{\"type\":\"HEADER\",\"format\":0,\"text\":\"智能otp正在{{#}}活动中\",\"example\":{\"header_text\":[\"打折\"]}}";
    String bodyValJson = "{\"type\":\"BODY\",\"text\":\"公司新开业, {{#}}活动火热进行中....\",\"example\":{\"body_text\":[[\"打折\"]]}}";
    String footerJson = "{\"type\":\"FOOTER\",\"text\":\"颂量科技\"}";


    Map componentsMap = MapUtil.builder("header", JSONUtil.parseObj(headerJson))
                                                .put("body", JSONUtil.parseObj(bodyValJson))
                                                .put("footer", JSONUtil.parseObj(footerJson))
                                                .build();

    String body = JSONUtil.createObj()
            .set("appId", appId)
            .set("channelType", channelType)
            .set("templateName", templateName)
            .set("type", type)
            .set("components", componentsMap)
            .set("language", language)
            .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/im/template/add";

$dataArr["appId"] = $appId;
$dataArr["channelType"] = 0;
$dataArr["templateName"] = "template name";
$dataArr["type"] = 1;
$dataArr["language"] = "language code";

$headerArr = array(
    array(
        "type" => "HEADER",
        "format" => 0,
        "text" => "智能otp正在{{#}}活动中",
        "example" => array(
            "header_text" => "打折"
        )
    )
);
$bodyArr = array(
    array(
        "type" => "BODY",
        "text" => "公司新开业, {{#}}活动火热进行中....",
        "example" => array(
            "body_text" => "打折"
        )
    )
);
$footerArr = array(
    array(
        "type" => "FOOTER",
        "text" => "颂量科技"
    )
);
$dataArr["components"] = array(
    'header' => $headerArr,
    'body' => $bodyArr,
    'footer' => $footerArr
);

$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

{
    "status": "0",
    "reason": "success",
    "data": {
    "templateId": "0bd9ea48738d4d0db667b1e1f50020a9",
        "templateName": "itnio_template_xk_test",
        "type": 1,
        "languageCode": "en",
        "status": "APPROVED"
    }
}