騰訊云短信sdk
已發(fā)布于npm,可以直接使用npm install qcloudsms 引入郑原, 如果對您有幫助或者有任何建議唉韭,請去github 點(diǎn)個(gè)star或者提出你的建議。
start 開始使用
before you start to use this package,you should get this appid and appkey from TencentCloud ,dont use the phoneNumbers in demos,its just to show you how to run it.
在使用之前,請先去申請騰訊云短信應(yīng)用,以便獲取必要的appid和appkey,并且需要申請相關(guān)的短信簽名犯犁,短信模板,否則,你將會得到各種錯誤碼返回值属愤。錯誤碼
1、install 安裝
npm install qcloudsms --save
2酸役、require 導(dǎo)入
const Qsms=require("qcloudsms")
3住诸、init 初始化
const qsms=new Qsms("appid",'appkey')
4、send SMS 調(diào)用接口發(fā)送短信
5簇捍、check result 接收返回結(jié)果
return 返回值
類型:Promise
// demo中的res.data值示例
// 錯誤碼列表 https://cloud.tencent.com/document/product/382/3771
// 單發(fā)返回值
{
result: 0, // 成功字段值為0,失敗則會顯示錯誤狀態(tài)碼。
errmsg: 'OK', //成功字段值為"OK",失敗則會顯示錯誤原因
ext: '',
callid: '...'
}
// 群發(fā)返回值
{
result: 0,
errmsg: 'OK',
ext: '',
detail: [{
result: 0,
errmsg: 'OK',
mobile: '177883232323',
nationcode: '86',
sid: '8:Kdasdasddasdasd20171028',
fee: 1
},
{
result: 0,
errmsg: 'OK',
mobile: '17603073232',
nationcode: '86',
sid: '8:Lfdasdasds20171028',
fee: 1
}
]
}
methods
//demo public import
const Qsms=require("qcloudsms")
const qsms=new Qsms(idnumber,'key')
- singeSend({
phoneNumber,
msg,
msgType = 0,
nationCode = "86",
extend = "",
ext = ""
})
/**
* 單發(fā)短信
* @param {string} phoneNumber 手機(jī)號
* @param {string} msg 短信正文俏拱,如果需要帶簽名暑塑,簽名請使用【】標(biāo)注
* @param {number} msgType 短信類型,0 普通短信锅必,1 營銷短信事格。默認(rèn)值:0
* @param {string} nationCode 國家碼,默認(rèn)值:"86"
* @param {string} extend 擴(kuò)展字段惕艳,默認(rèn)值:""
* @param {string} ext 此字段騰訊云后臺服務(wù)器會按原樣在應(yīng)答中,默認(rèn)值:""
*/
//demo
qsms.singleSend({
phoneNumber,
msg,
msgType = 0,
nationCode = "86",
extend = "",
ext = ""
}).then(res=>{
console.log(res.data)
if(res.data.result===0){
// do success
}else{
//errors
//find your error code in this list
//https://cloud.tencent.com/document/product/382/3771
}
})
- singleSendWithParams({
phoneNumber,
tpl_id,
params,
sign,
nationCode = "86",
ext = "",
extend = ""
})
/**
* 模板單發(fā)短信
* @param {string} phoneNumber 手機(jī)號
* @param {number} tpl_id 短信模板id, 詳情:https://console.qcloud.com/sms/smsContent
* @param {array} params 模板參數(shù)數(shù)組,元素個(gè)數(shù)請不要超過模板參數(shù)個(gè)數(shù)
* @param {string} sign 短信簽名
* @param {string} nationCode 國家碼,默認(rèn)值:"86"
* @param {string} extend 擴(kuò)展字段驹愚,默認(rèn)值:""
* @param {string} ext 此字段騰訊云后臺服務(wù)器會按原樣在應(yīng)答中,默認(rèn)值:""
*/
//demo
qsms.singleSendWithParam({
phoneNumber: 176030703,
params: [12345],
tpl_id: 4266,
sign: '簽名'
}).then(res => {
console.log(res.data)
if(res.data.result===0){
// do success
}else{
//errors
//find your error code in this list
//https://cloud.tencent.com/document/product/382/3771
}
})
- multiSend({
phoneNumbers,
msg,
msgType = 0,
nationCode = "86",
extend = "",
ext = ""
})
/*
* 群發(fā)短信【僅國內(nèi),一次不超過200】
* @param {array} phoneNumbers 群發(fā)手機(jī)號數(shù)組
* @param {string} msg 短信正文远搪,如果需要帶簽名,簽名請使用【】標(biāo)注
* @param {number} msgType 短信類型逢捺,0 普通短信谁鳍,1 營銷短信。 默認(rèn)值:0
* @param {string} nationCode 國家碼,默認(rèn)值:"86"
* @param {string} extend 擴(kuò)展字段劫瞳,默認(rèn)值:""
* @param {string} ext 此字段騰訊云后臺服務(wù)器會按原樣在應(yīng)答中,默認(rèn)值:""
*/
//demo
qsms.multiSend({
phoneNumbers: [17603070288, 17788770699],
msg: "您的驗(yàn)證碼6789倘潜,此驗(yàn)證碼10分鐘內(nèi)有效,請勿向他人泄露"
}).then(res =>{
console.log(res.data)
if(res.data.result===0){
//do success
}else{
//errors
//find your error code in this list
//https://cloud.tencent.com/document/product/382/3771
}
})
- multiSendWithParams({
phoneNumbers,
tpl_id,
params,
sign,
nationCode = "86",
ext = "",
extend = ""
})
/**
* 模板群發(fā)短信【僅國內(nèi),一次不超過200】
* @param {array} phoneNumbers 群發(fā)手機(jī)號數(shù)組
* @param {number} tpl_id 短信模板id志于, 詳情:https://console.qcloud.com/sms/smsContent
* @param {array} params 模板參數(shù)數(shù)組,元素個(gè)數(shù)請不要超過模板參數(shù)個(gè)數(shù)
* @param {string} sign 短信簽名
* @param {string} nationCode 國家碼,默認(rèn)值:"86"
* @param {string} extend 擴(kuò)展字段涮因,默認(rèn)值:""
* @param {string} ext 此字段騰訊云后臺服務(wù)器會按原樣在應(yīng)答中,默認(rèn)值:""
*/
//demo
qsms.multiSendWithParams({
phoneNumbers: [17603070288, 17788770668],
params: [4523],
tpl_id: 423866,
sign: '哈哈'
}).then(res =>{
console.log(res.data)
if(res.data.result===0){
//do success
//
}else{
//errors
//find your error code in this list
//https://cloud.tencent.com/document/product/382/3771
}
})
- sendVoice({
phoneNumber,
msg,
playtimes = 2,
nationCode = "86",
ext = ""
})
/* 語音驗(yàn)證碼
* @param {string} nationCode 國家碼,默認(rèn)值:"86"
* @param {string} ext 此字段騰訊云后臺服務(wù)器會按原樣在應(yīng)答中,默認(rèn)值:""
* @param {number} playtimes 重播次數(shù),默認(rèn)2伺绽,最大3养泡。
* @param {number} phoneNumber 手機(jī)號碼
* @param {number|string} msg 驗(yàn)證碼,支持英文字母奈应、數(shù)字及組合澜掩。
*/
qsms.sendVoice({
phoneNumber: 17603070437,
msg: "876123"
}).then(res => {
if(res.data.result===0){
//do success
//
}else{
//errors
//find your error code in this list
//https://cloud.tencent.com/document/product/382/3771
}
})
- sendVoicePrompt({
phoneNumber,
promptfile,
prompttype = 2,
nationCode = "86",
playtimes = 2,
ext = ""
})
/*發(fā)送語音通知
* @param {number} phoneNumber 手機(jī)號碼
* @param {string} promptfile 通知內(nèi)容,utf8編碼钥组,支持中文英文输硝、數(shù)字及組合,需要和語音* 內(nèi)容模版相匹配
* @param {number} prompttype 語音類型,目前固定為2
* @param {number} playtimes 重播次數(shù)程梦,默認(rèn)2点把,最大3。
* @param {string} nationCode 國家碼,默認(rèn)值:"86"
* @param {string} ext 此字段騰訊云后臺服務(wù)器會按原樣在應(yīng)答中,默認(rèn)值:""
*/
qsms.sendVoicePrompt({
phoneNumber: 17603070235,
promptfile: "您好雷鋒屿附,您的參會申請已經(jīng)審核通過郎逃,請于11點(diǎn)按時(shí)參加會議,期待您的到來挺份。"
}).then(res => {
if(res.data.result===0){
//do success
//
}else{
//errors
//find your error code in this list
//https://cloud.tencent.com/document/product/382/3771
}
})