-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
executable file
·56 lines (49 loc) · 1.58 KB
/
bot.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
#!/usr/bin/env node
"use strict";
const { WebClient } = require("@slack/client");
const { RTMClient } = require("@slack/client");
const fs = require("fs");
const Carryall = require("./lib/core/Carryall");
const { tail, anyPass } = require("ramda");
const parseConfig = () => {
const config = JSON.parse(fs.readFileSync("./carryall.json", { "encoding": "UTF-8" }));
config.reporter.mode = "slack";
config.control = { silent: true, noRestart: false }
return config;
}
const config = parseConfig();
const web = new WebClient(config.reporter.slack.token);
const rtm = new RTMClient(config.reporter.slack.token);
rtm.start();
/**
* If the regular expression matches, then the action function is invoked
* @return true if there was a match, false otherwhise
*/
const match = (regexp, action) => message => {
const match = message.text.match(regexp);
if (match) {
action(message,...tail(match));
return true;
} else {
return false;
}
}
const reply = message => original => {
web.chat.postMessage(Object.assign({ channel: original.channel }, message))
}
const handleMessage = anyPass([
match(/list\s+(\w+)/i, (message, environment) => {
const config = parseConfig();
if (environment.toUpperCase() === config.environment) {
config.reporter.slack.channel = message.channel;
new Carryall().state(config)
}
}),
match(/list/i, reply({ text: "Please specify an environment for listing" })),
match(/i\s+love*\s+[you|u]/i, reply({ text: "Me too" })),
]);
rtm.on("message", message => {
if (message.text && message.text.includes(`<@${rtm.activeUserId}>`)) {
handleMessage(message);
}
})