首先来说一下当中的几个方法:
Nodejs 关联阿里大鱼
首先需要安装环境:
npm install --save alidayu-node
注意的是要是有类似于
const DysmsapiClient = require('@alicloud/dysmsapi')
const DybaseapiClient = require('@alicloud/dybaseapi')
这种的导入则应该需要分别install不同的包
用npm install @alicloud/aaaa --save 导入不同的环境:只需要换的是aaaa 而已
使用方法 一:
var App = require('alidayu-node');
var app = new App('App Key', 'App Secret');
app.smsSend({
sms_free_sign_name: '这里是在阿里云上申请的签名',
sms_param: {"code": "123456", "product": "测试网站"},
rec_num: '13599999999',
sms_template_code: 'SMS_640004'
});
使用方法 一:
smsClient.sendSMS({
PhoneNumbers: phoneNum,
SignName: '欢迎使用淘友闲',
TemplateCode: 'SMS_146801021',
TemplateParam: '{"code":' + randomstr + ',"product":"淘友闲"}'
}).then(function (res) {
let {Code} = res
if (Code === 'OK') {
//处理返回参数
console.log(res)
return res;
}
}, function (err) {
console.log(err)
})
注:randomstr 这个参数是用自己定义的函数生成的,大鱼只负责发送,不会生成验证码;产生随机数的函数如下:
//产生六位的随机验证码
var range = function (start, end) {
var array = [];
for (var i = start; i < end; ++i) array.push(i);
return array;
};
var randomstr = range(0, 6).map(function (x) {
return Math.floor(Math.random() * 10);
}).join('');
console.log(randomstr);
但是这种使用的方法需要额外的一个实例,里面的方法在之后会详细的介绍,这里只是简单的注释和调用:代码如下所示:
'use strict';
const DysmsapiClient = require('@alicloud/dysmsapi')
const DybaseapiClient = require('@alicloud/dybaseapi')
const MNSClient = require('@alicloud/mns')
// 短信回执报告:SmsReport,短信上行:SmsUp
const msgTypeList = ["SmsReport", "SmsUp"]
const DYSMSAPI_ENDPOINT = 'http://dysmsapi.aliyuncs.com'
const DYBASEAPI_ENDPOINT = 'http://dybaseapi.aliyuncs.com'
class SMSClient {
constructor(options) {
let {accessKeyId, secretAccessKey}=options
if (!accessKeyId) {
throw new TypeError('parameter "accessKeyId" is required');
}
if (!secretAccessKey) {
throw new TypeError('parameter "secretAccessKey" is required');
}
this.dysmsapiClient = new DysmsapiClient({accessKeyId, secretAccessKey, endpoint: DYSMSAPI_ENDPOINT})
this.dybaseClient = new DybaseapiClient({accessKeyId, secretAccessKey, endpoint: DYBASEAPI_ENDPOINT})
this.expire = []
this.mnsClient = []
}
//发送短信
sendSMS(params) {
return this.dysmsapiClient.sendSms(params)
}
//查询详情
queryDetail(params) {
return this.dysmsapiClient.querySendDetails(params)
}
//失效时间与当前系统时间比较,提前2分钟刷新token
_refresh(type) {
return this.expire[type] - new Date().getTime() > 2 * 60 * 1000
}
//获取token
_getToken(type) {
let msgType = msgTypeList[type]
return this.dybaseClient.queryTokenForMnsQueue({MessageType: msgType})
}
//根据类型获取mnsclient实例
async _getMNSClient(type) {
if (this.mnsClient && (this.mnsClient[type] instanceof MNSClient) && this._refresh(type)) {
return this.mnsClient[type]
}
let {
MessageTokenDTO:{
SecurityToken,
AccessKeyId,
AccessKeySecret
}
}=await this._getToken(type)
if (!(AccessKeyId && AccessKeySecret && SecurityToken)) {
throw new TypeError('get token fail')
}
let mnsClient = new MNSClient('1943695596114318', {
securityToken: SecurityToken,
region: 'cn-hangzhou',
accessKeyId: AccessKeyId,
accessKeySecret: AccessKeySecret,
// optional & default
secure: false, // use https or http
internal: false, // use internal endpoint
vpc: false // use vpc endpoint
})
this.mnsClient[type] = mnsClient
this.expire[type] = (new Date().getTime() + 10 * 60 * 1000)
return mnsClient
}
//typeIndex :0 为回执,1为上行
async receiveMsg(typeIndex = 0, preQueueName, waitSeconds = 10) {
let mnsClient = await this._getMNSClient(typeIndex)
return await mnsClient.receiveMessage(preQueueName + msgTypeList[typeIndex], waitSeconds)
}
}
module.exports = SMSClient
方法列表:
- 短信发送:
smsSend(params, callback)
- params
- {string} sms_free_sign_name 短信签名 传入的短信签名必须是在阿里大鱼“管理中心-短信签名管理”中的可用签名。
- {json} sms_param 短信模板变量 传参规则{"key":"value"},key的名字须和申请模板中的变量名一致,多个变量之间以逗号隔开
- {string} rec_num 短信接收号码 支持单个或多个手机号码,传入号码为11位手机号码,不能加0或+86。群发短信需传入多个号码,以英文逗号分隔,一次调用最多传入200个号码
- {string} sms_template_code 短信模板ID 传入的模板必须是在阿里大鱼“管理中心-短信模板管理”中的可用模板。示例:SMS_585014
- callback 回调,参考 http://open.taobao.com/doc2/apiDetail.htm?apiId=25450
- 短信发送记录查询
smsQuery(params, callback)
- params
- {string} rec_num 短信接收号码
- {string} query_date 短信发送日期,支持近30天记录查询,格式yyyyMMdd
- {number} current_page 分页参数,页码
- {number} page_size 分页参数,每页数量。最大值100
- callback 回调,参考 http://open.taobao.com/doc2/apiDetail.htm?apiId=26039
3.语音通知
voiceSinglecall(params, callback)
- params
- {string} called_num 被叫号码,支持国内手机号与固话号码,格式如下057188773344,13911112222,4001112222,95500
- {string} called_show_num 被叫号显,传入的显示号码必须是阿里大鱼“管理中心-号码管理”中申请通过的号码
- {string} voice_code 语音文件ID,传入的语音文件必须是在阿里大鱼“管理中心-语音文件管理”中的可用语音文件
- callback 回调,参考 http://open.taobao.com/doc2/apiDetail.htm?apiId=25445
4.文本转语音通知
ttsSinglecall(params, callback)
- params
- {json} tts_param 文本转语音(TTS)模板变量,传参规则{"key":"value"},key的名字须和TTS模板中的变量名一致,多个变量之间以逗号隔开
- {string} called_num 被叫号码,支持国内手机号与固话号码,格式如下057188773344,13911112222,4001112222,95500
- {string} called_show_num 被叫号显,传入的显示号码必须是阿里大鱼“管理中心-号码管理”中申请或购买的号码
- {string} tts_code TTS模板ID,传入的模板必须是在阿里大鱼“管理中心-语音TTS模板管理”中的可用模板
- callback 回调,参考 http://open.taobao.com/doc2/apiDetail.htm?apiId=25444
5.语音双呼
voiceDoublecall(params, callback)
- params
- {string} caller_num 主叫号码,支持国内手机号与固话号码,格式如下057188773344,13911112222,4001112222,95500
- {string} caller_show_num 主叫号码侧的号码显示,传入的显示号码必须是阿里大鱼“管理中心-号码管理”中申请通过的号码。显示号码格式如下057188773344,4001112222,95500
- {string} called_num 被叫号码,支持国内手机号与固话号码,格式如下057188773344,13911112222,4001112222,95500
- {string} called_show_num 被叫号码侧的号码显示,传入的显示号码可以是阿里大鱼“管理中心-号码管理”中申请通过的号码。显示号码格式如下057188773344,4001112222,95500。显示号码也可以为主叫号码。
- callback 回调,参考 http://open.taobao.com/doc2/apiDetail.htm?apiId=25443
一下就是方法的具体使用:
/**
* 云通信基础能力业务短信发送、查询详情以及消费消息示例,供参考。
* Created on 2017-07-31
*/
const SMSClient = require('./../index')
// ACCESS_KEY_ID/ACCESS_KEY_SECRET 根据实际申请的账号信息进行替换
const accessKeyId = 'yourAccessKeyId'
const secretAccessKey = 'yourAccessKeySecret'
//在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName,不用填最后面一段
const queueName = 'Alicom-Queue-1092397003988387-'
//初始化sms_client
let smsClient = new SMSClient({accessKeyId, secretAccessKey})
//短信回执报告
smsClient.receiveMsg(0, queueName).then(function (res) {
//消息体需要base64解码
let {code, body}=res
if (code === 200) {
//处理消息体,messagebody
console.log(body)
}
}, function (err) {
console.log(err)
})
//短信上行报告
smsClient.receiveMsg(1, queueName).then(function (res) {
//消息体需要base64解码
let {code, body}=res
if (code === 200) {
//处理消息体,messagebody
console.log(body)
}
}, function (err) {
console.log(err)
})
//查询短信发送详情
smsClient.queryDetail({
PhoneNumber: '1500000000',
SendDate: '20170731',
PageSize: '10',
CurrentPage: "1"
}).then(function (res) {
let {Code, SmsSendDetailDTOs}=res
if (Code === 'OK') {
//处理发送详情内容
console.log(SmsSendDetailDTOs)
}
}, function (err) {
//处理错误
console.log(err)
})
//发送短信
smsClient.sendSMS({
PhoneNumbers: '1500000000',
SignName: '云通信产品',
TemplateCode: 'SMS_000000',
TemplateParam: '{"code":"12345","product":"云通信"}'
}).then(function (res) {
let {Code}=res
if (Code === 'OK') {
//处理返回参数
console.log(res)
}
}, function (err) {
console.log(err)
})