This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
slack.js
38 lines (33 loc) · 1.31 KB
/
slack.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
const logger = require('./logger')
const {SLACK_API_TOKEN, SLACK_CHANNEL_VOTING} = process.env
let SlackWebClient
if (SLACK_API_TOKEN) {
SlackWebClient = new (require('@slack/client').WebClient)(SLACK_API_TOKEN)
}
const publish = (channel, content) => {
if (!SlackWebClient) {
logger.log('slack message not published (missing SLACK_API_TOKEN):\n' + content)
return true
}
return new Promise((resolve, reject) => {
SlackWebClient.chat.postMessage(channel, content, (err, res) => {
if (err) {
return reject(err)
}
return resolve(res)
})
})
}
exports.publish = publish
exports.publishComment = (user, comment) => {
const content = `*${user.firstName} ${user.lastName}* wrote: (${comment.id.substring(0, 8)})\n\n${comment.content}`
return publish(SLACK_CHANNEL_VOTING, content)
}
exports.publishCommentUpdate = (user, comment, oldComment) => {
const content = `*${user.firstName} ${user.lastName}* edited: (${comment.id.substring(0, 8)})\n\n*old:*\n${oldComment.content}\n\n*new:*\n${comment.content}`
return publish(SLACK_CHANNEL_VOTING, content)
}
exports.publishCommentUnpublish = (user, comment) => {
const content = `*${user.firstName} ${user.lastName}* unpublished: (${comment.id.substring(0, 8)})\n\n${comment.content}`
return publish(SLACK_CHANNEL_VOTING, content)
}