Promise風(fēng)格的JavaScript 騰訊云短信sdk

騰訊云短信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)的短信簽名犯犁,短信模板,否則,你將會得到各種錯誤碼返回值属愤。錯誤碼

實(shí)例 Demo

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')
  1. 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
    }
  })

  1. 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
          }
      })  
  1. 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
    }
  })

  1. 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
      }
  })

  1. 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
    }
  })
  1. 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
      }
    })
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末褒翰,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子匀泊,更是在濱河造成了極大的恐慌优训,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件各聘,死亡現(xiàn)場離奇詭異揣非,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)躲因,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門早敬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來忌傻,“玉大人,你說我怎么就攤上這事搞监∷ⅲ” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵琐驴,是天一觀的道長俘种。 經(jīng)常有香客問我,道長棍矛,這世上最難降的妖魔是什么安疗? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮够委,結(jié)果婚禮上荐类,老公的妹妹穿的比我還像新娘。我一直安慰自己茁帽,他們只是感情好玉罐,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著潘拨,像睡著了一般吊输。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上铁追,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天季蚂,我揣著相機(jī)與錄音,去河邊找鬼琅束。 笑死扭屁,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的涩禀。 我是一名探鬼主播料滥,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼艾船!你這毒婦竟也來了葵腹?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤屿岂,失蹤者是張志新(化名)和其女友劉穎践宴,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體爷怀,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡阻肩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了霉撵。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片磺浙。...
    茶點(diǎn)故事閱讀 40,040評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖徒坡,靈堂內(nèi)的尸體忽然破棺而出撕氧,到底是詐尸還是另有隱情,我是刑警寧澤喇完,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布伦泥,位于F島的核電站,受9級特大地震影響锦溪,放射性物質(zhì)發(fā)生泄漏不脯。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一刻诊、第九天 我趴在偏房一處隱蔽的房頂上張望防楷。 院中可真熱鬧,春花似錦则涯、人聲如沸复局。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽亿昏。三九已至,卻和暖如春档礁,著一層夾襖步出監(jiān)牢的瞬間角钩,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工呻澜, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留递礼,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓易迹,卻偏偏與公主長得像宰衙,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子睹欲,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容