This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
272 lines (240 loc) · 12.7 KB
/
index.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// MIT License
//
//
// Copyright (c) 2017 Red Pebble by John Ozbay, Shelby Hutchison, Senem Cinar
// http://pebble.red
// Made using the amazing API from http://marsweather.ingenology.com/ with data from Centro de Astrobiologia (CSIC-INTA)
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
process.env.DEBUG = 'actions-on-google:*';
var Assistant = require('actions-on-google').ApiAiAssistant;
var firebase = require("firebase-admin");
var http = require('http');
//////////////////////////////////////////////////////
// INITIALIZE FIREBASE ADMIN WITH CREDENTIALS HERE. //
firebase.initializeApp({ ... });
//////////////////////////////////////////////////////
//Useful to pick random strings from an array.
Array.prototype.random = function () {
return this[Math.floor((Math.random()*this.length))];
};
// For Google Cloud Functions
exports.redPebble = function redPebble(req, res) {
var assistant = new Assistant({request: req, response: res});
var userId = assistant.getUser().user_id;
var marsData;
//Check if this is an existing user, if so, don't ask if they'd like to know more etc.
function checkUser (userId, sentence){
var milliseconds = (new Date()).getTime();
firebase.database().ref('users/' + userId).once('value', function (data) {
if (data.val()) {
//existing user
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "getMarsWeather - existing" },function(){
assistant.tell(sentence);
});
} else {
firebase.database().ref('users/' + userId).update({ "lastused": milliseconds },function(){
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "getMarsWeather - new" },function(){
assistant.ask(sentence + " Would you like to know other things, like the season or sunset time on mars?");
});
});
}
});
}
// Didn't understand what the user said.
function unhandledDeepLinks () {
var sentence;
var idks = [
"Houston, we have a problem. I didn’t understand the weather.",
"Meteors caused radio interference, can you repeat?",
"Sorry! The line spaced out, can you repeat that?"
];
sentence = idks.random();
var milliseconds = (new Date()).getTime();
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "unhandledDeepLinks" },function(){
assistant.ask("Houston we have a problem. Didn't understand the question. Could you repeat that?");
});
}
// "What's the weather like on Mars?"
function getMarsWeather () {
var cAvg = Math.floor((marsData.report.min_temp + marsData.report.max_temp) / 2);
var fAvg = Math.floor((marsData.report.min_temp_fahrenheit + marsData.report.max_temp_fahrenheit) / 2);
var temp = fAvg;
var sentence = "";
var hots = [
"Gosh it's hot! It's a " + marsData.report.atmo_opacity + " " + temp + " degrees right now!",
"It’d be pretty nice to have a beach right about now. It’s a hot " + temp + " degrees.",
];
var warms = [
"Great! You're just in time for lemonade! Today will be a lovely " + temp + " degrees!",
"A warm day out here. It’s a lovely " + temp + " degrees!",
];
var neutrals = [
"What a beautiful day! Today is a perfect " + marsData.report.atmo_opacity + " " + temp + " degrees!",
"Get out that sun visor, we’re going on a space walk! It’s a great " + temp + " degrees!",
];
var colds = [
"Get those hand warmers, today is a chilly " + temp + " degrees!",
"Break out those space boots! It’s a cold " + temp + " degrees!",
];
var frozens = [
"Can planets hibernate? It's a freezing " + marsData.report.atmo_opacity + " " + temp + " degrees!",
"Now I know how Pluto feels. It’s only " + temp + " degrees.",
];
if (temp <= -146) { sentence = frozens.random(); }
if ((temp > -146) && (temp <= -92)) { sentence = colds.random(); }
if ((temp > -92) && (temp <= -38)) { sentence = neutrals.random(); }
if ((temp > -38) && (temp <= 16)) { sentence = warms.random(); }
if ((temp > 16) && (temp <= 70)) { sentence = hots.random(); }
if (temp > 70) { sentence = hots.random(); }
checkUser (userId, sentence);
}
// "What's the date on Earth?"
function getEarthDate () {
var milliseconds = (new Date()).getTime();
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "getEarthDate" },function(){
assistant.ask("Today's date is " + marsData.report.terrestrial_date + ' on Earth! Do you have more questions? I would love to hear them.');
});
}
// "What's the date on Mars?"
function getMarsDate () {
var milliseconds = (new Date()).getTime();
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "getMarsDate" },function(){
assistant.ask('Today is Sol ' + marsData.report.sol + " on Mars! Do you have more questions? I'd love to hear them. ");
});
}
// "What's the season on Mars?"
function getSeason () {
var season;
var sentence;
if (marsData.report.ls <= 90) { season = "spring"; sentence = "It’s Spring at the moment, so grab those boots and come on a space walk!";}
if ((marsData.report.ls > 90) && (marsData.report.ls <= 180)) { season = "summer"; sentence = "It’s Summer at the moment. Bring on that sunshine!";}
if ((marsData.report.ls > 180) && (marsData.report.ls <= 270)) { season = "autumn"; sentence = "It’s Fall at the moment, but don’t be fooled, I’m red all year round.";}
if (marsData.report.ls > 270) { season = "winter"; sentence = "It’s Winter, so keep a jacket handy if you’re visiting!";}
var milliseconds = (new Date()).getTime();
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "getSeason" },function(){
assistant.ask(sentence + " What more would you like to know about my red planet self?");
});
}
// "What's the lowest temperature on Mars?"
function getMinTemp () {
var milliseconds = (new Date()).getTime();
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "getMinTemp" },function(){
assistant.ask('The lowest temperature on Mars today is ' + Math.floor(marsData.report.min_temp_fahrenheit) + ' degrees! I would love to answer more questions if you have some?');
});
}
// "What's the highest temperature on Mars?"
function getMaxTemp () {
var milliseconds = (new Date()).getTime();
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "getMaxTemp" },function(){
assistant.ask('The highest temperature on Mars today is ' + Math.floor(marsData.report.max_temp_fahrenheit) + ' degrees! What else would you like to know?');
});
}
// "Is it cold on Mars?"
function getCold () {
var cAvg = Math.floor((marsData.report.min_temp + marsData.report.max_temp) / 2);
var fAvg = Math.floor((marsData.report.min_temp_fahrenheit + marsData.report.max_temp_fahrenheit) / 2);
var milliseconds = (new Date()).getTime();
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "getCold" },function(){
assistant.ask('Yes! Today is ' + fAvg + " degrees. If you're coming to visit me, have a jacket handy. What more would you like to know about my red planet self?");
});
}
// "What's the atmospheric pressure on Mars?"
function getAtmPres () {
var milliseconds = (new Date()).getTime();
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "getAtmPres" },function(){
assistant.ask('The average atmospheric pressure on Mars today is ' + Math.floor(marsData.report.pressure) + ' pascals! Do you have more adventurous questions up your sleeve?');
});
}
// "When does the sun set on Mars?" -- According to Curiosity Rover
function getSunset () {
var time = marsData.report.sunset.split('T')[1];
var hour = time.split(":")[0];
var mins = time.split(':')[1];
var milliseconds = (new Date()).getTime();
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "getSunset" },function(){
assistant.ask('Where the Curiosity rover is, the sunset should be at ' + hour + " " + mins + ". It’s really beautiful. Curiosity the rover even took photos of it and uploaded them online. You should check it out. I'd love to answer more questions if you have some?");
});
}
// "When's the sunrise on Mars?" -- According to Curiosity Rover
function getSunrise () {
var time = marsData.report.sunrise.split('T')[1];
var hour = time.split(":")[0];
var mins = time.split(':')[1];
var milliseconds = (new Date()).getTime();
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "getSunrise" },function(){
assistant.ask('Where the Curiosity rover is, the sunrise should be at ' + hour + " " + mins + ". It’s my favourite thing to wake up to. What else would you like to know?");
});
}
// "Does the sun set on Mars?"
function doesSunset () {
var time = marsData.report.sunset.split('T')[1];
var hour = time.split(":")[0];
var mins = time.split(':')[1];
var milliseconds = (new Date()).getTime();
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "doesSunset" },function(){
assistant.ask('Yes! Where the Curiosity rover is, the sunset should be at ' + hour + " " + mins + ". Do you have more adventurous questions up your sleeve?");
});
}
// "Does the sun rise on Mars?"
function doesSunrise () {
var time = marsData.report.sunrise.split('T')[1];
var hour = time.split(":")[0];
var mins = time.split(':')[1];
var milliseconds = (new Date()).getTime();
firebase.database().ref('stats/' + milliseconds).update({ user: userId, comm: "doesSunrise" },function(){
assistant.ask('Yes! Where the Curiosity rover is, the sunrise should be at ' + hour + " " + mins + ". Do you have more questions? I'd love to hear them.");
});
}
// Main response handler that checks the latest weather json file. Updated daily.
function responseHandler (callback){
http.get('http://pebble.red/latest.json', function (res) {
if (('' + res.statusCode).match(/^2\d\d$/)) {
res.setEncoding('utf8');
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on("end", function() {
console.log("BODY: ", body);
marsData = JSON.parse(body);
console.log("MARSDATA: ", marsData);
var intent = assistant.getIntent();
switch (intent) {
case 'deeplink.unknown' : unhandledDeepLinks(); break;
case 'get_mars_weather' : getMarsWeather(); break;
case 'get_mars_earth_date' : getEarthDate(); break;
case 'get_mars_date' : getMarsDate(); break;
case 'get_mars_season' : getSeason(); break;
case 'get_mars_min_temp' : getMinTemp(); break;
case 'get_mars_max_temp' : getMaxTemp(); break;
case 'get_mars_atmospheric_pressure' : getAtmPres(); break;
case 'get_mars_sunset' : getSunset(); break;
case 'get_mars_sunrise' : getSunrise(); break;
case 'get_does_sunset' : doesSunset(); break;
case 'get_does_sunrise' : doesSunrise(); break;
case 'get_mars_cold' : getCold(); break;
}
});
}
});
}
assistant.handleRequest(responseHandler);
};