-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
80 lines (71 loc) · 2.5 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
const weather = require('./module/weather');
function close(sessionAttributes, fulfillmentState, message, responseCard) {
return {
sessionAttributes,
dialogAction: {
type: 'Close',
fulfillmentState,
message,
responseCard,
},
};
}
module.exports.cityWeather = (event, context, callback) => {
const city = event.currentIntent.slots.city;
const details = 'You want weather from ' + city;
const outputSessionAttributes = event.sessionAttributes || {};
weather.weatherFromCity(city, function (data) {
function prepareWeatherReport() {
var sky = data.weather[0].main;
if (sky.indexOf('Clouds') === 0) {
sky = 'Sky is Cloudy';
} else if (sky.indexOf('Haze') === 0) {
sky = 'Sky is Hazy';
} else if (sky.indexOf('Rain') === 0) {
sky = 'It is rainy';
} else {
sky = 'Sky is ' + sky;
}
const temp = data.main.temp;
const humidity = data.main.humidity;
const windspeed = data.wind.speed;
var report = "Weather information from " + city + ". " + sky + ".";
report = report + " Temperature is " + temp + " degree celcius.";
report = report + " Humidity is " + humidity + " percent.";
report = report + " Wind speed is " + windspeed + " meters per second.";
return report;
}
if (data) {
if(data.cod == '401') {
callback(null, close(outputSessionAttributes, 'Fulfilled', {
contentType: 'PlainText',
content: 'There was an error fetching weather from ' + city
}));
} else if (data.cod == '404') {
callback(null, close(outputSessionAttributes, 'Fulfilled', {
contentType: 'PlainText',
content: 'Sorry! Could not gather weather information from the provided city. Please try again!'
}));
} else {
var report = prepareWeatherReport();
callback(null, close(outputSessionAttributes, 'Fulfilled', {
contentType: 'PlainText',
content: report
}));
}
} else {
callback(null, close(outputSessionAttributes, 'Fulfilled', {
contentType: 'PlainText',
content: 'Sorry! Could not recognize the city. Please try again!'
}));
}
});
};
module.exports.greetUser = (event, context, callback) => {
const outputSessionAttributes = event.sessionAttributes || {};
callback(null, close(outputSessionAttributes, 'Fulfilled', {
contentType: 'PlainText',
content: 'Hello. Just say, tell me weather.'
}));
};