-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb.js
336 lines (270 loc) · 10.1 KB
/
web.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//See https://github.com/cooperhewitt/objectphone
var express = require("express"),
twilio = require("twilio"),
rest = require("restler");
var app = express();
var apikey = process.env.APIKEY;
app.get('/', function(request, response) {
var twilioResponse = new twilio.TwimlResponse();
twilioResponse.say("Welcome to art phone.")
.gather({
action: "initial-handler",
method: "GET",
numDigits: 1
}, function() {
this.say("Press one on your phone to search the Harvard Art Museums collection by object ID.")
.say("Press two for a random object.")
.say("Or, press three to learn more about art phone.");
});
twilioResponse.say("I'm sorry, I missed that, please try again.");
twilioResponse.redirect("/", {method: "GET"});
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
});
app.get('/initial-handler', function(request, response) {
var digits = request.query.Digits;
var twilioResponse = new twilio.TwimlResponse();
if (digits == 1) {
twilioResponse.gather({
action: "/object",
method: "GET",
timeout: 10
}, function() {
this.say("Ok. Enter an object ID followed by the pound key and we will see what we can do.");
});
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
}
if (digits == 2) {
twilioResponse.say("Fetching a random object. Please hold while we dig through the crates.")
.redirect("/random", {method: "GET"});
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
}
if (digits == 3) {
twilioResponse.say("Art phone is a research and development project of the Harvard Art Museums.")
.say("It is an experiment in data accessibility and malleability.")
.say("It is based on the Cooper Hewitt Museum's object phone project.")
.pause({length: 1})
.redirect("/", {method: "GET"});
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
}
twilioResponse.say("I missed that. Please try again.")
.redirect("/");
response.end(twilioResponse.toString());
});
app.get('/object-action-handler', function(request, response) {
var objectid = request.query.objectid;
var apiQuery = "https://api.harvardartmuseums.org/object/" + objectid + "?apikey=" + apikey;
var twilioResponse = new twilio.TwimlResponse();
rest.get(apiQuery)
.on("complete", function(data) {
if (data) {
twilioResponse.sms("I am a " + data.worktypes[0].worktype + ". " +
"My title is " + data.title + ". " +
"Visit me at " + data.url + ".")
.redirect("/", {method: "GET"});
} else {
//do nothing
}
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
})
.on("error", function(error) {
// twilioResponse.sms("Something went wrong. Please try again.");
// response.setHeader("Content-Type", "text/xml");
// response.end(twilioResponse.toString());
});
});
app.get('/object', function(request, response) {
var digits = request.query.Digits;
var twilioResponse = new twilio.TwimlResponse();
rest.get("https://api.harvardartmuseums.org/object/" + digits + "?apikey=" + apikey)
.on("complete", function(data) {
if (data.objectid) {
twilioResponse.say("We found something for you.")
.say("I am a " + data.worktypes[0].worktype + ".")
.say("My title is " + data.title + ".")
.redirect("/", {method: "GET"});
} else {
twilioResponse.say("I'm sorry, we couldn't find that object. Please try again.")
.redirect("/", {method: "GET"});
}
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
})
.on("error", function(error) {
twilioResponse.say("Something went wrong. Please try again.")
.redirect("/");
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
});
});
app.get('/random', function(request, response) {
var twilioResponse = new twilio.TwimlResponse();
rest.get("https://api.harvardartmuseums.org/object?s=random&size=1&q=title:*&apikey=" + apikey)
.on("complete", function(data) {
var slowObjectID = data.records[0].objectid.toString().replace(/\B(?=(\d{1})+(?!\d))/g, ", ");
twilioResponse.say("We found something for you.")
.say("I am a " + data.records[0].worktypes[0].worktype + ".")
.say("My title is " + data.records[0].title + ".")
.pause({length: 1})
.say("For future reference my ID number is, " + slowObjectID + ".")
.gather({
action: "object-action-handler?objectid=" + data.records[0].objectid,
method: "GET",
numDigits: 1
}, function() {
this.say("Press one on your phone to receive a text message containing this information.")
.say("or, stay on the line to start over.");
})
.redirect("/", {method: "GET"});
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
})
.on("error", function(error) {
twilioResponse.say("Something went wrong. Please try again.")
.redirect("/");
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
});
});
app.get('/sms', function(request, response) {
var digits = request.query.Body || "";
switch (digits.trim().toLowerCase()) {
case "random":
sendSMSRandomObject(request, response);
break;
case "random person":
sendSMSRandomPerson(request, response);
break;
case "about":
sendSMSAbout(request, response);
break;
default:
sendSMSSpecificObject(request, response);
}
});
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});
function sendSMSAbout(request, response) {
var twilioResponse = new twilio.TwimlResponse();
twilioResponse.message(function() {
this.body("ARTphone is a R&D project of the Harvard Art Museums.")
.body("It is an experiment in data accessibility and malleability.")
.body("It is based on the Cooper-Hewitt Museum's object phone project.");
});
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
}
function sendSMSRandomObject(request, response) {
var apiQuery = "https://api.harvardartmuseums.org/object?s=random&size=1&q=title:*&apikey=" + apikey;
var twilioResponse = new twilio.TwimlResponse();
rest.get(apiQuery)
.on("complete", function(data) {
if (data) {
data = data.records ? data.records[0] : data;
var d = new Date();
var daysSinceLastAccess = (d - new Date(data.dateoflastpageview))/(1000 * 60 * 60 * 24);
var linkMessage = "";
var imageURL = "";
if (daysSinceLastAccess > 45) {
linkMessage = "I haven't been viewed in quite some time. Come visit at " + data.url + ".";
} else {
linkMessage = "Get my whole story at " + data.url + ".";
}
if (data.primaryimageurl) {
if (data.imagepermissionlevel == 0) {
imageURL = "http://ids.lib.harvard.edu/ids/view/" + data.images[0].idsid + "?width=500&height=500";
}
}
twilioResponse.message(function() {
this.body("I am a " + data.worktypes[0].worktype + ".")
.body("My title is " + data.title + ".")
.body(linkMessage)
.media(imageURL);
});
} else {
//do nothing
}
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
})
.on("error", function(error) {
twilioResponse.sms("Something went wrong. Please try again.");
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
});
}
function sendSMSRandomPerson(request, response) {
var apiQuery = "https://api.harvardartmuseums.org/person?s=random&size=1&q=displayname:*&apikey=" + apikey;
var twilioResponse = new twilio.TwimlResponse();
rest.get(apiQuery)
.on("complete", function(data) {
if (data) {
data = data.records ? data.records[0] : data;
var linkMessage = "";
if (data.objectcount === 1) {
linkMessage = "I'm associated with 1 work of art in the collection. Check it out at " + data.url + ".";
} else if (data.objectcount > 1) {
linkMessage = "I'm associated with " + data.objectcount + " works of art in the collection. Check them out at " + data.url + ".";
}
twilioResponse.message(function() {
this.body("Hi. I am " + data.displayname + ".")
.body(linkMessage);
});
} else {
//do nothing
}
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
})
.on("error", function(error) {
twilioResponse.sms("Something went wrong. Please try again.");
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
});
}
function sendSMSSpecificObject(request, response) {
var digits = request.query.Body;
var apiQuery = "https://api.harvardartmuseums.org/object/" + digits.trim() + "?apikey=" + apikey;
var twilioResponse = new twilio.TwimlResponse();
rest.get(apiQuery)
.on("complete", function(data) {
if (data.objectid) {
data = data.records ? data.records[0] : data;
var d = new Date();
var daysSinceLastAccess = (d - new Date(data.dateoflastpageview))/(1000 * 60 * 60 * 24);
var linkMessage = "";
var imageURL = "";
if (daysSinceLastAccess > 45) {
linkMessage = "I haven't been viewed in quite some time. Come visit at " + data.url + ".";
} else {
linkMessage = "Get my whole story at " + data.url + ".";
}
if (data.primaryimageurl) {
if (data.imagepermissionlevel == 0) {
imageURL = "https://ids.lib.harvard.edu/ids/view/" + data.images[0].idsid + "?width=500&height=500";
}
}
twilioResponse.message(function() {
this.body("I am a " + data.worktypes[0].worktype + ".")
.body("My title is " + data.title + ".")
.body(linkMessage)
.media(imageURL);
});
} else {
twilioResponse.message(digits + " doesn't mean anything to me. Please try again.");
}
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
})
.on("error", function(error) {
twilioResponse.sms("Something went wrong. Please try again.");
response.setHeader("Content-Type", "text/xml");
response.end(twilioResponse.toString());
});
}