Created
February 7, 2018 14:59
-
-
Save dileephell/554aa4b0e26bb477f99ba8b3afe1dee2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 http = require('http'); | |
| exports.handler = (event, context,callback) => { | |
| function getJSON(options, callback){ | |
| http.request(options, function(res){ | |
| var body = ""; | |
| console.log('calling the http'); | |
| res.on('data', function(chunk){ | |
| console.log('body' + chunk); | |
| body+=chunk; | |
| }); | |
| res.on('end', function(){ | |
| console.log('end event'); | |
| var result = JSON.parse(body); | |
| callback(null, result); | |
| }) | |
| res.on('error', function(error){ | |
| console.log('Error event'); | |
| callback('error', callback); | |
| }) | |
| console.log("getting the response"+ options + typeof options); | |
| }) | |
| .on('error', callback) | |
| .end(); | |
| } | |
| function buildValidationResult(isValid, violatedSlot, messageContent, options) { | |
| if (messageContent == null) { | |
| return { | |
| isValid, | |
| violatedSlot, | |
| options | |
| }; | |
| } | |
| return { | |
| isValid, | |
| violatedSlot, | |
| message: { contentType: 'PlainText', content: messageContent }, | |
| options | |
| }; | |
| } | |
| function getCityWeather(cityName, outputSessionAttributes){ | |
| var options = { | |
| host: `api.openweathermap.org`, | |
| port: 80, | |
| path: `/data/2.5/weather?q=${cityName}&appid=b20db28dc5de27f47ce67d6381326268`, | |
| method: 'GET' | |
| }; | |
| getJSON(options, function(err, result){ | |
| if(err){ | |
| console.log(err + "i am the error"); | |
| return buildValidationResult(false, 'TodayWeatherCity', `Invalid city name. Please let me know the city again.`); | |
| } | |
| outputSessionAttributes.temprature = result.main.temp; | |
| console.log(outputSessionAttributes.temprature + ' value'); | |
| return buildValidationResult(true, null, null); | |
| console.log("validationresult"+ buildValidationResult); | |
| }); | |
| function getWeatherUpdate(intentRequest, callback) { | |
| const country = intentRequest.currentIntent.slots.TodayWeatherCountry; | |
| const city = intentRequest.currentIntent.slots.TodayWeatherCity; | |
| const source = intentRequest.invocationSource; | |
| const outputSessionAttributes = intentRequest.sessionAttributes || {}; | |
| console.log("Invocation sourec" + invocationSource); | |
| console.log("outputSessionArribute", intentRequest.sessionAttributes); | |
| if (source === 'DialogCodeHook') { | |
| const slots = intentRequest.currentIntent.slots; | |
| //without promiss implemeation | |
| var validationResult = getCityWeather(city, outputSessionAttributes); | |
| if(!validationResult.isValid) { | |
| console.log('after calling getCityWeather with result'); | |
| slots[`${validationResult.violatedSlot}`] = null; | |
| //if response not found then return the invalid city message | |
| callback(elicitSlot(intentRequest.sessionAttributes, intentRequest.currentIntent.name, slots, validationResult.violatedSlot, validationResult.message)); | |
| return; | |
| } | |
| console.log('getWeatherUpdate after calling getCityWeather'); | |
| callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText',content: `Sorry, I couldn't server your request` })); | |
| return; | |
| } | |
| console.log('getWeatherUpdate after DialogCodeHook'); | |
| /* if(outputSessionAttributes.temprature){ | |
| console.log('getWeatherUpdate inside outputSessionAttributes.temprature return'); | |
| //get the value from the session variable and prompt to user | |
| callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText', | |
| content: `Okay, temprature reading for ${city} is ${outputSessionAttributes.temprature}` })); | |
| }*/ | |
| //get the value from the session variable and prompt to user | |
| } | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment