本文为您介绍了Node.js调用SendMessageWithTemplate发送中国地区短信。

请求参数

名称 类型 是否必选 示例值 描述
To String 45***0121 接收方的手机号码;说明:号码格式为国家码+号码。
From String XXCompany 发送方标识;说明:发往中国时,该参数为短信签名。
TemplateCode String SMS_0000 短信模板ID。请在控制台中查看。
TemplateParam String {“code”:”1234”,”product”:”ytx”} 短信模板变量对应的实际值,JSON格式;说明:如果JSON中需要带换行符,请参照标准的JSON协议处理。
SmsUpExtendCode String 90999 上行短信扩展码,无特殊需要此字段的用户请忽略此字段。

返回数据

名称 类型 示例值 描述
To String 45***0121 接收方的手机号码;说明:号码格式为国家码+号码。
MessageId String 100***03003003 短信发送ID,可根据该ID在接口QueryMessage中查询具体的发送状态。
ResponseCode String OK 请求状态码,返回OK代表请求成功,其他错误码详见错误码列表。
ResponseDescription String 短信接收成功 状态码的描述。
Segments String 1 短信计费条数。

引入阿里云核心包

  • 方法1:通过npm/yarn安装。
    npm install @alicloud/pop-core
                        
  • 方法2:core-sdk下载地址:Node.js API SDK

调用示例

注意事项如下:

  • RegionId必须为ap-southeast-1,请勿修改。
  • endpoint必须为https://dysmsapi.ap-southeast-1.aliyuncs.com, 请勿修改。
  • apiVersion必须为2018-05-01,请勿修改。
const Core = require('@alicloud/pop-core');

var client = new Core({
  accessKeyId: '<accessKeyId>',
  accessKeySecret: '<accessSecret>',
  endpoint: 'https://dysmsapi.ap-southeast-1.aliyuncs.com',
  apiVersion: '2018-05-01'
});

var params = {
  "RegionId": "ap-southeast-1",
  "To": "8615200000000",
  "From": "SMS signature",
  "TemplateCode": "SMS_225001",
  "TemplateParam": "{\"code\":\"1234\"}",
  "SmsUpExtendCode": "12345"
}

var requestOption = {
  method: 'POST'
};

client.request('SendMessageWithTemplate', params, requestOption).then((result) => {
  console.log(result);
}, (ex) => {
  console.log(ex);
})