-
Notifications
You must be signed in to change notification settings - Fork 9
/
checkin.js
333 lines (318 loc) · 10.3 KB
/
checkin.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
import { getInfoForUser, recordMeeting, initBot } from '../utils'
import { parseDate } from 'chrono-node'
import { sample } from 'lodash'
const interactionCheckin = (bot = initBot(), message) => {
bot.startPrivateConversation(message, (err, convo) => {
if (err) {
console.error(err)
}
convo.sayFirst(`Give me a second while I pull up my notes`)
convo.next()
convo.addMessage(
{
text: `Give me a second while I pull up my notes`,
},
'start'
)
convo.addMessage(
{
delay: 1000,
text: `_typewriter noises_`,
},
'start'
)
convo.gotoThread('start')
getInfoForUser(message.user).then(
({ leader, club, history, slackUser }) => {
if (!leader || !club) {
convo.say({
delay: 2000,
text: `I don't have any record of you being a club leader (ಠ_ಠ)`,
})
convo.stop()
return
}
convo.addMessage(
{
delay: 2000,
text: `Found you! It's *${leader.fields['Full Name']}*, right?`,
},
'found'
)
convo.addMessage(
{
delay: 2000,
text: `From *${club.fields['Name']}*`,
action: 'date',
},
'found'
)
convo.addMessage(
{
delay: 1500,
text: 'You can see your meeting history with the `/stats` command',
},
'done'
)
convo.addMessage(
{
delay: 2000,
text: sample([
'done!',
'finished!',
'pleasure doing business with you!',
'See ya!',
'cya!',
'aloha!',
'bye!',
'bye bye!',
'come back soon!',
'until next time!',
'adios!',
]),
},
'done'
)
convo.addMessage(
{
delay: 2000,
text:
'Ok, just to confirm...\n> Attendance: *{{vars.attendance}} hackers*\n> Meeting date: *{{vars.date.dayName}} ({{{vars.date.mmddyyyy}}})*',
},
'confirm'
)
convo.addQuestion(
{
text: 'Is this correct?',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text:
"Just to double check, this is what I'm about to submit to your club history record",
},
},
{
type: 'actions',
elements: [
{
type: 'button',
text: {
type: 'plain_text',
text: '✅ submit',
emoji: true,
},
value: 'submit',
},
{
type: 'button',
text: {
type: 'plain_text',
text: '↩️ restart',
emoji: true,
},
value: 'restart',
},
{
type: 'button',
text: {
type: 'plain_text',
text: '⛔️ cancel',
emoji: true,
},
value: 'cancel',
},
],
},
],
},
[
{
pattern: 'submit',
callback: (response, convo) => {
console.log('*user submitted their checkin!*')
const reply = sample([
'grins with delight',
'fidgets in mild excitement',
'fumbles around with her large stubby arms for a pencil and paper',
'sifts through the Permian layer to find her notes',
])
bot.replyInteractive(
response,
`_✅ You confirm everything is accurate as orpheus ${reply}._`
)
convo.say("I'll write it in my notepad...")
const { date, attendance } = convo.vars
recordMeeting(
club,
{ date: date.mmddyyyy, attendance },
(err, meetingRecord) => {
if (err) {
convo.say({
text: `:warning: looks like something isn't working. All it says on my end is \`${err}\`.`,
action: 'done',
})
} else {
convo.say({
text: 'Got it recorded',
action: 'done',
})
}
convo.next()
}
)
},
},
{
pattern: 'restart',
callback: (response, convo) => {
console.log('*user wants to restart their checkin*')
bot.replyInteractive(
response,
'_↩️ You ask orpheus to start again_'
)
convo.gotoThread('found')
convo.vars = {}
convo.next()
},
},
{
pattern: 'cancel',
callback: (response, convo) => {
console.log('*user clicked "cancel"*')
const reply = sample([
'She looks slightly crestfallen',
'She promptly tears up the paper on her desk and eats it',
'She tosses the notes on her desk into her mouth and starts chewing',
'*VRRRRR* She raises her arm and swipes all the papers off the table into the hungry paper shredder lying by her table.',
])
bot.replyInteractive(
response,
`_⛔ You ask orpheus to cancel the checkin. ${reply}._`
)
convo.gotoThread('done')
},
},
],
{},
'confirm'
)
convo.addMessage('What day was your meeting on?', 'date')
let dateSuggestions = ['Today']
if (history.meetings.length > 0) {
const lastMeetingDay = new Date(
history.meetings[0].fields['Date']
).toLocaleDateString('en-us', {
weekday: 'long',
timeZone: slackUser.tz,
})
if (lastMeetingDay) dateSuggestions.push(`last ${lastMeetingDay}`)
}
convo.addQuestion(
{
text: 'When was your meeting?',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text:
'(You can tell me a date in `YYYY-MM-DD` format, say things like `last tuesday`, or click a shortcut button',
},
},
{
type: 'actions',
elements: dateSuggestions.map(suggestion => ({
type: 'button',
text: {
type: 'plain_text',
text: suggestion,
},
value: suggestion,
})),
},
],
},
[
{
default: true,
callback: (response, convo) => {
console.log('*User responded to attendance question*')
// attempt to parse
const meetingDate = parseDate(
`${response.text} ${slackUser.tz}`
)
if (
Object.prototype.toString.call(meetingDate) ===
'[object Date]'
) {
bot.replyInteractive(
response,
`_You tell orpheus you met ${response.text}_`
)
convo.setVar('date', {
full: meetingDate,
dayName: meetingDate.toLocaleDateString('en-us', {
weekday: 'long',
timeZone: slackUser.tz,
}),
mmddyyyy: meetingDate.toLocaleDateString('en-us', {
timeZone: slackUser.tz,
}),
})
convo.say({
text: `Ok, I'll record that you met *{{vars.date.dayName}} ({{{vars.date.mmddyyyy}}})*`,
action: 'attendance',
})
convo.next()
} else {
console.log(response, convo)
bot.replyInteractive(
response,
`_orpheus gives you a knowing look. she's apparently masking the fact she can't tell what day you meant_`
)
convo.gotoThread('date')
convo.next()
}
},
},
],
{},
'date'
)
convo.addQuestion(
`How many people showed up? (please just enter digits– I'm fragile)`,
(response, convo) => {
const attendance = +response.text
if (attendance > 0 && attendance % 1 === 0) {
console.log(
`*User said they had "${response.text}" in attendance, which is valid`
)
convo.setVar('attendance', attendance)
convo.say({
text: `I parsed that as *{{vars.attendance}}* hackers`,
action: 'confirm',
})
convo.next()
} else {
console.log(
`*User said they had "${response.text}" in attendance, which is invalid`
)
convo.say({
text:
"_orpheus scrunches her face, eyeing your input with suspicion. looks like that wasn't what she was looking for_",
})
convo.repeat()
convo.next()
}
},
{},
'attendance'
)
convo.gotoThread('found')
}
)
})
}
export default interactionCheckin