-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmeetingAdd.js
206 lines (194 loc) · 5.5 KB
/
meetingAdd.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
import { parseDate } from 'chrono-node'
import { recordMeeting, getInfoForUser, transcript, initBot, reaction } from '../utils'
import interactionTutorial from './tutorial'
const reactOnSuccess = ({ club, history }) => {
const channel = club.fields['Slack Channel ID']
const meetingCount = history.meetings.length + 1 // Increase by one because we've just successfully added a new club meeting
const p = new Promise(resolve => {
initBot(true).api.conversations.info({ channel }, (err, res) => {
if (!err && res.channel && res.channel.name) {
resolve(res.channel.name)
}
})
initBot(true).api.channels.info({ channel }, (err, res) => {
if (!err && res.channel && res.channel.name) {
resolve(res.channel.name)
}
})
})
.then(
channelName =>
new Promise((resolve, reject) => {
initBot(true).api.search.messages(
{
query: `"you are responsible" in:#${channelName}`,
count: 1,
sort: 'timestamp',
},
(err, res) => {
if (err) {
reject(err)
} else {
resolve(res.messages.matches[0])
}
}
)
})
)
.then(message => {
reaction(initBot(), 'add', message.channel.id, message.ts, 'white_check_mark')
initBot().replyInThread(
{ channel: message.channel.id, ts: message.ts },
`${transcript(
'meetingAdd.successThread.confirmation'
)} ${transcript('meetingAdd.successThread.count', { meetingCount })}`,
err => {
if (err) {
console.error(err)
}
}
)
})
}
const interactionMeetingAdd = (bot, message) => {
getInfoForUser(message.user).then(({ club, history, slackUser }) => {
if (message.text.indexOf(',') === -1) {
// either the user typed "help" or an incorrectly formatted command
bot.replyPrivateDelayed(
message,
transcript('meetingAdd.help', { day: history.lastMeetingDay }),
(err, response) => {
if (err) {
console.error(err)
}
}
)
return
}
const [rawDate, rawAttendance, ...other] = message.text.split(',')
const date = parseDate(rawDate)
const dayName = date.toLocaleDateString('en-us', {
weekday: 'long',
timeZone: slackUser.tz,
})
const mmddyyyy = date.toLocaleDateString('en-us', {
timeZone: slackUser.tz,
})
const attendance = parseInt((rawAttendance.match(/(\d+)/) || [])[0])
recordMeeting(
club,
{ date: mmddyyyy, attendance },
(err, meetingRecord) => {
if (err) {
console.error(err)
bot.replyPrivateDelayed(message, `Got error: \`${err}\``)
return
}
const formUrl = `https://airtable.com/shrMyUEbqWXqImXE3?prefill_Meeting+ID=${meetingRecord.id}`
bot.replyPrivateDelayed(
message,
transcript('meetingAdd.success', { formUrl })
)
interactionTutorial(bot, message)
reactOnSuccess({ club, history })
}
)
return
bot.replyPrivate(
message,
{
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: 'Just to confirm, is the following correct?',
},
},
{
type: 'divider',
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `Date: *${dayName} (${mmddyyyy})}*\nAttendance: *${attendance}*`,
},
},
{
type: 'divider',
},
{
type: 'actions',
elements: [
{
type: 'button',
text: {
type: 'plain_text',
text: '✅ submit',
emoji: true,
},
value: 'true',
},
{
type: 'button',
text: {
type: 'plain_text',
text: '⛔️ cancel',
emoji: true,
},
value: 'false',
},
],
},
{
type: 'context',
elements: [
{
type: 'mrkdwn',
text: 'For help, type `/meeting-add help`',
},
],
},
],
},
[
{
pattern: 'true',
callback: response => {
recordMeeting(
club,
{ date: date.mmddyyyy, attendance },
(err, meetingRecord) => {
if (err) {
bot.replyInteractive(
response,
`_⚠️ looks like something isn't working. All it says on my end is \`${err}\`._`
)
} else {
bot.replyInteractive(
response,
`_✅ You confirm everything is accurate._`
)
}
}
)
},
},
{
pattern: 'false',
callback: response => {
bot.replyInteractive(response, `_⛔️ You cancelled the command._`)
},
},
{
default: true,
callback: response => {
console.log('ignoring button')
},
},
]
)
})
}
export default interactionMeetingAdd