发送模板消息

POST
https://api.itniotech.com/wa/template/sendMsg
根据创建的模板来发送模板消息
请求参数
参数 说明 是否必填 类型
appId 应用Id String
businessPhone 商家号码 String
recipient 接收者电话号码 string
channelType 通道类型:0-WhatsApp,默认送0; Integer
template Object
name 模板名称 String
language Object
code 模板语言枚举 String
components List
type 组件类型:header,body,button,当body或者header有变量时必填。
parameters 变量,当body或者header有变量时必填 List
type 当components下type为header时类型有:text、image、video;document;当为body时类型有text、currency、date_time。 String
text 变量对应的文本内容,当变量类型type=text时必填。 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"
    }
}
响应参数
参数 说明 类型
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

意见反馈

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

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