1. 程式人生 > >阿里雲簡訊傳送模組api及demo

阿里雲簡訊傳送模組api及demo

安裝方法:$ npm install @alicloud/sms-sdk --save

程式碼:

/**

 * 雲通訊基礎能力業務簡訊傳送、查詢詳情以及消費訊息示例,供參考。

 * Created on 2017-07-31

 */

const SMSClient = require('@alicloud/sms-sdk')

// 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.sendBatchSMS({

    PhoneNumberJson: JSON.stringify(['18040580000', '15088650000']),

    SignNameJson: JSON.stringify(['簡訊遷移測試簽名','簡訊遷移測試簽名']),

    TemplateCode: 'SMS_71175823',

    TemplateParamJson: JSON.stringify([{code: "1234", product: "ytx1"}, {code: "5678", product: "ytx2"}]),

}).then(function (res) {

    let {Code}=res

    if (Code === 'OK') {

       //處理返回引數

       console.log(res)

    }

}, function (err) {

    console.log('err', err)

})

//簡訊回執報告

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)

})