-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathslack.js
73 lines (64 loc) · 1.78 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
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
const { IncomingWebhook } = require('@slack/webhook')
const { WebClient } = require('@slack/web-api')
module.exports = {
/*
async postMessage (instance, data) {
const web = new WebClient(instance.accessToken)
await web.chat.postMessage(data)
},
*/
async publishView (instance, user, view) {
const web = new WebClient(instance.accessToken)
const object = {
user_id: user,
view
}
return web.views.publish(object)
},
async scheduleMessage (instance, timestamp, data) {
const web = new WebClient(instance.accessToken)
const object = {
...data,
channel: instance.channel,
post_at: Math.floor(timestamp.toSeconds())
}
try {
return await web.chat.scheduleMessage(object)
} catch (e) {
// If we fail because we're not in the channel we're trying to post to, join it
// first, then try again.
if (e.data.error === 'not_in_channel') {
await web.conversations.join({
channel: instance.channel
})
return web.chat.scheduleMessage(object)
} else {
throw e
}
}
},
async sendImToUser (instance, user, data) {
const web = new WebClient(instance.accessToken)
const conversationData = await web.conversations.open({
users: user
})
await web.chat.postMessage({
...data,
channel: conversationData.channel.id
})
},
async sendReplacingResponse (responseUrl, data) {
const webhook = new IncomingWebhook(responseUrl)
await webhook.send({
...data,
replace_original: true
})
},
async unscheduleMessage (instance, messageId, channel) {
const web = new WebClient(instance.accessToken)
await web.chat.deleteScheduledMessage({
channel,
scheduled_message_id: messageId
})
}
}