-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstates.js
420 lines (401 loc) · 14 KB
/
states.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
'use strict'
var prompts = require('./prompts')
//stateHandlers set user's state and return the user and the messageSend
var stateHandlers = {
//NOT_STARTED: welcome the user and introduce Pepper
0: function(user, messageReceived) {
//set state to 1, ask for content
user.state = 1
return {
user,
messageSend: prompts.WELCOME(user)
}
},
//ASK_TOPICS
1: function(user, messageReceived) { //
user.state = 2
// ASK TOPICS
return{
user,
messageSend: prompts.TOPICS
}
},
//ASK_CITIES
2: function(user, messageReceived){
if(messageReceived === "sports" || "news" || "tech"){
user.topic = messageReceived;
}
else if (messageReceived !== "sports" || "news" || "tech"){
user.state = 2;
return{
user,
messageSend: prompts.ERROR_TOPIC
}
}
user.state = 3;
return {
user,
messageSend: prompts.CITY(user)
}
},
//TIMETOWAKEUP_ASKING
3: function(user, messageReceived, data) {
if(data.entities.location === undefined){
user.state=3;
return { user, messageSend: prompts.CITY("Oops, looks like you didn't enter in the name of your state. ⚡", user)
}
} else {
user.state = 4
user.city = data.entities.location[0].value;
return {
user: user,
messageSend: prompts.TIMETOWAKEUP()
}
}
},
//SETUP_READY
4: function(user, messageReceived, data) {
user.state = 5
if(!data.entities.datetime){
user.state = 4;
return {user, messageSend: prompts.TIME_INCOMPLETE};
}
var dt = new Date(data.entities.datetime[0].values[0].value);
console.log("wit date data", data.entities.datetime[0].values[0].value)
user.timeToWakeUp.hour = dt.getHours();
var minutes = dt.getMinutes();
user.timeToWakeUp.minute = minutes;
if(minutes <10){
user.timeToWakeUp.minuteString = minutes === 0 ? "00" : minutes <10 ?"0"+ minutes.toString(): minutes.toString();
console.log("minute string", user.timeToWakeUp.minuteString);
console.log("reached minute string")
}
// if(dt.getHours() <= Math.abs(user.timezone)){
// console.log("hours reached")
// user.timeToWakeUp.hour = 24 + dt.getHours() + user.timezone;
// }
// else {
// user.timeToWakeUp.hour = dt.getHours() + user.timezone;
// }
user.timeToWakeUp.time = user.timeToWakeUp.hour+":"+user.timeToWakeUp.minuteString;
console.log(user.timeToWakeUp , "time above^^^", user.timeToWakeUp.minuteString)
return {
user,
messageSend: ["You're all set! Click the menu button to change preferences at any time. I'll be contacting you tomorrow morning at "+user.timeToWakeUp.time+ " to help you start your day."]
}
},
//START_MORNING
5: function(user, messageReceived, witData, content) {
user.state = 6
return {
user,
messageSend: prompts.START_MORNING(user, content)
} //send video here
},
// START_WORKING
6: function(user, messageReceived) {
user.state = 7
return {
user,
messageSend: prompts.START_WORKING
}
},
//HANDLING_START_WORKING
7: function(user, messageReceived) {
if (messageReceived === 'yes') {
// ASK_FOR_TASKS
user.state = 8
return {
user,
messageSend: prompts.ASK_FOR_TASKS
}
} else if (messageReceived === 'no') {
// NO_WORKING
user.state = 11
return {
user,
messageSend: prompts.NO_WORKING
}
}
},
// ADD_ANOTHER_TASK
8: function(user, messageReceived) {
user.tasks = user.tasks.concat(messageReceived);
console.log("user.tasks", user.tasks);
user.state = 9
return {
user: user,
messageSend: prompts.ADD_ANOTHER_TASK
}
},
//HANDLING_ADD_TASK
9: function(user, messageReceived) {
if (messageReceived === 'yes') {
user.state = 8
return {
user: user,
messageSend: prompts.ASK_FOR_TASKS
}
} else if (messageReceived === 'no') {
user.state = 10
return {
user,
messageSend: prompts.SHOW_TASKS
}
}
},
//SHOW_TASKS
10: function(user, messageReceived, witData, content) {
if (messageReceived === "Add another task"){
user.state = 8;
return {
user,
messageSend: prompts.ASK_FOR_TASKS
}
}
if (user.tasks.indexOf(messageReceived)> -1) {
var index = user.tasks.indexOf(messageReceived)
console.log("Reached")
user.tasks.splice(index, 1)
user.save(function(err) {console.log('err from saving routine', err)})
}
if (!user.tasks.length) {
console.log("REACHED YO")
user.state = 12
return {
user,
messageSend: prompts.DONE_WORKING(user, content)
}
}
return {
user,
messageSend: prompts.SHOW_TASKS
}
},
// NO_WORKING
11: function(user, messageReceived) {
user.state = 6
return {
user,
messageSend: ['Glad you are back!']
}
},
// DONE_WORKING
12: function(user, messageReceived) {
user.state = 13
return {
user,
messageSend: prompts.REFLECT_OPTION
}
},
// ASK_REFLECTION_QUESTIONS
13: function(user, messageReceived) {
if (messageReceived === 'yes') {
user.state = 14
//choose a random reflection question
user.reflectionQuestion = prompts.ASK_REFLECTION_QUESTIONS(user.reflectionState);
user.reflectionState++;
if(user.reflectionQuestion === undefined){
user.reflectionState = 0} // prevents question not in prompts array from being asked
}
else{
user.state=12;
user.prevState=10;
return {
user,
messageSend: ["Fine. You can reflect later if you'd like..."]
}
}
return {
user,
messageSend: ["Another day down. Take a second to reflect and your response will be saved and visualized",user.reflectionQuestion]
}
},
// SAVE_REFLECTION_QUESTION
14: function(user, messageReceived) {
var date = new Date();
// user.reflection.title.text.headline = user.reflectionQuestion;
// user.reflection.title.text.headline = messageReceived;
user.reflectionAnswer = messageReceived;
user.state = 15
return {
user,
messageSend: ['Upload a picture, selfie, or video from today and I\'ll stitch it into your reflection. Otherwise, reply "no"...']
}
},
// REFLECTION_PICTURE_QUESTION
15: function(user, messageReceived) {
var date = new Date();
user.state = 5; //changes state back to 5:morning routine
if (messageReceived.indexOf('https') < 0) {
messageReceived = 'https://source.unsplash.com/category/nature'
}
user.reflection.events.push(
{
//detect key words and display pictures
"start_date": {
"month": date.getMonth() + 1,
"day": date.getDay(),
"year": date.getYear()
},
"text": {
"headline": user.reflectionQuestion,
"text": user.reflectionAnswer
},
"media":{
"url": messageReceived
}
})
user.reflection.title.media.url = user.profile;
user.reflection.title.text.headline = user.firstname + "'s Memories";
return {
user,
messageSend: ["I've saved your reflection, thanks for sharing. Your information will be kept private", 'Check out your reflection timeline at https://pepper-chatbot.herokuapp.com/reflection/' + user._id, "I'll be in touch tomorrow!"]
}
},
// >= 100: EDIT STATES
// EDIT_WAKEUP_TIME
100:
function(user, messageReceived, data) {
user.state = user.prevState; //remembers where you were before menu
user.prevState = null;
console.log("WIT DATA", data)
// user.token = randomCode();
var dt = new Date(data.entities.datetime[0].values[0].value);
user.timeToWakeUp.minute = dt.getMinutes();
console.log("hour", dt.getHours())
if(dt.getHours() <= Math.abs(user.timezone)){
user.timeToWakeUp.hour = 24 + dt.getHours() + user.timezone;
console.log("user.timeToWakeUp.hour", user.timeToWakeUp.hour)
} else {
user.timeToWakeUp.hour = dt.getHours() + user.timezone;
}
user.timeToWakeUp.time = user.timeToWakeUp.hour+":"+user.timeToWakeUp.minute;
//reduce all the routines to a string
var CHANGE_TIME = prompts.CHANGE_TIME;
CHANGE_TIME += user.timeToWakeUp.time;
return {
user,
messageSend: [CHANGE_TIME]
}
},
// EDIT_CITY
101: function(user, messageReceived){
user.city = messageReceived
// user.state = user.prevState; //remembers where you were before menu
// user.prevState = 101;
user.state = user.prevState
user.prevState = null;
var CHANGE_CITY = prompts.CHANGE_CITY;
CHANGE_CITY += user.as;
return {
user,
messageSend: [prompts.CHANGE_CITY]
}
},
// EDIT_ROUTINES sendMultiButton creates a message with multiple buttons of the tasks/routines in the array
102: function(user, messageReceived) {
// user.state = user.prevState; //remembers where you were before menu
console.log("STATE", user.state, "PREVIOUS STATE", user.prevState)
if(messageReceived.indexOf("ADD_NEW_ROUTINE") !== -1 ){
user.state = 103
user.prevState = null;
return {
user,
messageSend: prompts.ADDROUTINE
}
} else if (messageReceived.indexOf("DELETE_ROUTINE") > -1){
var index = parseInt(messageReceived[messageReceived.length-1])
var deletedMessage = user.routine[index].routine;
user.routine.splice(index, 1)
// user.state = user.prevState
user.prevState = null;
return {
user,
messageSend: ["Your task "+ deletedMessage +" has been deleted"]
}
}
},
//add routine
// 103: function(user, messageReceived, data){
// console.log("USer", user)
// var newRoutine = {};
// if(user.missingDuration || user.missingRoutine){
// newRoutine = {
// routine: user.missingDuration, //filled in
// duration: user.missingRoutine //null
// }
// }
// console.log("New Routine", newRoutine)
// // if(data.entities.agenda_entry === undefined || data.entities.duration === undefined){
// // user.state = 2;
// // }
// if(data.entities.duration){ //new routine is checked because if one is saved and the other isn't then it must be
// newRoutine.duration = data.entities.duration[0].normalized.value/60 //to acount for seconds
// user.missingRoutine = data.entities.duration[0].normalized.value/60
// }
// if(data.entities.agenda_entry){
// newRoutine.routine = data.entities.agenda_entry[0].value; //to acount for seconds
// user.missingDuration = data.entities.agenda_entry[0].value;
// }
// if(newRoutine.routine === null || newRoutine.routine === undefined){ // data.entities.location === 'undefined'
// user.state = 2
// user.prevState = 1;
// return {user, messageSend: prompts.SETUP("Woops, either you forgot to include a routine or I didn't pick up on that?")}
// }
// if(newRoutine.duration === null || newRoutine.duration === undefined){
// user.state = 2
// user.prevState = 1;
// console.log("Duration undefined")
// return {user, messageSend: prompts.SETUP("For how long? 5 minutes, half an hour...?")}
// }
// else{
// user.routine.push(newRoutine)
// // user.state = user.prevState;
// user.prevState = null;
// console.log("ROUTINE", newRoutine)
// }
// // user.save();
// user.missingRoutine = null;
// user.missingDuration = null;
// return {
// user: user,
// messageSend: ["Your Routine " + newRoutine.routine + " for " + newRoutine.duration + " minutes has been added"]
// }
// },
// 104: function(user, messageReceived, data) {
// //to be changed to change frequency messages
// user.state = user.prevState; //remembers where you were before menu
// // user.prevState = null;
// var CHANGE_TIME = prompts.CHANGE_TIME;
// // CHANGE_TIME += user.timeToWakeUp;
// // user.timeToWakeUp.hour = new Date(data.entities.datetime[0].values[0].value).getHours();
// // user.timeToWakeUp.minute = new Date(data.entities.datetime[0].values[0].value).getMinutes();
// // user.timeToWakeUp.time = user.timeToWakeUp.hour+":"+user.timeToWakeUp.minute;
// return {
// user,
// messageSend: prompts.CHANGE_FREQ
// }
// },
105: function(user, messageReceived, data) {
//to be changed to change frequency messages
user.state = user.prevState; //remembers where you were before menu
user.prevState = null;
var dt = new Date(data.entities.datetime[0].values[0].value);
user.reflectionTime.minute = dt.getMinutes();
if(dt.getHours() <= Math.abs(user.timezone)){
user.reflectionTime.hour = 24 + dt.getHours() + user.timezone;
console.log("user.timeToWakeUp.hour", user.reflectionTime.hour)
} else {
user.reflectionTime.hour = dt.getHours() + user.timezone;
}
user.reflectionTime.time = user.reflectionTime.hour+":"+user.reflectionTime.minute;
var CHANGE_TIME_REFLECTION = prompts.CHANGE_TIME_REFLECTION[0];
CHANGE_TIME_REFLECTION += user.reflectionTime.time;
return {
user,
messageSend: [CHANGE_TIME_REFLECTION]
}
},
}
module.exports = stateHandlers;