-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
90 lines (79 loc) · 3.75 KB
/
index.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
var fs = require('fs');
var dirname = require('path').dirname;
var conciergeFile = dirname(__dirname) + '/concierge.json';
module.exports = function(context) {
// Initialize concierge on first run
try {
fs.accessSync(conciergeFile, fs.F_OK);
} catch (e) {
// It isn't accessible
try {
fs.writeFileSync(conciergeFile, '{}');
} catch(er) {
console.error('Unable to create concierge file');
}
}
return {
callConcierge: function(req, res) {
try {
var list = fs.readFileSync(conciergeFile);
list = JSON.parse(list);
if (!list[req.channel.name]) {
return res.text('No concierge assigned for this channel. Use `@concierge assign @user`').send();
}
var link = 'https://auth0.slack.com/archives/' + req.channel.name + '/p' + req.message.timestamp.replace('.', '');
var conciergeMessage = '@' + req.from.name + ' needs your attention in #' + req.channel.name + ' (' + link + ') \n\n*Message*:\n';
res.text(conciergeMessage + req.message.value.text, list[req.channel.name]);
return res.text('A message has been sent to the concierge (`' + list[req.channel.name] + '`). If your message is urgent and you don\'t receive a reply within 15 minutes, please use `@here` or `@channel`.').send();
} catch (e) {
return res.text('An error has occurred while trying to contact the concierge.\n```' + JSON.stringify(e) + '```').send();
}
},
whosConcierge: function(req, res) {
try {
var list = fs.readFileSync(conciergeFile);
list = JSON.parse(list);
var conciergeName = list[req.channel.name];
if (conciergeName) {
return res.text('The concierge for this channel is `' + conciergeName + '`. To send a direct message to the concierge use `@concierge message: {text}`').send();
}
return res.text('This channel does not have a concierge assigned.').send();
} catch (e) {
return res.text('An error has occurred while trying to assign the concierge.\n```' + JSON.stringify(e) + '```').send();
}
},
assignConcierge: function(req, res) {
try {
var list = fs.readFileSync(conciergeFile);
list = JSON.parse(list);
var name = req.params.name;
if (name.charAt(0) !== '@') {
name = '@' + name;
}
list[req.channel.name] = name;
fs.writeFileSync(conciergeFile, JSON.stringify(list, null, 2));
return res.text('User ' + name + ' has been assigned concierge for this channel.').send();
} catch (e) {
return res.text('An error has occurred while trying to assign the concierge.\n```' + JSON.stringify(e) + '```').send();
}
},
clearConcierge: function(req, res) {
try {
var list = fs.readFileSync(conciergeFile);
list = JSON.parse(list);
if (list[req.channel.name]) {
delete list[req.channel.name];
fs.writeFileSync(conciergeFile, JSON.stringify(list, null, 2));
return res.text('Unassigned concierge for this channel.').send();
}
return res.text('This channel does not have a concierge assigned. Nothing changed.').send();
} catch (e) {
return res.text('An error has occurred while trying to unassign the concierge.\n```' + JSON.stringify(e) + '```').send();
}
},
help: function(req, res) {
var helpMessage = "Here\'s what I can do:\n- Use `@concierge who` to check who is the assigned concierge for this channel.\n- Use `@concierge assign: {user}` to assign a person to the current channel.\n- Use `concierge message: {text}` to send a direct message to the concierge.\n- Use `concierge stop: on-call` to clear the assignment for this channel.";
return res.text(helpMessage).send();
}
};
};