-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_directAPI.js
89 lines (82 loc) · 4 KB
/
weather_directAPI.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
const axios = require('axios');
const date = require('date-and-time');
exports.handler = function(context, event, callback) {
const body = event.Body ? event.Body.toLowerCase(): null ;
let twiml = new Twilio.twiml.MessagingResponse();
let key = body;
//obtain current UTC time with +GMT8 offset and shorten into a single variable with data.format and offset
const now = date.format(date.addHours(new Date(), +8), 'YYYY-MM-DDTHH:mm:ss');
const addr = 'https://api.data.gov.sg/v1/environment/2-hour-weather-forecast?date_time='+now ;
console.log(addr);
let jsonobj1 = []; //global variable to hold the axios response
return axios.get(addr).then(response => {
jsonobj = JSON.stringify(response.data.items).substr(1,JSON.stringify(response.data.items).length-2);
jsonobj1 = JSON.parse(jsonobj);
}).catch(error =>{
console.log("There is an error");
twiml.message("Sorry, there seems to be an error, please try again later");
callback(null, twiml); // send a message back to user if remote NEA API Server is unavailable
})
.then(data => {
var key = body; //In Twilio function, the body constant though inherit BODY event but cannot be used for later uses. declaring a var to inherit body
var keyval = [] ; //the array to obtain the value property of the key
var msg = "" ; // the response message type to be send back
var keyarea = "" ; // to obtain the matched area
var timestamp = (jsonobj1.valid_period.end).substr(0,10) + " " + (jsonobj1.valid_period.end).substr(11,5) +"h" ; // print valid timestamp
//obtaining the value property of the key matched in the returned axios key-value array
for (var i = 0; i < jsonobj1.forecasts.length; i++){
var obj =jsonobj1.forecasts[i] ;
//Check for a special key
if (key === "all") {
// Print out the whole JS array
// Currently Twilio Messaging Platform has a length limit of 1600 characters. The raw json array would exceed this length. Parse the array to obtain only the "area" and "forecast" field
keyval += jsonobj1.forecasts[i].area + " : " + jsonobj1.forecasts[i].forecast + "\n" ;
console.log(keyval);
msg = 2;
}
// check for key "area" to print out all the avail regions/area.
else if
(key === "area") {
var x = "area" ;
var tempkeyval = {} ;
for (x in jsonobj1.forecasts) {
tempkeyval += jsonobj1.forecasts[x].area + " " ;
msg = 0 ; // set the variable to determine which message to send back
console.log(tempkeyval) ;
} break ;
}
else if
// looping through the JSON Javascript object array
//for (var i = 0; i < jsonobj1.forecasts.length; i++){
// var obj =jsonobj1.forecasts[i] ;
(key === obj.area.toLowerCase()) {
keyval = obj.forecast ;
keyarea = obj.area ;
msg =1 ; //set the variable to determine which message to send back
break ; // break is necessary to get out of the loop. Seems to be a bug even when true, send to next action.
} else {
msg = 3 ;
}
}
// keyval = jsonobj1.forecasts['key']; // this only work for JSON JS object but the imported file is JSON JS array instead
console.log("The key is " + key +" , " + keyval);
var resp3 = "The valid keyword to use is All, Area, and the Area-name. Type Area to obtain the Area-name, follow by Area-name of your choice" ;
var resp2 = "The weather forecast in Singapore " +keyval +"\n" +"Valid till: " + timestamp ;
var resp1 = "The areas for weather forecasts are: " + tempkeyval;
var resp = "The weather in " + keyarea + " is " +keyval + "\n" + "Valid till: " + timestamp ; //constructing the message response, twilio twl does not support var within
if (msg === 0) {
twiml.message(resp1) ;
}
if (msg === 1) {
twiml.message(resp) ;
}
if (msg === 2) {
twiml.message(resp2) ;
}
if (msg === 3) {
twiml.message(resp3) ;
}
console.log(resp + " " + keyval);
callback(null, twiml);
});
};