Coder Social home page Coder Social logo

aliyun-sms's Introduction

aliyun-sms-sdk

阿里云短信服务Node.js的实现

安装

$ npm install aliyun-sms-sdk --save

阿里云官方文档:短信服务文档

使用方法

在项目中安装aliyun-sms-sdk package后,需要根据官方文档做一些配置

send()

const aliSms = require('aliyun-sms-sdk');
const confSend = {
    accessKeyId: 'Your accessKeyId',
    secretAccessKey: 'Your secretAccessKey',
    recNum: 'Send SMS to ....',
    signName: 'Sign name',
    templateCode: 'SMS template code (SMS_XXXXXX)',
    param: { "username": "Sirius" }
};

aliSms.send(confSend);  //发送短信

返回值:

{   
    Message: 'OK',
    RequestId: '502FC867-40EB-4A9F-8561-3272FBF7C4CB',
    BizId: '109226971936^1112321971296',
    Code: 'OK' 
}

receive()

const aliSms = require('aliyun-sms-sdk');
const confReceive = {
    accessKeyId: 'Your accessKeyId',
    secretAccessKey: 'Your secretAccessKey',
    queueName: 'Alicom-Queue-xxxxxxxxxxxxx-'
};

aliSms.receive(confReceive);   //接收用户发来的短信(短信上行)

返回值:

{
    receiveMsg: {
        "code": 200,
        "headers": {},
        "body": {
            "MessageId": "DD48C8069525249D-2-15DAC6DF8B7-300000001",
            "MessageBodyMD5": "B94416522AF45DA65D66A07BEEB396ED",
            "MessageBody": 'eyJjb250ZW50Ijoi5ZOI5ZOI5ZOI5ZOI5ZOIIiwic2VuZF90aW1lIjoiMjAxNzA4MDcxNTQ4MTIiLCJzcF9pZCI6bnVsbCwicGhvbmVfbnVtYmVyIjoiMTg1MTQyNDMwODgiLCJkZXN0X2NvZGUiOiIxMjQzMzM1Iiwic2VxdWVuY2VfaWQiOjQ4MjExODA5NX0=', //需要进行Base64解码Ï
            "ReceiptHandle": "1-MTI4ODQ5MDE4ODktMTUwMjA4NzUwMi0yLTg=",
            "EnqueueTime": "1501836474551",
            "FirstDequeueTime": "1501836581811",
            "NextVisibleTime": "1502087502000",
            "DequeueCount": "3",
            "Priority": "8"
        }
    },
    deleteMsg: {
        code: 204,
        headers: {
            
        },
        body: undefined
    }
}

作者修改了官方的SDK,因为官方SDK中并没有对已经消费的消息进行删除操作。

receiveMsg是消费一条消息,其中body.MessageBody需要进行Base64解码:

const msgBody = new Buffer(result.body.MessageBody, 'base64').toString();
console.log(msgBody);

结果样例:

{
    "content": "123456789",
    "send_time": "20170804164754",
    "sp_id": null,
    "phone_number": "13800138000",
    "dest_code": "1243335",
    "sequence_id": 481806640
}

Test

#send short message
node test/send

#receive short message upload
node test/receive

补充说明

阿里云提供了Node.js版本的短信服务SDK,但是比较简陋。作者因公司业务需要,没有对本项目进行优化和架构(实在也是简单),如有意见、建议或bug,请直接提issue给我。

官方的SDK中已经实现了查询短信发送记录和短信发送状态回执的方法,我司项目暂时不需要这两个方法,同时阿里云官方的被动消息模式也比主动查询MQ更能保证可用性和易用性,大家可以自行建接口实现。

aliyun-sms's People

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

codefunstech

aliyun-sms's Issues

发送可以 ,回执报告也能和跑通,消息上行出问题了

错误信息:
{ MNSMessageNotExistError: GET http://1943695596114318.mns.cn-hangzhou.aliyuncs.com/queues/Alicom-Queue-1669047560238860-SmsUp/messages?waitseconds=10 failed with 404. requestid: 5CFCDB7C4145337043B690C1, hostid: http://1943695596114318.mns.cn-hangzhou.aliyuncs.com, message: Message not exist.
at Client.request (C:\Users\Administrator\Desktop\aliyun-sms\node_modules@alicloud\mns\lib\client.js:69:21)
at process._tickCallback (internal/process/next_tick.js:68:7) name: 'MNSMessageNotExistError' }

现在不清楚哪里出问题了。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.