-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
177 lines (155 loc) · 5.46 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var RtmClient = require('@slack/client').RtmClient,
token = process.env.SANDBOT_TOKEN || '',
sheet = require('./sheets'),
Promise = require('bluebird'),
request = require('request'),
rtm = new RtmClient(token, {logLevel: 'error'}),
auth = sheet.authorize(),
RTM_EVENTS = require('@slack/client').RTM_EVENTS,
STATUS_PATTERN = /sandbot status/i,
BOOK_PATTERN = /(biore|taking) (sandbox|adeng)-/i,
RELEASE_PATTERN = /(zwalniam|releasing) (sandbox|adeng)-/i,
PING_PATTERN = /sandbot (zyjesz|ping)\?/i;
rtm.start();
rtm.on(RTM_EVENTS.MESSAGE, function (message) {
if (message.text && STATUS_PATTERN.test(message.text)) {
getStatus(message.channel).then(function (data) {
var promises = [];
Object.keys(data.result).forEach(function (key) {
promises.push(parseSandboxStatus(key, data.result[key]));
});
Promise.all(promises).then(function (data) {
var parsedMsg = '```';
data.forEach(function (item) {
parsedMsg += item[0] + ': ' + item[1] + '\n';
});
rtm.sendMessage(parsedMsg + '```', message.channel);
})
})
}
if (message.text && BOOK_PATTERN.test(message.text)) {
var sandboxName = getSandboxNameFromMessage(message),
msg = '<@' + message.user + '> ';
getPreviousUser(message.channel, sandboxName)
.then(function (previousUser) {
if (previousUser.result) {
msg += ':-1: - <@' + previousUser.result + '> is using it';
rtm.sendMessage(msg, message.channel)
} else {
bookSandbox(message)
.then(function () {
msg += ':+1:';
rtm.sendMessage(msg, message.channel)
})
}
});
}
if (message.text && RELEASE_PATTERN.test(message.text)) {
releaseSandbox(message)
.then(function (data) {
if (data.response) {
rtm.sendMessage(data.response, message.channel);
} else {
var msg = '<@' + message.user + '> :+1:';
}
rtm.sendMessage(msg, message.channel)
})
}
if (message.text && PING_PATTERN.test(message.text)) {
rtm.sendMessage('zyje :+1:', message.channel);
}
});
function getStatus(channel) {
return new Promise(function (resolve, reject) {
auth
.then(function (authData) {
return sheet.getStatus(authData, channel);
})
.then(function (data) {
resolve(data)
});
});
}
function getPreviousUser(channel, sandboxName) {
return new Promise(function (resolve, reject) {
auth
.then(function (authData) {
return sheet.getCurrentUser(authData, channel, sandboxName);
})
.then(function (data) {
resolve(data);
})
})
}
function bookSandbox(message) {
var sandboxName = getSandboxNameFromMessage(message);
return new Promise(function (resolve, reject) {
auth
.then(function (authData) {
return sheet.bookSandbox(authData, message.channel, sandboxName, message.user);
})
.then(function (data) {
resolve(data)
})
});
}
function getUserNameById(userId) {
return new Promise(function (resolve, reject) {
request(
{
url: 'https://slack.com/api/users.info',
qs: {
token: token,
user: userId
}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var json = JSON.parse(body);
resolve(json.user.name);
}
})
})
}
function getSandboxNameFromMessage(message) {
var text = message.text,
match = text.match(/((sandbox|adeng)-.*)/i);
console.log(match);
if (match) {
return match[0].replace('-', '_');
}
}
function parseSandboxStatus(key, value) {
if (value) {
return new Promise(function (resolve, reject) {
getUserNameById(value)
.then(function (userName) {
resolve([key, userName]);
});
});
} else {
return [key, 'free'];
}
}
function releaseSandbox(message) {
var authorization, response, sandboxName;
return new Promise(function (resolve, reject) {
auth
.then(function (authData) {
authorization = authData;
sandboxName = getSandboxNameFromMessage(message);
return sheet.getCurrentUser(authData, message.channel, sandboxName);
})
.then(function (data) {
if (data.result && data.result !== message.user) {
response = ':pirate: take over!!! <@' + data.result + '>, <@' + message.user + '> is releasing your sandbox!:pirate:';
}
return sheet.releaseSandbox(authorization, message.channel, sandboxName, message.user);
})
.then(function (data) {
resolve({
response: response,
data: data
});
});
})
}