Last active
April 5, 2018 15:40
-
-
Save dileephell/0f65ad2306af20baa17fada02b93fb33 to your computer and use it in GitHub Desktop.
weatherApi.js
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
| function elicitSlot(sessionAttributes, intentName, slots, slotToElicit, message, responseCard) { | |
| return { | |
| sessionAttributes, | |
| dialogAction: { | |
| type: 'ElicitSlot', | |
| intentName, | |
| slots, | |
| slotToElicit, | |
| message, | |
| responseCard, | |
| }, | |
| }; | |
| } | |
| function delegate(sessionAttributes, slots) { | |
| return { | |
| sessionAttributes, | |
| dialogAction: { | |
| type: 'Delegate', | |
| slots, | |
| }, | |
| }; | |
| } | |
| function close(sessionAttributes, fulfillmentState, message) { | |
| return { | |
| sessionAttributes, | |
| dialogAction: { | |
| type: 'Close', | |
| fulfillmentState, | |
| message, | |
| }, | |
| }; | |
| } | |
| function buildValidationResult(isValid, violatedSlot, messageContent) { | |
| if (messageContent == null) { | |
| return { | |
| isValid, | |
| violatedSlot | |
| }; | |
| } | |
| return { | |
| isValid, | |
| violatedSlot, | |
| message: { contentType: 'PlainText', content: messageContent } | |
| }; | |
| } | |
| var http = require('http'); | |
| 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; | |
| console.log(body + "I am in the body"); | |
| }); | |
| res.on('end', function(){ | |
| console.log('end event'); | |
| var result = JSON.parse(body); | |
| console.log(typeof result); | |
| console.log(result + "I am the result"); | |
| callback(null, result); | |
| }) | |
| res.on('error', function(error){ | |
| console.log('Error event'); | |
| callback('error', callback); | |
| console.log(error + "I am the error"); | |
| }) | |
| }) | |
| .on('error', callback) | |
| .end(); | |
| } | |
| function getCityWeather(cityName, outputSessionAttributes,callback){ | |
| 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 error in getjson"); | |
| 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); | |
| }); | |
| return callback; | |
| } | |
| function getWeatherUpdate(intentRequest, callback) { | |
| const source = intentRequest.invocationSource; | |
| const outputSessionAttributes = intentRequest.sessionAttributes || {}; | |
| console.log("outputSessionArribute", intentRequest.sessionAttributes); | |
| if (source === 'DialogCodeHook') { | |
| var slots = intentRequest.currentIntent.slots; | |
| //const country = intentRequest.currentIntent.slots.TodayWeatherCountry; | |
| const cityName = intentRequest.currentIntent.slots.TodayWeatherCity; | |
| //without promise implemeation | |
| var validationResult = getCityWeather(cityName, outputSessionAttributes,callback); | |
| console.log(validationResult + " Here is the validationResult"); | |
| 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(delegate(outputSessionAttributes, slots)); | |
| 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 ${cityName} is ${outputSessionAttributes.temprature}` })); | |
| // } | |
| //get the value from the session variable and prompt to user | |
| callback(close(outputSessionAttributes, 'Fulfilled', { contentType: 'PlainText', | |
| content: `Sorry, I couldn't server your request` })); | |
| } | |
| // --------------- Main handler ----------------------- | |
| // Route the incoming request based on intent. | |
| // The JSON body of the request is provided in the event slot. | |
| exports.handler = (event, context, callback) => { | |
| try { | |
| getWeatherUpdate(event, | |
| (response) => { | |
| callback(null, response); | |
| }); | |
| } catch (err) { | |
| callback(err); | |
| } | |
| }; | |
| -----------COnfigure Test Event---------------- | |
| { | |
| "messageVersion": "1.0", | |
| "invocationSource": "DialogCodeHook", | |
| "userId": "user-1", | |
| "sessionAttributes": {}, | |
| "bot": { | |
| "name": "Weather", | |
| "alias": "$LATEST", | |
| "version": "$LATEST" | |
| }, | |
| "outputDialogMode": "Text", | |
| "currentIntent": { | |
| "name": "GetWeather", | |
| "slots": { | |
| "greet": "name", | |
| "TodayWeatherCountry": "USA", | |
| "TodayWeatherCity": "washington" | |
| }, | |
| "confirmationStatus": "None" | |
| }, | |
| "rejectUnauthorized": false | |
| } | |
| ----------- Lambda Response after running the code ----------------- | |
| Response: | |
| { | |
| "sessionAttributes": {}, | |
| "dialogAction": { | |
| "type": "ElicitSlot", | |
| "intentName": "GetWeather", | |
| "slots": { | |
| "greet": "name", | |
| "TodayWeatherCountry": "USA", | |
| "TodayWeatherCity": "washington", | |
| "undefined": null | |
| } | |
| } | |
| } | |
| Request ID: | |
| "5176a201-38e7-11e8-832b-c9a9a7cb551e" | |
| Function Logs: | |
| START RequestId: 5176a201-38e7-11e8-832b-c9a9a7cb551e Version: $LATEST | |
| 2018-04-05T15:37:59.048Z 5176a201-38e7-11e8-832b-c9a9a7cb551e outputSessionArribute {} | |
| 2018-04-05T15:37:59.083Z 5176a201-38e7-11e8-832b-c9a9a7cb551e (response) => { | |
| callback(null, response); | |
| } Here is the validationResult | |
| 2018-04-05T15:37:59.084Z 5176a201-38e7-11e8-832b-c9a9a7cb551e after calling getCityWeather with result | |
| 2018-04-05T15:37:59.184Z 5176a201-38e7-11e8-832b-c9a9a7cb551e calling the http | |
| 2018-04-05T15:37:59.203Z 5176a201-38e7-11e8-832b-c9a9a7cb551e body{"coord":{"lon":-77.04,"lat":38.9},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":278.9,"pressure":1024,"humidity":35,"temp_min":277.15,"temp_max":280.15},"visibility":16093,"wind":{"speed":4.1,"deg":290,"gust":7.7},"clouds":{"all":90},"dt":1522941480,"sys":{"type":1,"id":1325,"message":0.1706,"country":"US","sunrise":1522925137,"sunset":1522971375},"id":4366164,"name":"Washington DC.","cod":200} | |
| 2018-04-05T15:37:59.203Z 5176a201-38e7-11e8-832b-c9a9a7cb551e {"coord":{"lon":-77.04,"lat":38.9},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":278.9,"pressure":1024,"humidity":35,"temp_min":277.15,"temp_max":280.15},"visibility":16093,"wind":{"speed":4.1,"deg":290,"gust":7.7},"clouds":{"all":90},"dt":1522941480,"sys":{"type":1,"id":1325,"message":0.1706,"country":"US","sunrise":1522925137,"sunset":1522971375},"id":4366164,"name":"Washington DC.","cod":200}I am in the body | |
| 2018-04-05T15:37:59.204Z 5176a201-38e7-11e8-832b-c9a9a7cb551e end event | |
| 2018-04-05T15:37:59.204Z 5176a201-38e7-11e8-832b-c9a9a7cb551e object | |
| 2018-04-05T15:37:59.223Z 5176a201-38e7-11e8-832b-c9a9a7cb551e [object Object]I am the result | |
| 2018-04-05T15:37:59.223Z 5176a201-38e7-11e8-832b-c9a9a7cb551e 278.9 value | |
| END RequestId: 5176a201-38e7-11e8-832b-c9a9a7cb551e | |
| REPORT RequestId: 5176a201-38e7-11e8-832b-c9a9a7cb551e Duration: 176.93 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 21 MB | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment