Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
KOJI TATEOKA
Created January 8, 2016

Escape Button

Push AWS Iot Button , when you met troubles. It will ring your cellphone for your escape.

Escape Button

Things used in this project

Hardware components

AWS IoT Button
Amazon Web Services AWS IoT Button
×1

Software apps and online services

AWS Lambda
Amazon Web Services AWS Lambda
Twilio Voice API

Story

Read more

Code

AWS Lambda to Twilio

JavaScript
Set Original Event by AWS IoT button
var accountSid = 'YOUR TWILIO ACCOUNTSID';
var authToken = 'YOUR TWILIO AUTHCODE';
var fromNumber = 'YOUR TWILIO PHONE NUMBER';
var toNumber = 'SET PHONE NUMBER CALL TO';
var configUrl = 'http://twimlets.com/echo?Twiml=%3CResponse%3E%0A%3CSay%3E%20This%20is%20emergency%20call.%20This%20is%20emergency%20call.%20%3C%2FSay%3E%0A%3CSay%3E%20IoT%20button%20is%20pushed%2C%20escape%20now.%20%3C%2FSay%3E%0A%3C%2FResponse%3E&';

var https = require('https');
var queryString = require('querystring');

exports.handler = function (event, context) {
  console.log('Start twilio call');

  callTwilio(function(status) {
    context.done(null, status);
  });
};

function callTwilio(callback) {
  var messageString = queryString.stringify({
    From: fromNumber,
    To: toNumber,
    Url: configUrl,
    Method: 'GET'
  });

  var options = {
    host: 'api.twilio.com',
    port: 443,
    path: '/2010-04-01/Accounts/' + accountSid + '/Calls.json',
    method: 'POST',
    headers: {
      'Authorization': 'Basic ' + new Buffer(accountSid + ':' + authToken).toString('base64'),
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': Buffer.byteLength(messageString)
    }
  };

  var req = https.request(options, function(res) {
    res.setEncoding('utf-8');

    var responseString = '';
    res.on('data', function(data) {
      responseString += data;
    });

    res.on('end', function() {
      console.log('Twilio Response: ' + responseString);
      callback('API request sent successfully.');
    });
  });

  req.on('error', function(e) {
    console.error('HTTP error: ' + e.message);
    callback('API request completed with error(s).');
  });

  console.log('Twilio API call: ' + messageString);
  req.write(messageString);
  req.end();
}

Credits

KOJI TATEOKA

KOJI TATEOKA

1 project • 0 followers

Comments