Is there a best approach to deploy an architecture to send SMS using a Microservice model?

后端 未结 2 2072
独厮守ぢ
独厮守ぢ 2021-02-02 04:42

We have a service within a Backend class, the service looks like:

// Setup AWS SNS
AWS.config.update({
    region: \'eu-west-1\',
    accessKeyI         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-02 05:15

    If your use case is to deliver single sms to individuals then you don't need to create a topic and delete it afterwards. It's possible to simply send one sms with the following code.

    let AWS = require('aws-sdk');
    const sns = new AWS.SNS();
    exports.handler = function (event, context, callback) {
        var params = {
      Message: event.message, //  your message you would like to send
            MessageAttributes: {
                'AWS.SNS.SMS.SMSType': {
                    DataType: 'String',
                    StringValue: event.messageType // the smsType "Transactional" or "Promotional"
                },
                'AWS.SNS.SMS.SenderID': {
                    DataType: 'String',
                    StringValue: event.messageSender // your senderId - the message that will show up as the sender on the receiving phone
                },
            },
      PhoneNumber: event.phone // the phone number of the receiver 
    };
    
    sns.publish(params, function (err, data) {
            callback(null, {err: err, data: data});
            if (err) {
                console.log(err);
                context.fail(err);
            } else {
                console.log("Send sms successful to user:", event.phone);
                context.succeed(event);
                return;
            }
        });
    };
    

    the api endpoint/lambda receives the following body

    {
    "message": "hey ho I am the sms message.",
    "messageType": "Transactional", //or "Promotional"
    "messageSender": "Your Brand",
    "phone":"+436640339333"
    }
    

提交回复
热议问题