-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification.js
More file actions
94 lines (71 loc) · 3.21 KB
/
notification.js
File metadata and controls
94 lines (71 loc) · 3.21 KB
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
import axios from 'axios'
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc.js'
import timezone from 'dayjs/plugin/timezone.js'
dayjs.extend(utc)
dayjs.extend(timezone)
import { database } from './firebase.js'
const timezoneBR = 'America/Sao_Paulo'
const SLACK_WEBHOOK = process.env.SLACK_WEBHOOK
const BISPER_WEBHOOK = process.env.BISPER_WEBHOOK
const BISPER_DEBOUNCE_INTERVAL = 4 * 60 * 60 * 1000 // 4 hours
let lastBisperNotification = dayjs().subtract(3, 'hour')
let namesDictionary
function formatMessage(ranking, recentReactions = []) {
const sortedNames = Object.keys(ranking).sort((a, b) => ranking[a].position - ranking[b].position)
const detailedPositions = sortedNames.reduce((msg, name) => {
const rnkngInfo = ranking[name]
const positionChange = rnkngInfo.prevRelease?.position - rnkngInfo.position
const positivePositionChangeText = positionChange > 0 ? `:arrow_up: ${positionChange}` : ''
const negativePositionChangeText = positionChange < 0 ? `:small_red_triangle_down: ${positionChange * -1}` : ''
const positionChangeText = positivePositionChangeText || negativePositionChangeText
const pointsChange = rnkngInfo.points - rnkngInfo.prevRelease?.points
const pointsChangeText = pointsChange ? (pointsChange > 0 ? `(+${pointsChange})` : `(${pointsChange})`) : ''
return `${msg} ${rnkngInfo.position}º ${namesDictionary[name] || name}: ${rnkngInfo.points}${pointsChangeText} ${positionChangeText} \n`
}, '')
const dateLabel = dayjs().tz(timezoneBR).format('D, MMMM, YYYY')
const message = `:wave: Ranking Individual do IEE :statue_of_liberty: \n\n :spiral_calendar_pad: ${dateLabel} \n\n ${detailedPositions}`
return message
}
async function notify(ranking) {
if (!namesDictionary) {
const namesSnap = await database.ref('names').once('value')
namesDictionary = namesSnap.val()
}
const message = formatMessage(ranking)
const now = dayjs()
const shouldSendBisper = now.diff(lastBisperNotification, 'millisecond') >= BISPER_DEBOUNCE_INTERVAL
console.log('lastBisperNotification', lastBisperNotification.format(), now.format())
console.log('shouldSendBisper', shouldSendBisper)
const promises = [axios.post(SLACK_WEBHOOK, { text: message })]
if (shouldSendBisper) {
promises.push(axios.post(BISPER_WEBHOOK, { text: message }, {
headers: {
'key': process.env.BISPER_API_KEY
}
}))
lastBisperNotification = now
}
await Promise.all(promises)
}
async function notifyReaction(reaction) {
if (!namesDictionary) {
const namesSnap = await database.ref('names').once('value')
namesDictionary = namesSnap.val()
}
const associateName = namesDictionary[reaction.associateName] || reaction.associateName
const emoji = reaction.emoji
const createdBy = reaction.createdBy || 'Anônimo'
const message = reaction.message ? `\n💬 "${reaction.message}"` : ''
const reactionMessage = `${associateName} recebeu ${emoji} de ${createdBy}${message}\n`
try {
await axios.post(BISPER_WEBHOOK, { text: reactionMessage }, {
headers: {
'key': process.env.BISPER_API_KEY
}
})
} catch (error) {
console.error('Erro ao enviar notificação de reação:', error)
}
}
export { notify, notifyReaction }