/
main.js
216 lines (153 loc) · 5.79 KB
/
main.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
require("dotenv").config();
let Framework = require("webex-node-bot-framework");
let webhook = require("webex-node-bot-framework/webhook");
let express = require("express");
let bodyParser = require("body-parser");
let path = require("path");
let fs = require("fs");
let opensky = require("./webex");
scheduled_flights = [];
// The server that will accept webhooks and host the calendar
var expressApp = express();
// Allow the usage of ?= url params and json bodies
expressApp.use(bodyParser.urlencoded({extended: true}));
expressApp.use(bodyParser.json());
const config = {
webhookUrl: process.env.BOT_WEBHOOK,
token: process.env.BOT_ACCESS_TOKEN,
port: process.env.PORT || 8080 // Server runs at :8080
};
var framework = new Framework(config);
framework.start();
framework.on("initialized", function () {
framework.debug("Framework initialized successfully! [Press CTRL-C to quit]");
});
framework.on('spawn', function (bot, id, addedBy) {
if (!addedBy) {
// don't say anything here or your bot's spaces will get
// spammed every time your server is restarted
framework.debug(`Framework created an object for an existing bot in a space called: ${bot.room.title}`);
} else {
// addedBy is the ID of the user who just added our bot to a new space,
// Say hello, and tell users what you do!
bot.say('Hi there, you can say hello to me. Don\'t forget you need to mention me in a group space!');
}
});
framework.hears('hello', function(bot, trigger) {
bot.say('Hello %s!', trigger.person.displayName);
responded = true;
});
expressApp.use(express.static('public'));
expressApp.set('view engine', 'ejs');
framework.hears("schedule", function(bot, trigger) {
bot.say(
"markdown",
"Submit a new flight [here](" + encodeURI("https://shrouded-dusk-67323.herokuapp.com/newflight?displayName="+trigger.person.displayName) + ")"
);
});
/* Server stuff */
expressApp.post("/webhook", webhook(framework));
expressApp.get('/', (req, res) => res.send('Hello'));
expressApp.get("/newflight", (req, res) => {
let { spaceId, displayName } = req.query;
res.render("form", { spaceId, displayName });
});
expressApp.post("/submit", async (req, res) => {
// console.log(req.body);
let { departure, arrival, flightnumber, spaceId, displayName } = req.body;
// console.log(displayName);
// console.log(typeof departure);
let departure_unix = new Date(departure).getTime()/1000; // use .getTime() to get the date in milliseconds since 1970
let arrival_unix = new Date(departure).getTime()/1000 + 10800; // use .getTime() to get the date in milliseconds since 1970
// console.log(flightnumber);
let l = flightnumber.length;
for (let i = l; i < 8; i++) {
flightnumber += " ";
}
// console.log(opensky.scheduled_flights.length);
try {
let flight = await opensky.scheduleFlight(flightnumber, departure_unix, arrival_unix, displayName);
if (flight != null) {
scheduled_flights.push(flight);
flight.displayName = displayName;
}
res.redirect("/calendar");
} catch (err) {
console.error(err);
return res.send("Failed to schedule flight. Try again.");
}
// console.log(opensky.scheduled_flights.length);
// console.log(opensky.scheduled_flights[0].arrival);
// console.log(flightnumber.length);
// let event = [
// {
// id: '2',
// calendarId: '1',
// title: 'yo',
// category: 'time',
// dueDateClass: '',
// start: '2021-01-21T17:30:00+09:00',
// end: '2021-01-22T17:31:00+09:00',
// isReadOnly: true // schedule is read-only
// },
// {
// id: '1',
// calendarId: '2',
// title: 'second schedule',
// category: 'time',
// dueDateClass: '',
// start: '2021-01-14T17:30:00+09:00',
// end: '2021-01-15T17:31:00+09:00',
// isReadOnly: true
// }
// opensky.scheduled_flights()
//trim string call sign to be certain number of characters, convert every flight json into a json array of "events" with ISO times,
//stringify this json array, and use calendar.ejs
//to populate calendar
//
// console.log(opensky.scheduled_flights.length);
// Record the information
//instead of sending blank html file with this response
// res.send("Your data has been recorded! You may now close this tab");
//send calendar
});
// localhost:8080/calendar, http://whatever.com/calendar
var user = "bob";
expressApp.get('/calendar', (req, res) => {
//static calendr without flight data
// res.sendFile(path.join(__dirname, '/views/calendar.html'));
let events = [];
let color = ['blue', 'red', 'green'];
for (let i = 0; i < scheduled_flights.length; i++) {
let event_template = {
id: '2',
calendarId: '1',
title: 'yo',
category: 'time',
dueDateClass: '',
start: '2021-01-21T17:30:00+09:00',
end: '2021-01-22T17:31:00+09:00',
isReadOnly: true, // schedule is read-only
bgColor: '#bbdc00'
}
event_template.start = new Date(scheduled_flights[i].departure * 1000).toISOString();
event_template.end = new Date(scheduled_flights[i].arrival * 1000).toISOString();
event_template.title = scheduled_flights[i].displayName + ' ' + scheduled_flights[i].arr_airport + '->' + ' ' + scheduled_flights[i].dep_airport;
event_template.bgColor = '#'+(Math.random()*0xFFFFFF<<0).toString(16);
events.push(event_template);
}
let event_string = JSON.stringify(events);
res.render('calendar', { event: event_string });
});
var server = expressApp.listen(config.port, function() {
framework.debug(`Framework listening on port ${config.port}`);
});
function exitHandler() {
framework.debug("Stopping...");
server.close();
framework.stop().then(function() {
process.exit();
});
}
process.on("SIGINT", exitHandler);
process.on("exit", exitHandler);