Last active
April 11, 2018 10:25
-
-
Save bachvtuan/0dffcd28c4f87cf424f3893605621dc1 to your computer and use it in GitHub Desktop.
Send SMS Amazon SNS Nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @Author: [email protected] | |
* 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