This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
birthder.js
369 lines (298 loc) · 12 KB
/
birthder.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
// Description:
// Birthday bot.
//
// Commands:
// begin group Happy Birthder
// hubot birthdays list - shows a list of users and their birthdays
// hubot birthdays on <date>.<month> - shows a list of users with a set birthday date
// hubot fwd list - shows a list of users and their first working days
// begin admin
// hubot birthday set @username <date>.<month>.<year> - sets a birthday for the user
// hubot birthday delete @username - deletes birthday for the user
// hubot fwd set @username <date>.<month>.<year> - sets a first working day for the user
// end admin
// end group
//
(function () {
const moment = require('moment')
const routines = require('hubot-routines')
const schedule = require('node-schedule')
const utils = require('./utils')
module.exports = async (robot) => {
// Checking if the bot is in the channel specified via the BIRTHDAY_LOGGING_CHANNEL environment variable.
const botChannels = await robot.adapter.api.get('channels.list.joined')
const botGroups = await robot.adapter.api.get('groups.list')
const chExists = botChannels.channels.filter(item => item.name === utils.BIRTHDAY_LOGGING_CHANNEL).length
const grExists = botGroups.groups.filter(item => item.name === utils.BIRTHDAY_LOGGING_CHANNEL).length
if (!chExists && !grExists) {
routines.rave(robot, `Hubot is not in the group or channel named '${utils.BIRTHDAY_LOGGING_CHANNEL}'`)
return
}
const regExpUsername = new RegExp(/(?:@?(.+))/)
const regExpDate = new RegExp(/((\d{1,2})\.(\d{1,2})\.(\d{4}))\b/)
const regExpShortDate = new RegExp(/((\d{1,2})\.(\d{1,2}))\b/)
const routes = {
set: new RegExp(/(birthday set)\s+/.source + regExpUsername.source + /\s+/.source + regExpDate.source, 'i'),
delete: new RegExp(/(birthday delete)\s+/.source + regExpUsername.source + /\b/.source, 'i'),
check: new RegExp(/(birthdays on)\s+/.source + regExpShortDate.source, 'i'),
list: new RegExp(/(birthdays|fwd) list$/, 'i'),
fwd_set: new RegExp(/(fwd set)\s+/.source + regExpUsername.source + /\s+/.source + regExpDate.source, 'i'),
agreeToPitchIn: new RegExp(/Yes, I'll pitch in on a present for\s+/.source + regExpUsername.source, 'i'),
disagreeToPitchIn: new RegExp(/No, I won't pitch in on a present for\s+/.source + regExpUsername.source, 'i')
}
if (utils.TENOR_API_KEY === '') {
routines.rave(robot, 'TENOR_API_KEY is a mandatory parameter, however it\'s not specified.')
return
}
robot.enter(msg => {
if (msg.message.user.roomID === 'GENERAL') {
const brain = robot.brain.data.users
const username = msg.message.user.name
const user = Object.values(brain).filter(item => item.name === username).shift()
if (!user.dateOfBirth) {
robot.adapter.sendDirect({ user: { name: user.name } }, `Welcome to ${utils.COMPANY_NAME}! :tada:\nEmm... where was I?\nOh! Please, enter your date birth (DD.MM.YYYY).`)
}
const today = moment().format(utils.OUTPUT_DATE_FORMAT)
user.dateOfFwd = today
}
})
robot.respond(regExpDate, msg => {
const username = msg.message.user.name
const user = robot.brain.userForName(username)
const date = msg.match[1]
if (!user.dateOfBirth) {
if (routines.isValidDate(date, utils.DATE_FORMAT)) {
user.dateOfBirth = date
msg.send('I memorized you birthday, well done! :wink:')
robot.messageRoom(utils.BIRTHDAY_LOGGING_CHANNEL, `All right, @${user.name}'s birthday was specified!`)
} else {
msg.send(utils.MSG_INVALID_DATE)
}
}
})
// Link together the specified birthday and user and store the link in the brain.
robot.respond(routes.set, async (msg) => {
let date
let name
let user
let users
if (!await routines.isAdmin(robot, msg.message.user.name.toString())) {
msg.send(utils.MSG_PERMISSION_DENIED)
return
}
name = msg.match[2].trim()
date = msg.match[3]
users = []
for (const u of robot.brain.usersForFuzzyName(name)) {
if (await routines.isUserActive(robot, u)) {
users.push(u)
}
}
if (!routines.isValidDate(date, utils.DATE_FORMAT)) {
msg.send(utils.MSG_INVALID_DATE)
return
}
if (users.length === 1) {
user = users[0]
user.dateOfBirth = date
return msg.send(`Saving ${name}'s birthday.`)
} else if (users.length > 1) {
return msg.send(utils.getAmbiguousUserText(users))
} else {
return msg.send(`I have never met ${name}.`)
}
})
// Print the users names whose birthdays match the specified date.
robot.respond(routes.check, async (msg) => {
let date
let message
let users = []
let userNames
date = msg.match[2]
for (const u of utils.findUsersByDate(utils.BDAY_EVENT_TYPE, moment(date, utils.SHORT_DATE_FORMAT), robot.brain.data.users)) {
if (await routines.isUserActive(robot, u)) {
users.push(u)
}
}
if (users.length === 0) {
return msg.send('Could not find any user with the specified birthday.')
}
userNames = users.map(user => `@${user.name}`)
message = `${userNames.join(', ')}`
return msg.send(message)
})
// Delete the birthday associated with the specified user name.
robot.respond(routes.delete, async (msg) => {
let name = msg.match[2].trim()
let user
let users = []
if (!await routines.isAdmin(robot, msg.message.user.name.toString())) {
msg.send(utils.MSG_PERMISSION_DENIED)
return
}
for (const u of robot.brain.usersForFuzzyName(name)) {
if (await routines.isUserActive(robot, u)) {
users.push(u)
}
}
if (users.length === 1) {
user = users[0]
if (!user.dateOfBirth) {
return msg.send('A birth date is not specified for the user.')
}
user.dateOfBirth = null
return msg.send(`Removing ${name}'s birthday.`)
} else if (users.length > 1) {
return msg.send(utils.getAmbiguousUserText(users))
} else {
return msg.send(`I have never met ${name}.`)
}
})
// Print sorted users birthdays and first working days.
robot.respond(routes.list, async (msg) => {
let attr, desc, title
const getSortedDate = (date) => {
if (attr === 'dateOfBirth') {
const thisYear = moment().year()
const newDate = moment(date, utils.DATE_FORMAT).year(thisYear)
return newDate.unix() >= moment().unix()
? newDate.format(`DD.MM.${newDate.year() - 1}`) : newDate.format(`DD.MM.${newDate.year()}`)
}
if (attr === 'dateOfFwd') {
return date
}
}
if (msg.match[1] === 'birthdays') {
attr = 'dateOfBirth'
desc = `was born on`
title = `Birthdays`
}
if (msg.match[1] === 'fwd') {
attr = 'dateOfFwd'
desc = `joined our team`
title = `First working days`
}
let message
const allUsers = []
for (const u of Object.values(robot.brain.data.users)) {
if (await routines.isUserActive(robot, u)) {
allUsers.push(u)
}
}
message = allUsers
.filter(user => routines.isValidDate(user[attr], utils.DATE_FORMAT))
.map(user => {
const sortedDate = getSortedDate(user[attr])
return {
name: user.name,
[attr]: user[attr],
sortedDate: sortedDate
}
})
.sort((a, b) => utils.sorting(a.sortedDate, b.sortedDate, 'DD.MM.YYYY'))
.map(user => ` @${user.name} ${desc} ${moment(user[attr], utils.DATE_FORMAT).format(utils.OUTPUT_DATE_FORMAT)}`)
if (!message.length) {
msg.send('Oops... No results.')
return
}
msg.send(`*${title} list*\n${message.join('\n')}`)
})
robot.respond(routes.agreeToPitchIn, async msg => {
const targetUsername = msg.match[1]
const bdayUser = await routines.findUserByName(robot, targetUsername)
if (!bdayUser) {
msg.send(`I have never met ${targetUsername}.`)
return
}
if (!bdayUser.birthdayChannel) {
msg.send('This person does not plan to celebrate their birthday in the nearest future. Do not rush.')
return
}
bdayUser.birthdayPitchingInList = bdayUser.birthdayPitchingInList || []
if (bdayUser.birthdayPitchingInList.includes(msg.message.user.id)) {
msg.send('You have already told me that you would.')
} else {
bdayUser.birthdayPitchingInList.push(msg.message.user.id)
msg.send('Fine! We count on you.')
}
})
robot.respond(routes.disagreeToPitchIn, async msg => {
const targetUsername = msg.match[1]
const bdayUser = await routines.findUserByName(robot, targetUsername)
if (!bdayUser) {
msg.send(`I have never met ${targetUsername}.`)
return
}
if (!bdayUser.birthdayChannel) {
msg.send('This person does not plan to celebrate their birthday in the nearest future. Do not rush.')
return
}
bdayUser.birthdayPitchingInList = bdayUser.birthdayPitchingInList || []
if (bdayUser.birthdayPitchingInList.includes(msg.message.user.id)) {
bdayUser.birthdayPitchingInList = bdayUser.birthdayPitchingInList.filter(
userId => userId !== msg.message.user.id
)
msg.send('It\'s a shame. I will remove you from the pitching in list.')
} else {
msg.send('Ok, I understand.')
}
})
// Reset date of first working day.
robot.respond(routes.fwd_set, async (msg) => {
let date
let name
let user
let users
if (!await routines.isAdmin(robot, msg.message.user.name.toString())) {
msg.send(utils.MSG_PERMISSION_DENIED)
return
}
name = msg.match[2].trim()
date = msg.match[3]
users = []
for (const u of robot.brain.usersForFuzzyName(name)) {
if (await routines.isUserActive(robot, u)) {
users.push(u)
}
}
if (!routines.isValidDate(date, utils.DATE_FORMAT)) {
msg.send(utils.MSG_INVALID_DATE)
return
}
if (users.length === 1) {
user = users[0]
user.dateOfFwd = date
return msg.send(`Saving ${name}'s first working day.`)
} else if (users.length > 1) {
return msg.send(utils.getAmbiguousUserText(users))
} else {
return msg.send(`I have never met ${name}.`)
}
})
// Check regularly if today is someone's birthday, write birthday messages to the general channel.
if (utils.HAPPY_REMINDER_SCHEDULER) {
schedule.scheduleJob(utils.HAPPY_REMINDER_SCHEDULER, () => utils.sendCongratulations(robot, utils.BDAY_EVENT_TYPE))
}
// Check regularly if today is someone's anniversary day, write congratulation message to the general channel.
if (utils.HAPPY_REMINDER_SCHEDULER) {
schedule.scheduleJob(utils.HAPPY_REMINDER_SCHEDULER, () => utils.sendCongratulations(robot, utils.FWD_EVENT_TYPE))
}
// test
// Send reminders of the upcoming birthdays to the users (except ones whose birthday it is).
if (utils.HAPPY_REMINDER_SCHEDULER) {
schedule.scheduleJob(utils.HAPPY_REMINDER_SCHEDULER, () => utils.sendReminders(robot, utils.NUMBER_OF_DAYS_IN_ADVANCE, 'days'))
}
if (utils.HAPPY_REMINDER_SCHEDULER) {
schedule.scheduleJob(utils.HAPPY_REMINDER_SCHEDULER, () => utils.sendReminders(robot, 1, 'day'))
}
if (utils.HAPPY_REMINDER_SCHEDULER) {
schedule.scheduleJob(utils.HAPPY_REMINDER_SCHEDULER, () => utils.removeExpiredBirthdayChannels(robot))
}
if (utils.HAPPY_REMINDER_SCHEDULER) {
schedule.scheduleJob(utils.HAPPY_REMINDER_SCHEDULER, () => utils.detectBirthdaylessUsers(robot))
}
if (utils.HAPPY_REMINDER_SCHEDULER) {
schedule.scheduleJob(utils.HAPPY_REMINDER_SCHEDULER, () => utils.sendReminderOfBegging(robot))
}
}
}).call(this)