-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
226 lines (189 loc) · 6.03 KB
/
index.js
File metadata and controls
226 lines (189 loc) · 6.03 KB
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
const postbacks = require('./src/postbacks');
const andybot = require('./src/andybot');
const activities = require('./src/activities.json');
const TriviaHandler = require('./src/trivia');
const ScavengerHuntHandler = require('./src/scavengerhunt');
const utils = require('./utils');
const config = require('./src/config');
const activityHandlers = {
trivia: TriviaHandler,
scavengerhunt: ScavengerHuntHandler
};
const _ = require('lodash');
const handleScan = require('./src/scan');
let eventHandlers = {};
let getStarted;
module.exports = function (bp) {
function isValidActivityType(activityType) {
const validActivityTypes = ['trivia', 'scavengerhunt'];
return validActivityTypes.indexOf(activityType);
};
function getActivityType(activityId) {
if (activityId.indexOf('-') > 0) {
const split = activityId.split('-');
return split[0];
}
else {
return activityId;
}
};
async function checkForUser(event) {
try {
const pageId = event.user.id;
const exists = await andybot.userExists(pageId);
if (exists === false) {
event.reply('#welcome');
andybot.createUser(pageId, event.user.first_name);
}
} catch (err){
console.error(err);
event.reply('#error');
}
}
// Catch get started button press
// As well as any codes scanned
async function getStarted(event, next) {
try {
const pageId = event.user.id;
const exists = await andybot.userExists(pageId);
if (exists === false) {
event.reply('#welcome');
andybot.createUser(pageId, event.user.first_name);
} else {
const user = await andybot.getUser(pageId);
event.reply('#welcome-back', { user });
}
// Get Started may have an associated event.
let referral;
if (utils.isNonNull(event.raw.postback.referral) && utils.isNonNull(event.raw.postback.referral.ref)) {
console.log("SCANNED CODE BEFORE GETTING STARTED:")
console.log(event.raw.postback.referral.ref);
referral = event.raw.postback.referral.ref;
await handleScan(referral, event);
}
} catch (err){
console.error(err);
event.reply('#error');
}
}
// Catch scanned codes
async function fallBackHandler(event, next) {
// console.log(event.raw.referral);
await checkForUser(event);
// console.log("Checked for user")
if (utils.isNonNull(event.raw.referral) && utils.isNonNull(event.raw.referral.ref)) {
// A code was scanned
// console.log("SCANNED CODE:")
// console.log(event.raw.referral.ref);
await handleScan(event.raw.referral.ref, event)
}
}
async function howToPlay(event, next){
await checkForUser(event);
if (bp.convo.find(event)) {
await stopConvo(event, null, false);
}
const howtoplay = activities['howtoplay'];
event.reply('#how-to-play', { howtoplay });
return;
}
async function clearHunt(event, next) {
await checkForUser(event);
const pageId = event.user.id;
const cleared = await andybot.scavengerhunt.clearProgress(pageId);
event.reply("#scavengerhunt-clearhunt");
try {
activityHandlers.scavengerhunt(null, event, null);
} catch (err) {
console.error(err);
}
}
async function sendScavengerHuntHint(event, next) {
await checkForUser(event);
const clueNumber = event.raw.postback.payload.split(':')[1];
const hintResponse = await andybot.scavengerhunt.getHint(clueNumber);
event.reply("#scavengerhunt-hint", { hint: hintResponse.hint });
return;
}
async function startActivity(event, next) {
await checkForUser(event);
const activityName = event.raw.postback.payload.split(':')[1];
const activityType = getActivityType(activityName);
if (isValidActivityType(activityType) === false) {
event.reply('#error');
}
if (bp.convo.find(event)) {
await stopConvo(event, null, false);
}
const convo = bp.convo.create(event);
try {
activityHandlers[activityType](convo, event, activityName);
} catch (err) {
console.error(err);
}
}
async function seeEvents(event) {
await checkForUser(event);
if (bp.convo.find(event)) {
await stopConvo(event, null, false);
}
const avaliableEvents = await andybot.avaliableEvents(event.user.id);
const payload = {
template_type: "generic",
elements: _.map(avaliableEvents.slice(0,9), (ele) => {
const imageName = _.find(activities['schedule'], (scheduleObj) => {
return scheduleObj.id === ele.eventId
}).image;
return {
title: ele.title,
subtitle: ele.subtitle,
image_url: `${config.staticUrl}${imageName}`,
buttons: [
{
type: "web_url",
url: ele.link,
title: "Details"
}
]
}
})
}
bp.messenger.sendTemplate(event.user.id, payload, { typing: 2000 })
}
async function stopConvo(event, next, sendNotification){
await checkForUser(event);
const convo = bp.convo.find(event);
if (convo) {
convo.stop('aborted');
if (sendNotification === true) {
event.reply('#activity_ended');
const avaliableActivities = await andybot.avaliableActivities(event.user.id);
event.reply('#activities', { activities: _.shuffle(avaliableActivities).slice(0, 9) });
}
} else {
event.reply('#no_activity');
}
}
async function beginAdventure(event, next) {
await checkForUser(event);
const avaliableActivities = await andybot.avaliableActivities(event.user.id);
event.reply('#activities', { activities: _.shuffle(avaliableActivities).slice(0, 9) });
}
bp.fallbackHandler = fallBackHandler;
bp.hear(/GET_STARTED/i, getStarted);
bp.hear(/START_ACTIVITY:/, startActivity);
bp.hear(/SEE_EVENTS/, seeEvents);
bp.hear(/SCAVENGER_HUNT_HINT:/, sendScavengerHuntHint);
bp.hear(/HOW_TO_PLAY/, howToPlay);
bp.hear(/BEGIN_ADVENTURE/, beginAdventure);
bp.hear(/CLEAR_HUNT/, clearHunt);
bp.hear(/STOP_CONVO/, (event, next) => { stopConvo(event, next, true) });
eventHandlers.fallBackHandler = fallBackHandler;
eventHandlers.getStarted = getStarted;
eventHandlers.clearHunt = clearHunt;
eventHandlers.howToPlay = howToPlay;
eventHandlers.stopConvo = stopConvo;
eventHandlers.startActivity = startActivity;
eventHandlers.sendScavengerHuntHint = sendScavengerHuntHint;
};
module.exports.eventHandlers = eventHandlers;