Last active
April 2, 2018 13:48
-
-
Save realadeel/aa25560c6ca04f3cd22deb90c91dc644 to your computer and use it in GitHub Desktop.
Pressing an Amazon AWS IoT Button to Find the Rright Restaurant - Node.js on AWS Lambda
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
var AWS = require('aws-sdk'); | |
var sns = new AWS.SNS(); | |
var GoogleMaps = require('googlemaps'); | |
var GooglePlaces = require('node-googleplaces'); | |
var weather = require('openweather-apis'); | |
var stripe = require('stripe')(STRIPE_SECRET_KEY); | |
var googleConfig = { key: GOOGLE_API_KEY }; | |
var maps = new GoogleMaps(googleConfig); | |
var places = new GooglePlaces(googleConfig.key); | |
var direction_params = { | |
origin: 'LAT,LONG', | |
}; | |
weather.setLang('en'); | |
weather.setUnits('imperial'); | |
weather.setAPPID(OPENWEATHERMAP_API_KEY); | |
weather.setCoordinate(LAT, LONG); | |
exports.handler = (event, context) => { | |
weather.getAllWeather(function(err, response){ | |
if ([800, 801, 802, 803, 804, 951, 952, 953, 954, 955].indexOf(response.weather.id) && event.clickType == 'SINGLE') { | |
params = {location: 'LAT,LONG', | |
radius: 200, | |
type: 'restaurant', | |
opennow: true, | |
maxprice: 2 | |
}; | |
places.nearbySearch(params, (err, res) => { | |
index = Math.floor(Math.random() * res.body.results.length); | |
places.details({placeid: res.body.results[index].place_id}, (err, res) => { | |
direction_params.mode = 'walking'; | |
direction_params.destination = 'place_id:'+res.body.result.place_id; | |
maps.directions(direction_params, function(err, directions) { | |
sns.publish({ | |
Message: "Beautiful day! Walk over to " + res.body.result.name + " at " + res.body.result.vicinity + ". It'll take " + directions.routes[0].legs[0].duration.text, | |
TopicArn: SNS_TOPIC_ARN | |
}, function(error, data) { | |
if (error) { | |
context.fail(null, 'SNS error'); | |
} | |
context.done(null, 'Sent SMS!'); | |
}); | |
}); | |
}); | |
}); | |
} else { | |
stripe.balance.retrieve(function(err, balance) { | |
if (((balance.available.amount + balance.pending.amount) > 100000) || event.clickType == 'DOUBLE') { | |
params = {location: 'LAT,LONG', | |
radius: 10000, | |
type: 'restaurant', | |
opennow: true, | |
minprice: 3 | |
}; | |
places.nearbySearch(params, (err, res) => { | |
index = Math.floor(Math.random() * res.body.results.length); | |
places.details({placeid: res.body.results[index].place_id}, (err, res) => { | |
direction_params.mode = 'walking'; | |
direction_params.destination = 'place_id:'+res.body.result.place_id; | |
maps.directions(direction_params, function(err, directions) { | |
sns.publish({ | |
Message: "Treat yourself at " + res.body.result.name + " at " + res.body.result.vicinity + ". It'll take " + directions.routes[0].legs[0].duration.text + " driving. Want to call a Lyft?", | |
TopicArn: SNS_TOPIC_ARN | |
}, function(error, data) { | |
if (error) { | |
context.fail(null, 'SNS error'); | |
} | |
context.done(null, 'Sent SMS!'); | |
}); | |
}); | |
}); | |
}); | |
} else { | |
params = {location: 'LAT,LONG', | |
radius: 10000, | |
type: 'meal_delivery', | |
opennow: true, | |
maxprice: 2 | |
}; | |
places.nearbySearch(params, (err, res) => { | |
index = Math.floor(Math.random() * res.body.results.length); | |
places.details({placeid: res.body.results[index].place_id}, (err, res) => { | |
sns.publish({ | |
Message: "F-it. Just get delivery from " + res.body.result.name + ". The number is " + res.body.result.formatted_phone_number, | |
TopicArn: SNS_TOPIC_ARN | |
}, function(error, data) { | |
if (error) { | |
context.fail(null, 'SNS error'); | |
} | |
context.done(null, 'Sent SMS!'); | |
}); | |
}); | |
}); | |
} | |
}); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I executed the code, but "LAT,LONG" are not defined, could you please explain why ?