-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
68 lines (54 loc) · 2.88 KB
/
weather.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
exports.handler = function(context, event, callback) {
const body = event.Body ? event.Body.toLowerCase(): null ;
// const fs = require('fs'); //Opening a private JSON does not need fs
let file = Runtime.getAssets()['weather.json'].open(); //Opening the JSON file stored in Twilio Assets
// let text = fs.readFileSync(file).toString('utf-8');
// let jsonobj = JSON.stringify(file);
let jsonobj1 = JSON.parse(file); // Parse the JSON file to Javascript object array
console.log(jsonobj1) // Check the file has been parse correctly on the console
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
//Check for a special key
//Currently Twilio Messaging Platform has a length limit of 1600 characters. The raw JSON array would exceed this length.
if (key === "all") {
for (var i = 0; i < jsonobj1.forecasts.length; i++){
let obj =jsonobj1.forecasts[i];
keyval += jsonobj1.forecasts[i].area + " : " + jsonobj1.forecasts[i].forecast + "\n" ;
}
}
// check for key "area" to print out all the avail regions
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)
}}
// looping through the JSON Javascript object array
else
for (var i = 0; i < jsonobj1.forecasts.length; i++){
var obj =jsonobj1.forecasts[i]
if (obj.area.toLowerCase() === key.toLowerCase()) { //Using lowercase for matching
//obtaining the value property of the key matched in the key-value array
keyval = obj.forecast
msg = 1 //set the variable to determine which message to send back
}
// console.log("the search result is " + keyval); // including this will print out the length of the array - only for debugging
}
// 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 resp1 = "The Areas/Regions for Weather forecasts are: " + tempkeyval;
var resp = "The weather in " + body + " is " +keyval; //constructing the message response, twilio twl does not support var within
let twiml = new Twilio.twiml.MessagingResponse();
if (msg === 0) { // determining which message to send back
twiml.message(resp1)
}
else
twiml.message(resp); // using the var that contains the complete reponse message
console.log(resp + " " + keyval);
// console.log('Your file contents: ' + text);
callback(null, twiml);
}