-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
230 lines (196 loc) · 5.76 KB
/
server.ts
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
import * as Hapi from "@hapi/hapi";
import * as B from "bluebird";
import {
addDays,
format,
formatISO,
getTime,
subHours,
isPast,
} from "date-fns";
import * as Twilio from "twilio";
import * as crypto from "crypto";
const readFileAsync: (
name: string,
encoding: string
// eslint-disable-next-line @typescript-eslint/no-var-requires
) => Promise<any> = B.promisify(require("fs").readFile);
const writeFileAsync: (
name: string,
contents: string,
encoding: string
// eslint-disable-next-line @typescript-eslint/no-var-requires
) => Promise<any> = B.promisify(require("fs").writeFile);
interface TwilioBody {
From: string;
SmsMessageSid: string;
NumMedia: string;
ToCity: string;
FromZip: string;
SmsSid: string;
FromState: string;
SmsStatus: string;
FromCity: string;
Body: string;
FromCountry: string;
To: string;
ToZip: string;
NumSegments: string;
MessageSid: string;
AccountSid: string;
ApiVersion: string;
}
interface Subscription {
number: string;
expiration: string;
}
const STOP_STRINGS = [
"STOP",
"STOPALL",
"UNSUBSCRIBE",
"CANCEL",
"END",
"QUIT",
];
const denverTzOffset = 7;
const fileName = "./numbers.json";
const analyticsDirectory = "./analytics";
function readJSONFile(): Promise<Subscription[] | null> {
return readFileAsync(fileName, "utf8")
.then(JSON.parse)
.catch(() => {
return null;
});
}
function writeToJSONFile(updatedSubscriptions: Subscription[]): Promise<any> {
return writeFileAsync(fileName, JSON.stringify(updatedSubscriptions), "utf8");
}
function unsubscribeNumber(number: string, users?: Subscription[]) {
if (!users) return;
const filteredSubscriptions = users.filter((u) => u.number !== number);
return writeToJSONFile(filteredSubscriptions);
}
function saveAnalytics(
number: string,
action: string,
payload: any
): Promise<any> {
const hashedNumber = crypto.createHash("md5").update(number).digest("hex");
const time = getTime(new Date());
const fileName = `${analyticsDirectory}/${time}.json`;
const contents = JSON.stringify({
subscriptionHash: hashedNumber,
action,
time,
payload: payload || null,
});
return writeFileAsync(fileName, contents, "utf8");
}
async function sendMessage(
h,
number: string,
sub?: Subscription,
unsub = false,
count?: number
) {
const twiml = new Twilio.twiml.MessagingResponse();
if (sub) {
const formattedExpiration = format(
subHours(new Date(sub.expiration), denverTzOffset),
"M/d 'at' h:mm aaaa"
);
const message = `You are already signed up until ${formattedExpiration}, we will notify you with any road closures.`;
twiml.message(message);
await saveAnalytics(number, "DUPLICATE_SIGNUP", { message });
} else if (unsub) {
const message =
'You have been unsubscribed from road closure notifications. If you would like to join again reply with "START"';
twiml.message(message);
await saveAnalytics(number, "UNSUBSCRIBE", { message });
} else {
const message =
"You are all set, we will keep you up to date on any road closures for the next day. Check existing conditions here: cotrip.org/travelAlerts.htm";
twiml.message(message);
await saveAnalytics(number, "SIGNUP", { message, count });
}
const response = h.response(twiml.toString());
response.type("text/xml");
return response;
}
const init = async () => {
const server = Hapi.server({
port: 3000,
host: "localhost",
});
server.route({
method: "POST",
path: "/register",
handler: async (request, h) => {
const body: TwilioBody = request.payload;
const now = new Date();
const nowISO = formatISO(now);
const expiration = addDays(now, 1).toISOString();
// check to see if user is registered
const users = await readJSONFile();
// if the user wants to stop, remove them from the file if it exists
if (STOP_STRINGS.includes(body.Body.toUpperCase())) {
await unsubscribeNumber(body.From, users);
return sendMessage(h, body.From, undefined, true);
}
if (!users) {
console.log("creating users file");
await writeToJSONFile([
{
number: body.From,
expiration,
},
]);
return sendMessage(h, body.From, undefined, false, 1);
}
const existingSubscription = users.find((u) => u.number === body.From);
// if user is registered, let them know
if (!existingSubscription) {
console.log(
`${nowISO}: adding user until ${expiration} - total: ${
users.length + 1
}`
);
await writeToJSONFile([
...users,
{
number: body.From,
expiration,
},
]);
return sendMessage(h, body.From, undefined, false, users.length + 1);
}
// if the expiration date has already passed, re sign them up
const expirationDate = new Date(existingSubscription.expiration);
if (isPast(expirationDate)) {
console.log(
`${nowISO}: renewing user until ${expiration} - total: ${users.length}`
);
const updatedSubscriptions = users.filter((subscription) => {
subscription.number !== body.From;
});
await writeToJSONFile([
...updatedSubscriptions,
{
number: body.From,
expiration,
},
]);
return sendMessage(h, body.From, undefined, false, users.length);
}
// if user is not registered, add them and let them know
return sendMessage(h, body.From, existingSubscription);
},
});
await server.start();
console.log("Server running on %s", server.info.uri);
};
process.on("unhandledRejection", (err) => {
console.log(err);
process.exit(1);
});
init();