Created
February 27, 2019 21:38
-
-
Save taf2/1f87a452cc0fd26bc97414bc52e485d6 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
/* | |
Copyright 2018 CallTrackingMetrics | |
Integrating phone calls with openweathermap.org. | |
*/ | |
function KelvinToFahrenheit(kelvin) { | |
return Math.round(((9/5) * kelvin) - 459.67); | |
} | |
exports.handler = function(event, context, callback) { | |
let url = "https://api.openweathermap.org/data/2.5/weather"; | |
let zip = event.options.input; | |
url += "?zip=" + zip + "&appid=" + process.env.appid; | |
console.log(url); | |
if (!zip) { | |
let payload = [{action:"tag", data: "nozipcode"}]; | |
payload.push({action: 'say', data: "I did not get a zip code for your location."}); | |
payload.push({action: 'hangup'}); | |
console.log("payload:", payload); | |
context.done(null, payload); | |
return; | |
} | |
context.http_get(url).then(function(res){ | |
//console.log(res.responseBody); | |
let data = JSON.parse(res.responseBody); | |
let payload = [{action:"tag", data: "zipinput"}]; | |
// say:Joanna:en-US:What ever you want to say goes here | |
let voice = "say:Joanna:en-US" | |
let message = ""; | |
if (data.weather) { | |
let weather = data.weather.map(function(data) { return data.description }); | |
let location = data.name; | |
message = "The weather in " + location + " is " + KelvinToFahrenheit(data.main.temp) + | |
" degrees with " + data.main.humidity + ' percent humidity, expect ' + weather.join(" and "); | |
} else if (data.message) { | |
message = data.message; | |
} else { | |
message = "i don't understand you"; | |
} | |
payload.push({action: 'say', data: voice + ":" + message }); | |
payload.push({action: 'hangup'}); | |
console.log("payload:", payload); | |
context.done(null, payload); | |
}).catch(function(res) { | |
console.log(res); | |
context.done(null, [ | |
{action: 'say', data: "i don't understand you"}, | |
{action: 'hangup'} | |
]); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment