/
index.js
135 lines (124 loc) · 6.24 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
"use strict";
var Alexa = require("alexa-sdk");
var request = require("request");
var config = require("./config.js");
exports.handler = function(event, context, callback) {
console.log("In handler");
var alexa = Alexa.handler(event, context);
alexa.appId = config.appId;
alexa.registerHandlers(handlers);
alexa.execute();
};
var handlers = {
"AMAZON.HelpIntent": function () {
this.emit(':tellWithCard',
"Ask where Andrew is or ask how many times he has checked in",
"Mongo – Where's Andrew",
"Ask where Andrew is or ask how many times he has checked in",
{
smallImageUrl: "https://media.licdn.com/mpr/mpr/shrinknp_200_200/AAEAAQAAAAAAAALQAAAAJDhkZWUxZDQxLTM0NjctNDcxZS04NmJiLTA1YzRhNGVlNWY0ZQ.jpg",
largeImageUrl: "https://media.licdn.com/mpr/mpr/shrinknp_200_200/AAEAAQAAAAAAAALQAAAAJDhkZWUxZDQxLTM0NjctNDcxZS04NmJiLTA1YzRhNGVlNWY0ZQ.jpg"
}
)},
"CountIntent": function () {
var countURL = config.mongopopAPI + "checkInCount";
var _this = this;
request({url: countURL, json: true}, function (error, response, body) {
if (error || response.statusCode != 200) {
console.log("Failed to count checkins: " + error.message);
_this.emit(':tellWithCard',
"Network error, check Alexa app for details",
"Mongo – Error",
"Network error: " + error.message,
{
smallImageUrl: "https://cdn3.iconfinder.com/data/icons/wifi-2/460/connection-error-512.png",
largeImageUrl: "https://cdn3.iconfinder.com/data/icons/wifi-2/460/connection-error-512.png"
}
)
} else {
if (body.success) {
var successString = "Andrew has checked in " + body.count + " times.";
_this.emit(':tellWithCard',
successString,
"Mongo – Where's Andrew",
successString,
{
smallImageUrl: "https://media.licdn.com/mpr/mpr/shrinknp_200_200/AAEAAQAAAAAAAALQAAAAJDhkZWUxZDQxLTM0NjctNDcxZS04NmJiLTA1YzRhNGVlNWY0ZQ.jpg",
largeImageUrl: "https://media.licdn.com/mpr/mpr/shrinknp_200_200/AAEAAQAAAAAAAALQAAAAJDhkZWUxZDQxLTM0NjctNDcxZS04NmJiLTA1YzRhNGVlNWY0ZQ.jpg"
}
)
} else {
console.log("Failed to count checkins: " + body.error);
_this.emit(':tellWithCard',
"Application error, check Alexa app for details",
"Mongo – Error",
"Application error: " + body.error,
{
smallImageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Sad_face.gif/1024px-Sad_face.gif",
largeImageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Sad_face.gif/1024px-Sad_face.gif"
}
)
}
}
})
},
"WhereIntent": function () {
console.log("In WhereIntent");
var latestCheckinURL = config.mongopopAPI + "latestCheckin";
var _this = this;
request({url: latestCheckinURL, json: true}, function (error, response, body) {
console.log("in callback");
if (error || response.statusCode != 200) {
console.log("Failed to fetch latest Checkin, network problem: " + error.message);
_this.emit(':tellWithCard',
"Network error, check Alexa app for details",
"Mongo – Error",
"Network error: " + error.message,
{
smallImageUrl: "https://cdn3.iconfinder.com/data/icons/wifi-2/460/connection-error-512.png",
largeImageUrl: "https://cdn3.iconfinder.com/data/icons/wifi-2/460/connection-error-512.png"
}
)
} else {
if (body.success) {
var successString = "Andrew last checked in to " + body.venue + " on " + body.date;
var imgURL = body.location.replace("http", "https");
_this.emit(':tellWithCard',
successString,
"Mongo – Where's Andrew",
successString + ". Location URL: " + body.url + ". View venue: " + body.location + ".",
{
/*
smallImageUrl: "https://lh3.googleusercontent.com/81tvpT59weJbOGWT9jQ8_9RtcGXKCcVv59BU7Wl6PnS7okIgrS4iTCgwWpPQY2FRKw=w300",
largeImageUrl: "https://lh3.googleusercontent.com/81tvpT59weJbOGWT9jQ8_9RtcGXKCcVv59BU7Wl6PnS7okIgrS4iTCgwWpPQY2FRKw=w300" */
smallImageUrl: imgURL,
largeImageUrl: imgURL
}
)
} else {
console.log("Failed to fetch latest checkin, app error: " + body.error);
_this.emit(':tellWithCard',
"Application error, check Alexa app for details",
"Mongo – Error",
"Application error: " + body.error,
{
smallImageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Sad_face.gif/1024px-Sad_face.gif",
largeImageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Sad_face.gif/1024px-Sad_face.gif"
}
)
}
}
})
},
"Unhandled": function () {
this.emit(':tellWithCard',
"Unhandled event",
"Mongo – Error",
"Unhandled event",
{
smallImageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Sad_face.gif/1024px-Sad_face.gif",
largeImageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Sad_face.gif/1024px-Sad_face.gif"
}
)
}
};