-
Notifications
You must be signed in to change notification settings - Fork 2
/
WeatherForecast.js
213 lines (199 loc) · 6.27 KB
/
WeatherForecast.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// Action Fulfillment of an invitation action.
//
// Input contains:
// - spaceId: SpaceId the command was submitted in.
// - limit: max number of invites to generate cards for (defaults to 10)
//
var openwhisk = require("openwhisk");
var _ = require("lodash");
var request = require("superagent");
var fs = require("fs");
var mustache = require("mustache");
// Look for any locations mentioned
function getLocation(focus) {
return new Promise((resolve,reject) => {
// Get a list of location text from the focus.
var locations = [];
var locations = _.chain(focus.annotationPayload.extractedInfo.entities)
.filter(entity => {
return entity.type === "Location";
})
.map(entity => {
return entity.text;
})
.value();
if (locations.length > 0) {
// We have some location text, so let's see if we can geolocate it.
request
.get(focus.__bx_creds.weatherinsights.url + "/api/weather/v3/location/search")
.query({ query: locations.join(), language: 'en-US' })
.then(resp => {
var result = JSON.parse(resp.text).location;
// Assume we want the first one and add to focus
focus.location = {
city: result.city[0],
state: result.adminDistrict[0],
latitude: result.latitude[0],
longitude: result.longitude[0],
country: result.country[0],
location: [result.city[0], result.adminDistrict[0], result.country[0]].join(", ")
}
resolve(focus);
}).catch(err => {
reject({
error: err,
focus: focus
});
})
}
else {
resolve(focus);
}
})
}
// Look for and dates mentioned.
function getDates(focus) {
return new Promise((resolve, reject) => {
// Get a list of date texts from the focus. Might include one, or two in the case of a date range
var dates = [];
var dates = _.filter(focus.annotationPayload.extractedInfo.entities, entity => {
if (entity.type === "sys-date")
return entity;
})
var from = false;
var to = false;
// If we have dates add them to the focus.
if (dates.length === 1 || dates.length === 2) {
from = dates[0];
to = dates.length === 2 ? dates[1] : from
focus.fromDay = from.text;
focus.toDay = to.text;
}
resolve(focus);
})
}
// Get the current conditions given a specific location
function getConditions(focus) {
return new Promise((resolve,reject) => {
request
.get(focus.__bx_creds.weatherinsights.url + "/api/weather/v1/geocode/" + focus.location.latitude + "/" + focus.location.longitude + "/observations.json")
.query({ language: 'en-US' })
.then(resp => {
var conditions = resp.body.observation;
var text = mustache.render(`Weather is {{wx_phrase}}, temp of {{temp}}, but it feels like {{feels_like}}`,conditions)
resolve({
spaceId: focus.spaceId,
title: "The current weather at " + focus.location.location,
text: text,
actor: {
name: "Weather Bot"
}
});
}).catch(err => {
reject(err);
})
})
}
// Response when no location is found
function noLocation(focus) {
return {
spaceId: focus.spaceId,
title: "Where exactly?",
text: "I need to know a location in order to tell you the weather there...",
actor: {
name: "Weather Bot"
}
}
}
// Get the forecast at a given location between two days
function getForecast(focus) {
return new Promise((resolve,reject) => {
request
.get(focus.__bx_creds.weatherinsights.url + "/api/weather/v1/geocode/" + focus.location.latitude + "/" + focus.location.longitude + "/forecast/daily/10day.json?")
.query({ language: 'en-US' })
.then(resp => {
var forecasts = resp.body.forecasts;
var result = _.chain(forecasts)
.filter(forecast => {
var date = new Date(Date.parse(forecast.sunrise));
forecast.date = date.toISOString().substr(0,10);
forecast.dateString = date.toDateString();
return (forecast.date >= focus.fromDay && forecast.date <= focus.toDay)
})
.map(forecast => {
var strs = [];
var items = _.pick(forecast,["day","night"]);
_.keys(items).forEach(item => {
strs.push("*" + forecast.dateString + " " + item + ":* " + forecast[item].narrative);
})
return strs.join("\n");
})
.value();
resolve({
spaceId: focus.spaceId,
title: "Weather Forecast for " + focus.location.location,
text: result.join("\n"),
actor: {
name: "Weather Bot"
}
});
}).catch(err => {
reject(err);
})
})
}
function validateCredentials(focus) {
return new Promise((resolve,reject) => {
if (_.get(focus,"__bx_creds.weatherinsights.url"))
resolve(focus);
else {
reject({
response: {
spaceId: focus.spaceId,
title: "Missing Credentials",
text: "The WeatherInsights service has not been bound to this action, please read instructions.",
actor: {
name: "Weather Bot"
}
}
})
}
})
}
function main(focus) {
return new Promise((resolve,reject) => {
// check to see if we got a Weather Forecast Request
if (focus.annotationPayload.lens === "WeatherForecast") {
validateCredentials(focus).then(focus => {
return getLocation(focus);
}).then(focus => {
return getDates(focus);
}).then(focus => {
if (!focus.hasOwnProperty('location')) {
resolve(noLocation(focus));
}
else if (!focus.hasOwnProperty('fromDay')){
resolve(getConditions(focus));
}
else {
resolve(getForecast(focus));
}
}).catch(err => {
console.log("*** Error: " + JSON.stringify(err) + "***")
console.log("*** Focus: " + JSON.stringify(focus) + "***")
if (err.response)
resolve(err.response)
else
resolve({
spaceId: focus.spaceId,
title: "Oops!",
text: "There was an error processing, please try again later.",
actor: {
name: "Weather Bot"
}
})
});
}
})
};