This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
204 lines (174 loc) · 6.29 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'use strict';
var ArgParser = require('./lib/arg-parser');
var qs = require('qs');
// wraps logic around routing
function SlackBot(config) {
this.config = config || {};
this.commands = {};
this.aliases = {};
if (!this.config.structureResponse) {
this.config.structureResponse = function (response) {
return response;
};
}
}
// build a response to return to Slack
SlackBot.prototype._buildResponse = function (response_type, response) {
var modifiedResponse = response;
if (typeof response === 'string') {
modifiedResponse = { response_type: response_type, text: response };
} else {
modifiedResponse.response_type = response_type;
}
return this.config.structureResponse(modifiedResponse);
};
// add a command
SlackBot.prototype.addCommand = function (command, args, desc, callback) {
var realCallback = callback;
var realDesc = desc;
var realArgs = args;
// if only 3 arguments are passed, then we're assuming no args are used for
// this function, in which case we should shift all of the arguments down one
if (arguments.length === 3) {
realCallback = desc;
realDesc = args;
realArgs = [];
}
this[command] = realCallback;
this.commands[command] = { args: realArgs, desc: realDesc };
};
// alias a command so that it can be called by multiple names
SlackBot.prototype.aliasCommand = function (commandName) {
var argIndex;
if (!Object.prototype.hasOwnProperty.call(this.commands, commandName)) {
throw new Error(commandName + ' is not a configured command');
}
for (argIndex = 1; argIndex < arguments.length; argIndex += 1) {
if (Object.prototype.hasOwnProperty.call(this.aliases, arguments[argIndex])) {
throw new Error(arguments[argIndex] + ' is already aliased or is an invalid alias name');
}
this.aliases[arguments[argIndex]] = commandName;
}
};
// call a stored command
SlackBot.prototype.callCommand = function (commandName, event, callback) {
var args;
var modifiedEvent = event;
if (!Object.prototype.hasOwnProperty.call(this.commands, commandName)) {
return this.help(event, callback);
}
args = ArgParser.align(this.commands[commandName].args, event.args || []);
if (args !== false) {
modifiedEvent.args = args;
return this[commandName](modifiedEvent, callback);
}
return this.help(event, callback);
};
// add an action
SlackBot.prototype.addAction = function (action, callback) {
if (!this[action]) {
this[action] = callback;
} else {
throw new Error('Action ' + action + ' is already defined as a command or action');
}
};
// process an action
SlackBot.prototype.processAction = function (action, event, callback) {
if (!this[action]) {
throw new Error('Tried to process ' + action + ' but did not find the corresponding action');
} else {
return this[action](event, callback);
}
};
// respond to the whole channel
SlackBot.prototype.inChannelResponse = function (response) {
return this._buildResponse('in_channel', response);
};
// respond to just the requesting user
SlackBot.prototype.ephemeralResponse = function (response) {
return this._buildResponse('ephemeral', response);
};
// respond with a usage message
SlackBot.prototype.help = function (event, callback) {
var _this = this;
var helpText = '';
var aliasText;
Object.keys(this.commands).forEach(function (command) {
helpText += command;
// add argument description
if (this.commands[command].args.length) {
helpText += ' ' + this.commands[command].args.map(function (arg) {
var optionalArgName;
if (arg instanceof Object) {
optionalArgName = Object.keys(arg)[0];
return optionalArgName + ':' + arg[optionalArgName];
}
return arg.toString();
}).join(' ');
}
// add alias description
aliasText = Object.keys(this.aliases).filter(function (alias) {
return _this.aliases[alias] === command;
});
if (aliasText.length) {
helpText += ' (' + aliasText.join(', ') + ')';
}
// add command description
helpText += ': ' + this.commands[command].desc + '\n';
}.bind(this));
helpText += 'help: display this help message';
callback(null, this.ephemeralResponse({
text: 'Available commands:',
attachments: [{ text: helpText }]
}));
};
/**
* Find a command to match the given payload for "one two three", looks for matches for:
* - "one two three"
* - "one two"
* - "one"
*/
SlackBot.prototype.findCommand = function (payload) {
var splitPayload = payload.split(' ');
var commandName;
var commandNameLength;
for (commandNameLength = payload.length - 1; commandNameLength > 0; commandNameLength -= 1) {
commandName = splitPayload.slice(0, commandNameLength).join(' ');
if (Object.prototype.hasOwnProperty.call(this.aliases, commandName)) {
return { commandName: this.aliases[commandName], args: splitPayload.slice(commandNameLength) };
}
if (Object.prototype.hasOwnProperty.call(this.commands, commandName)) {
return { commandName: commandName, args: splitPayload.slice(commandNameLength) };
}
}
return { commandName: 'help' };
};
// control the flow of queries from Slack
SlackBot.prototype.buildRouter = function () {
return function (event, context, callback) {
var foundCommand;
var foundAction;
var builtEvent = event;
if (this.config.pingEnabled && event.source && event.source === 'aws.events' &&
event.resources && event.resources[0].indexOf('ping') !== -1) {
return context.succeed('Ok');
}
builtEvent.body = qs.parse(builtEvent.body);
if (builtEvent.body.payload) {
builtEvent.body = JSON.parse(builtEvent.body.payload);
}
if (this.config.token && (!builtEvent.body.token || builtEvent.body.token !== this.config.token)) {
return context.fail('Invalid Slack token');
}
// Handle actions from slack buttons
if (builtEvent.body.actions) {
// Though presented as an array, slack will only send a single action per incoming invocation.
foundAction = builtEvent.body.actions[0].name;
return this.processAction(foundAction, builtEvent, callback);
} // Else route to commands
foundCommand = this.findCommand(builtEvent.body.text);
builtEvent.args = foundCommand.args;
return this.callCommand(foundCommand.commandName, builtEvent, callback);
}.bind(this);
};
module.exports = SlackBot;