/
startSessionFunctions.js
403 lines (334 loc) · 9.88 KB
/
startSessionFunctions.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
import moment from 'moment-timezone';
import models from '../../../app/models';
import { utterances, colorsArray, buttonValues, colorsHash, timeZones, timeZoneAttachments } from '../../lib/constants';
import { witTimeResponseToTimeZoneObject, convertMinutesToHoursString, getRandomExample, getSessionContentFromMessageObject } from '../../lib/messageHelpers';
/**
* START WORK SESSION CONVERSATION FLOW FUNCTIONS
*/
// confirm that user has tz configured before continuing
export function confirmTimeZoneExistsThenStartSessionFlow(convo, text = ``) {
const { SlackUserId, UserId, tz, content } = convo.sessionStart;
if (text == ``) {
text = `One more thing! Since I help you make time for your priorities, I need to know your *timezone* before we focus`
if (content) {
text = `${text} on \`${content}\``;
}
}
if (tz) { // user has tz config'd
finalizeSessionTimeAndContent(convo); // entry point
convo.next();
} else { // user needs tz config'd!
convo.ask({
text,
attachments: timeZoneAttachments
}, (response, convo) => {
const { text } = response;
let timeZoneObject = false;
switch (text) {
case (text.match(utterances.eastern) || {}).input:
timeZoneObject = timeZones.eastern;
break;
case (text.match(utterances.central) || {}).input:
timeZoneObject = timeZones.central;
break;
case (text.match(utterances.mountain) || {}).input:
timeZoneObject = timeZones.mountain;
break;
case (text.match(utterances.pacific) || {}).input:
timeZoneObject = timeZones.pacific;
break;
case (text.match(utterances.other) || {}).input:
timeZoneObject = timeZones.other;
break;
default:
break;
}
if (!timeZoneObject) {
convo.say("I didn't get that :thinking_face:");
confirmTimeZoneExistsThenStartSessionFlow(convo, `Which timezone are you in?`);
convo.next();
} else if (timeZoneObject == timeZones.other) {
convo.say(`Sorry!`);
convo.say("Right now I’m only able to work in these timezones. If you want to demo Toki, just pick one of these timezones for now. I’ll try to get your timezone included as soon as possible!");
confirmTimeZoneExistsThenStartSessionFlow(convo, `Which timezone do you want to go with for now?`);
convo.next();
} else { // success!!
const { tz } = timeZoneObject;
models.User.update({
tz
}, {
where: { id: UserId }
})
.then((user) => {
convo.say(`Great! If this ever changes, you can always \`update settings\``);
convo.sessionStart.tz = tz;
finalizeSessionTimeAndContent(convo); // entry point
convo.next();
});
}
});
}
}
// confirm task and time in one place and start if it's good
function finalizeSessionTimeAndContent(convo) {
const { SlackUserId, tz, content, minutes, currentSession, changeTimeAndTask } = convo.sessionStart;
if (currentSession) {
/*
* ONLY IF YOU'RE CURRENTLY IN A SESSION...
*/
if (changeTimeAndTask) {
// clicking button `change time + task`
changeTimeAndTaskFlow(convo);
} else {
// wit `new session`
askToOverrideCurrentSession(convo);
}
} else {
/*
* STANDARD FLOW
*/
// we need both time and task in order to start session
// if dont have either, will run function before `convo.next`
if (!content) {
askForSessionContent(convo);
convo.next();
return;
} else if (!minutes) {
askForSessionTime(convo);
convo.next();
return;
}
convo.say(" ");
convo.sessionStart.confirmNewSession = true;
convo.next();
}
convo.next();
}
function changeTimeAndTaskFlow(convo) {
const { SlackUserId, tz, currentSession } = convo.sessionStart;
// we are restarting data so user can seamlessly create a new session
convo.sessionStart.content = currentSession.dataValues.content;
convo.sessionStart.minutes = false;
convo.sessionStart.currentSession = false;
let text = `Would you like to work on something other than \`${convo.sessionStart.content}\`?`;
let attachments = [
{
attachment_type: 'default',
callback_id: "EXISTING_SESSION_OPTIONS",
fallback: "Hey, you're already in a session!!",
actions: [
{
name: buttonValues.yes.name,
text: "Yes",
value: buttonValues.yes.value,
type: "button"
},
{
name: buttonValues.no.name,
text: "Nah, keep it!",
value: buttonValues.no.value,
type: "button"
}
]
}
]
convo.ask({
text,
attachments
}, [
{
pattern: utterances.yes,
callback: (response, convo) => {
convo.sessionStart.content = false;
const sessionExample = getRandomExample("session");
let question = `What would you like to work on? (i.e. \`${sessionExample}\`)`;
askForSessionContent(convo, question);
convo.next();
}
},
{
pattern: utterances.no,
callback: (response, convo) => {
finalizeSessionTimeAndContent(convo);
convo.next();
}
},
{
default: true,
callback: (response, convo) => {
convo.say("Sorry, I didn't catch that");
convo.repeat();
convo.next();
}
}
]);
}
function askToOverrideCurrentSession(convo) {
const { SlackUserId, tz, content, minutes, currentSession } = convo.sessionStart;
let now = moment().tz(tz);
let endTime = moment(currentSession.dataValues.endTime).tz(tz);
let endTimeString = endTime.format("h:mma");
let minutesLeft = Math.round(moment.duration(endTime.diff(now)).asMinutes());
let text = `Hey! You're already working on \`${currentSession.dataValues.content}\` until *${endTimeString}*`;
let attachments = [
{
attachment_type: 'default',
callback_id: "EXISTING_SESSION_OPTIONS",
fallback: "Hey, you're already in a session!!",
actions: [
{
name: buttonValues.newSession.name,
text: "Do something new :new:",
value: `{"overrideNewSession": true}`,
type: "button"
}
]
}
];
convo.say({
text,
attachments
});
convo.next();
// convo.ask({
// text,
// attachments
// }, [
// {
// pattern: utterances.containsNew,
// callback: (response, convo) => {
// convo.say(`Okay, sounds good to me!`);
// // restart everything!
// convo.sessionStart.minutes = false;
// convo.sessionStart.content = false;
// convo.sessionStart.currentSession = false;
// finalizeSessionTimeAndContent(convo);
// convo.next();
// }
// },
// {
// pattern: utterances.containsKeep,
// callback: (response, convo) => {
// convo.say(`You got this! Keep working on \`${currentSession.dataValues.content}\` and I’ll see you at *${endTimeString}*`);
// convo.next();
// }
// },
// {
// default: true,
// callback: (response, convo) => {
// convo.say("Sorry, I didn't catch that");
// convo.repeat();
// convo.next();
// }
// }
// ]);
}
/**
* CHOOSE SESSION TASK
*/
// ask which task the user wants to work on
function askForSessionContent(convo, question = '') {
const { SlackUserId, tz, content, minutes } = convo.sessionStart;
const sessionExample = getRandomExample("session");
if (question == '')
question = `What would you like to work on? (i.e. \`${sessionExample}\`)`;
convo.ask({
text: question,
},[
{
pattern: utterances.noAndNeverMind,
callback: (response, convo) => {
convo.say(`Okay! Let me know when you want to \`/focus\` on something`);
convo.next();
}
},
{
default: true,
callback: (response, convo) => {
const { text, intentObject: { entities: { reminder, duration, datetime } } } = response;
let content = getSessionContentFromMessageObject(response);
if (content) {
// optionally accept time here
let customTimeObject = witTimeResponseToTimeZoneObject(response, tz);
if (customTimeObject) {
let now = moment().tz(tz);
let minutes = Math.round(moment.duration(customTimeObject.diff(now)).asMinutes());
convo.sessionStart.minutes = minutes;
convo.next();
}
convo.sessionStart.content = content;
finalizeSessionTimeAndContent(convo);
} else {
convo.say(`I didn't get that :thinking_face:`);
convo.repeat();
}
convo.next();
}
}
]);
convo.next();
}
function askForSessionTime(convo) {
const { task } = convo;
const { bot, source_message } = task;
const { SlackUserId, tz, content, minutes } = convo.sessionStart;
// get time to session
convo.ask({
text: `How long would you like to work on \`${content}\`?`,
attachments:[
{
attachment_type: 'default',
callback_id: "START_SESSION",
fallback: "How long do you want to work on this?",
color: colorsHash.grey.hex,
actions: [
{
name: buttonValues.changeTasks.name,
text: "Wait, change task",
value: buttonValues.changeTasks.value,
type: "button"
}
]
}
]
}, [
{
pattern: utterances.containsChange,
callback: (response, convo) => {
convo.say(`Okay, let's change tasks!`);
convo.sessionStart.content = false;
finalizeSessionTimeAndContent(convo);;
convo.next();
}
},
{
pattern: utterances.noAndNeverMind,
callback: (response, convo) => {
convo.say(`Okay, let me know when to \`/focus\`!`);
convo.next();
}
},
{
default: true,
callback: (response, convo) => {
let now = moment().tz(tz);
let { intentObject: { entities } } = response;
// for time to tasks, these wit intents are the only ones that makes sense
let customTimeObject = witTimeResponseToTimeZoneObject(response, tz);
if (customTimeObject) {
let minutes = Math.round(moment.duration(customTimeObject.diff(now)).asMinutes());
convo.sessionStart.minutes = minutes;
finalizeSessionTimeAndContent(convo);
convo.next();
} else {
// invalid
convo.say("I'm sorry, I didn't catch that :dog:");
let question = `How much more time did you want to add to \`${content}\` today?`;
convo.repeat();
}
convo.next();
}
}
]);
convo.next();
}