开发者社区> 问答> 正文

短信消息API---Node.js



简介


当您使用短信的API接口发送短信后,可以通过使用MNS的Queue模型来接收短信的回执消息,假如服务出现异常情况时(如网络问题),导致消息回执未成功获取,还可以通过短信发送状态查询API接口进行一定的补偿(目前支持30天内发送记录的查询)。

消息的订阅

  • 云通信的所有业务消息都用过MNS消息服务向外发送。用户每订阅一个类别的消息(比如上行短信消息SmsUp),系统都会为用户分配一个独立的消息队列。
  • 用户可以通过阿里云账号拿到一个临时的token用于获取队列中的消息。用户可以下载demo,编写简单的消息处理类即可完成消息处理的任务。
  • 在页面上订阅消息,订阅完消息后,能拿到消息队列名称(queueName)。比如:Alicom-Queue-xxxxxx-SmsReport 。队列名字每个用户都不同。


消息类型


短信提供2种消息类型SmsReport(短信回执报告消息) 和 SmsUp(上行短信消息)
  • 通过订阅SmsReport短信状态报告,可以获知每条短信的发送情况,了解短信是否达到终端用户的状态与相关信息
  • 通过订阅SmsUp上行短信消息,可以获知终端用户回复短信的内容


短信回执消息SmsReport消息体格式

名称类型描述示例是否必须
phone_numberString短信接收号码13000000000可选
successBoolean发送是否成功true必须
biz_idString发送回执ID1234^345必须
out_idString调用发送短信接口时传的outId123456可选
send_timeString转发给运营商的时间2017-06-01 10:00:00必须
report_timeString收到运营商回执的时间2017-06-01 10:00:05可选
err_codeString错误码UNKNOW可选
err_msgString错误信息未知异常可选
sms_sizeString140字节算一条短信,短信长度超过140字节时会拆分成多条短信发送1,2,3可选


上行短信消息SmsUp

名称类型描述示例是否必须
phone_numberString短信接收号码13000000000可选
contentString短信内容true必须
send_timeString时间20150101120000必须
dest_codeString扩展码123456必须
sequence_idString消息序列ID123456必须


技术对接步骤



1:下载SDK工具包

<divre style='background: rgb(246, 246, 246); font: 12px/1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin: 0px 0px 16px; padding: 10px; outline: 0px; border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; overflow: auto; word-spacing: 0px; white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; orphans: 2; widows: 2; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' prettyprinted?="" linenums="">
  1. $ npm install @alicloud/sms-sdk --save

SDK&DEMO[ 下载地址]

编写样例代码

<pre style='background: rgb(246, 246, 246); font: 12px/1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; padding: 10px; outline: 0px; border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; overflow: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px !important; margin-left: 0px; word-spacing: 0px; white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; orphans: 2; widows: 2; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' prettyprinted?="" linenums="">
  1. /**
  2. * 云通信基础能力业务短信发送、查询详情以及消费消息示例,供参考。
  3. * Created on 2017-07-31
  4. */
  5. const SMSClient =require('@alicloud/sms-sdk')
  6. // ACCESS_KEY_ID/ACCESS_KEY_SECRET 根据实际申请的账号信息进行替换
  7. const accessKeyId = 'yourAccessKeyId'
  8. const secretAccessKey = 'yourAccessKeySecret'
  9. //在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName,不用填最后面一段
  10. const queueName = 'Alicom-Queue-1092397003988387-'
  11. //初始化sms_client
  12. let smsClient = new SMSClient({accessKeyId, secretAccessKey})
  13. //短信回执报告
  14. smsClient.receiveMsg(0, queueName).then(function (res) {
  15.     //消息体需要base64解码
  16.     let {code, body}=res
  17.     if (code === 200) {
  18.         //处理消息体,messagebody
  19.         console.log(body)
  20.     }
  21. }, function (err) {
  22.     console.log(err)
  23. })
  24. //短信上行报告
  25. smsClient.receiveMsg(1, queueName).then(function (res) {
  26.     //消息体需要base64解码
  27.     let {code, body}=res
  28.     if (code === 200) {
  29.         //处理消息体,messagebody
  30.         console.log(body)
  31.     }
  32. }, function (err) {
  33.     console.log(err)
  34. })

展开
收起
nicenelly 2017-10-25 11:51:39 2310 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
编程语言如何演化—— 以 JS 的 private 为例 立即下载
编程语言如何演化-以JS的private为例 立即下载
JS 语言在引擎级别的执行过程 立即下载