-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractiveMessagesManager.js
116 lines (103 loc) · 3.05 KB
/
interactiveMessagesManager.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
const uuid = require('uuid')
const request = require('request')
let interactiveMessageCallbacks = {}
function sendMessageToSlackResponseURL(responseURL, JSONmessage){
var postOptions = {
uri: responseURL,
method: 'POST',
headers: {
'Content-type': 'application/json'
},
json: JSONmessage
}
request(postOptions, (error, response, body) => {
if (error){
// handle errors as you see fit
}
})
}
module.exports.sendMessageToSlackResponseURL = sendMessageToSlackResponseURL
function interactiveResponse (context, channel, config, cb) {
const { webBot } = context
const { text, options } = config
const outerText = config.outerText || ""
const callback_id = uuid()
const toAttach = buttonAttachments(text, options, callback_id)
webBot.chat.postMessage(channel, outerText, toAttach, function (err, response) {
if (err) {
cb(err)
return
}
interactiveMessageCallbacks[callback_id] = {
cb,
outerText,
text,
options
}
})
// return this so that the caller of this function can cancel this
// like how setTimeout works
return callback_id
}
module.exports.interactiveResponse = interactiveResponse
function clearInteractiveResponse (id) {
interactiveMessageCallbacks[id] = undefined
}
module.exports.clearInteractiveResponse = clearInteractiveResponse
function clearInteractiveResponses () {
// todo: instead, iterate through and call the callbacks
// as having not completed
interactiveMessageCallbacks = {}
}
module.exports.clearInteractiveResponses = clearInteractiveResponses
function handleInteractiveResponse (payload, res) {
const { actions, callback_id } = payload
const interactiveMessage = interactiveMessageCallbacks[callback_id]
// todo: should this be an error?
if (!interactiveMessage) return
const value = actions[0].value
let replacement = interactiveMessage.options.find(function (o) {
return o.value === value
}).replaceWith
var message = {
"text": interactiveMessage.outerText,
"attachments": attachment(interactiveMessage.text + '\n' + replacement),
"replace_original": true
}
sendMessageToSlackResponseURL(payload.response_url, message)
interactiveMessage.cb(null, value)
delete interactiveMessageCallbacks[callback_id]
}
module.exports.handleInteractiveResponse = handleInteractiveResponse
function attachment (text) {
return [
{
"text": text,
"color": "#3AA3E3",
"attachment_type": "default"
}
]
}
module.exports.attachment = attachment
function buttonAttachments (text, options = [], callback_id) {
return {
"attachments": [
{
"text": text,
"fallback": "You are unable to offer your opinion",
"callback_id": callback_id,
"color": "#3AA3E3",
"attachment_type": "default",
"actions": options.map(o => {
return {
"name": "watthisdoes?",
"type": "button",
"text": o.text,
"value": o.value
}
})
}
]
}
}
module.exports.buttonAttachments = buttonAttachments