-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
102 lines (82 loc) · 2.35 KB
/
index.js
File metadata and controls
102 lines (82 loc) · 2.35 KB
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
const appInsights = require("applicationinsights");
appInsights.setup().start();
const express = require("express");
const app = express();
const Botkit = require("botkit");
const Promise = require("bluebird");
const _ = require("underscore");
const controller = Botkit.slackbot();
const haiku = require("./haiku");
const token = process.env.HAIKU_BOT_SECRET;
if (!token) {
throw "No Slack token found for Haiku bot under the HAIKU_BOT_SECRET environment variable";
}
const bot = controller.spawn({
token,
});
const channelInfo = Promise.promisify(bot.api.channels.info);
const userInfo = Promise.promisify(bot.api.users.info);
function restart() {
bot.closeRTM();
bot.startRTM();
}
restart();
setInterval(() => restart(), 60 * 60 * 1000);
try {
controller.on("ambient", function (bot, message) {
if (message.text) {
const words = haiku.expandMessage(message.text);
const possibleHaiku = haiku.tryAndMakeHaiku(words, debug);
if (possibleHaiku) {
bot.reply(
message,
{
text: `>>>${haiku.formatMessage(possibleHaiku)}`,
username: "HaikuBot",
icon_emoji: ":writing_hand:",
},
function (err, haikuMsg) {
if (err) {
return;
}
logSuccesfulHaiku(haikuMsg, message);
addReaction(haikuMsg);
}
);
}
}
});
} catch (err) {
console.error("Something failed", err);
}
function addReaction(message) {
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: "cherry_blossom",
});
}
function logSuccesfulHaiku(haikuMsg, originalMsg) {
const userPromise = userInfo({ user: originalMsg.user });
const channelPromise = channelInfo({ channel: haikuMsg.channel });
Promise.all([userPromise, channelPromise]).then((x) => {
const messageId = haikuMsg.ts.replace(".", "");
const link = `https://q42.slack.com/archives/${x[1].channel.name}/p${messageId}`;
log(`Haiku door ${x[0].user.name} in ${x[1].channel.name}.\n${link}`);
});
}
function debug(msg) {
log(`\`\`\`Debug: ${msg}\`\`\``);
}
function log(msg) {
bot.say({
text: msg,
channel: "C1CPAD96Z",
});
}
app.get("/", function (req, res) {
res.send("ping");
});
app.listen(process.env.PORT, function () {
console.log(`Example app listening on port ${process.env.PORT}!`);
});