Last active
May 18, 2016 21:00
-
-
Save jinman/5f842ed07245e765678e46ad5ab7b49a to your computer and use it in GitHub Desktop.
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
//This is a sample lambda function that sends an Email or SMS on click of a button. It creates a SNS topic, subscribes an endpoint (SMS/EMAIL) | |
//to the topic and publishes to the topic. | |
/* | |
The following JSON template shows what is sent as the payload. | |
{ | |
"serialNumber": "GXXXXXXXXXXXXXXXXX", | |
"batteryVoltage": "mV", | |
"clickType": "SINGLE | DOUBLE | LONG" | |
} | |
A “LONG” clickType is sent if the first press lasts longer than 1.5 seconds. “SINGLE” and “DOUBLE” clickType payloads are sent for short clicks. | |
*/ | |
var aws = require('aws-sdk'); | |
var sns = new aws.SNS(); | |
exports.handler = function(event, context) { | |
var buttonDSN = event.serialNumber; | |
console.log(event.clickType); | |
//Send SMS if the clickType is DOUBLE | |
if(event.clickType === 'SINGLE') | |
{ | |
//Send email if the clickType is SINGLE or LONG | |
var sendTo = 'youremail@email.com'; // ENTER YOUR EMAIL | |
var sendMode = 'email'; | |
} | |
//Send SMS if the clickType is DOUBLE | |
if(event.clickType === 'DOUBLE') | |
{ | |
sendTo = 'your phone number with countrycode'; // ENTER YOUR PHONE NUMBER | |
sendMode = 'sms'; | |
} | |
//Send SMS if the clickType is DOUBLE | |
if(event.clickType === 'LONG') | |
{ | |
console.log("Button Clicked For Long Time"); | |
return; | |
} | |
//lets create a SNS topic (idempotent call) | |
var topicparams = { | |
Name: 'aws-iot-button-sns-topic' /* required */ | |
}; | |
sns.createTopic(topicparams, function(err, data) { | |
if (err) { | |
console.log(err, err.stack); // an error occurred | |
return; | |
} | |
console.log('createTopic - Success' + data); // successful response | |
var topicArn = data.TopicArn; | |
//for SMS, we need to send DisplayName | |
var attributeparams = { | |
AttributeName: 'DisplayName', | |
/* required */ | |
TopicArn: topicArn, | |
/* required */ | |
AttributeValue: 'iot-button' | |
}; | |
sns.setTopicAttributes(attributeparams, function(err1, data1) { | |
if (err1) { | |
console.log(err1, err1.stack); // an error occurred | |
return; | |
} | |
console.log('setTopicAttributes - Success' + data1); // successful response | |
//Lets subscribe only if there are no subscriptions | |
var listparams = { | |
TopicArn: topicArn | |
}; | |
sns.listSubscriptionsByTopic(listparams, function(err5, data5) { | |
if (err5) { | |
console.log(err, err.stack); // an error occurred | |
return | |
} | |
console.log(data5); // successful response | |
console.log(data5.Subscriptions.length); | |
//Lets subscribe to endpoint (not an idempotent call) | |
//if (data5.Subscriptions.length === 0) { | |
var subscribeparams = { | |
Protocol: sendMode, | |
/* required */ | |
TopicArn: topicArn, | |
/* required */ | |
Endpoint: sendTo, | |
}; | |
sns.subscribe(subscribeparams, function(err2, data2) { | |
if (err2) { | |
console.log(err2, err2.stack); // an error occurred | |
return; | |
} | |
console.log('subscribe - success:' + data2); | |
}); | |
//} // endif subscriptionlength | |
var payload = buttonDSN + ' -- processed by Lambda'; | |
console.log(payload); | |
//Publish to the SNS Topic | |
var params = { | |
Message: payload, | |
Subject: 'Hello from your IoT Button ' + buttonDSN + ' ' + event.clickType, | |
TopicArn: topicArn | |
}; | |
sns.publish(params, function(err3, data3) { | |
if (err3) { | |
context.fail('ERROR:Calling SNS from Lambda: ' + err3) | |
} else { | |
console.log('Published - Great success! JSON: ' + JSON.stringify(data3, null, ' ')); | |
context.succeed('SUCCESS'); | |
} | |
}); | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment