Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Send SMS Amazon SNS Nodejs
/**
* @Author: bachvtuan@gmail.com
* to_number: String : user phone number
* message: String : sms message
* func_callback: function : callback function( status )
* Reference links :
* https://gist.github.com/stuartmyles/8099723
* http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#deleteTopic-property
*/
function sendSMS(to_number, message, func_callback) {
AWS.config.update({
accessKeyId: Config.amazon_sns.accessKeyId,
secretAccessKey: Config.amazon_sns.secretAccessKey,
region: Config.amazon_sns.region
});
var sns = new AWS.SNS();
var SNS_TOPIC_ARN = Config.amazon_sns.arn.sms;
sns.subscribe({
Protocol: 'sms',
//You don't just subscribe to "news", but the whole Amazon Resource Name (ARN)
TopicArn: SNS_TOPIC_ARN,
Endpoint: to_number
}, function(error, data) {
if (error) {
common.log("error when subscribe", error);
return func_callback(false);
}
common.log("subscribe data", data);
var SubscriptionArn = data.SubscriptionArn;
var params = {
TargetArn: SNS_TOPIC_ARN,
Message: message,
//hardcode now
Subject: 'Admin'
};
sns.publish(params, function(err_publish, data) {
if (err_publish) {
common.log('Error sending a message', err_publish);
} else {
common.log('Sent message:', data.MessageId);
}
var params = {
SubscriptionArn: SubscriptionArn
};
sns.unsubscribe(params, function(err, data) {
if (err) {
common.log("err when unsubscribe", err);
}
return func_callback(err_publish != null);
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment