John Keenan
Published © GPL3+

AWS IOT 'Easy Button' Enhances Your Relationship!

Busy? You can just press the IOT 'Easy Button' on your desk and your loved one will get a 'random' affectionate text sent from your phone.

IntermediateFull instructions provided2 hours1,403
AWS IOT 'Easy Button' Enhances Your Relationship!

Things used in this project

Story

Read more

Code

IOT "Easy Button' Lambda Function

JavaScript
Randomly selects from a list of messages and texts message to your phone
/**
 * This is a sample Lambda function that sends an SMS on click of a
 * button. It needs one permission sns:Publish. The following policy
 * allows SNS publish to SMS but not topics or endpoints.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "sns:Publish"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Effect": "Deny",
            "Action": [
                "sns:Publish"
            ],
            "Resource": [
                "arn:aws:sns:*:*:*"
            ]
        }
    ]
}
 *
 * The following JSON template shows what is sent as the payload:
{
    "serialNumber": "GXXXXXXXXXXXXXXXXX",
    "batteryVoltage": "xxmV",
    "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.
 *
 * For more documentation, follow the link below.
 * http://docs.aws.amazon.com/iot/latest/developerguide/iot-lambda-rule.html
 */

'use strict';

const AWS = require('aws-sdk');

const SNS = new AWS.SNS({ apiVersion: '2010-03-31' });
const PHONE_NUMBER = '16021234567'; // change it to your phone number

exports.handler = (event, context, callback) => {
    // one of these messages will be randomly selected and sent on short click
    const inMyThoughtsMessages = [
                          'Thinking of you...x',
                          'Hope youre having a great day...x',
                          'Looking forward to seeing you later...x',
                          'Theres something about you...x',
                          'How lucky I am...x'
                        ];
    // one of these messages will be randomly selected and sent on double click
    const encourageMessages = [
                          'You go for it!',
                          'Good luck!',
                          'Remember - youre the greatest!',
                          'Do it anyway!',
                          'Im cheering for you!'
                        ];
    // one of these messages will be randomly selected and sent on long click
    const congratsMessages = [
                          'Way to go!',
                          'Congrats!',
                          'You did it!',
                          'HUGE success!',
                          'Well done you!'
                        ];
    
    console.log('Received event:', event);

    console.log(`Sending SMS to ${PHONE_NUMBER}`);
    
    //uses randomizer to select one of the predefined messages
    var singleClick = inMyThoughtsMessages[Math.floor(Math.random()*inMyThoughtsMessages.length)];
    var doubleClick = encourageMessages[Math.floor(Math.random()*encourageMessages.length)];
    var longClick = congratsMessages[Math.floor(Math.random()*congratsMessages.length)];
    var randomMessage = singleClick;
    
    if(event.clickType == "DOUBLE"){
        randomMessage = doubleClick;
    }
    if(event.clickType == "LONG"){
        randomMessage = longClick;
    }
    
    const params = {
        PhoneNumber: PHONE_NUMBER,
        Message: randomMessage,
    };
    // result will go to function callback
    SNS.publish(params, callback);
};

Credits

John Keenan

John Keenan

1 project • 0 followers
https://www.linkedin.com/in/john-m-keenan-0135b/
Thanks to Winston Frick.

Comments