forked from carmex/SlackCountingBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
156 lines (136 loc) · 4.94 KB
/
app.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
require('dotenv').config();
const {App, ExpressReceiver} = require('@slack/bolt');
const {getStatsMessage, getHelpMessage} = require('./utils');
const statsManager = require('./statsManager');
const {processMessage} = require('./gameLogic');
const AsyncLock = require('async-lock');
const { i, detDependencies, efimovFactorDependencies, kron } = require('mathjs');
// Create a custom receiver
const receiver = new ExpressReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET,
processBeforeResponse: true
});
// Create the Bolt app, using the custom receiver
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
receiver
});
// Handle the challenge request (Slack APP verification)
receiver.router.post('/', (req, res) => {
if (req.body.type === 'url_verification') {
res.send(req.body.challenge);
} else {
res.sendStatus(404);
}
});
// Function to send error messages to the thread of the original message
async function sendErrorToSlack(error, client, channel, thread_ts) {
await client.chat.postMessage({
channel: channel,
thread_ts: thread_ts,
text: `An error occurred: \`\`\`${error}\`\`\``
});
}
const lock = new AsyncLock();
const messageQueue = [];
// Function to process messages (both regular and !eval)
async function processAndRespond(message, say, client, isEval = false) {
try {
const result = await processMessage(message, say, client, isEval);
if (isEval) {
// For !eval, return the result explicitly to be handled by the calling function
if (result) {
return result; // Return the result instead of calling say()
} else {
return "Invalid expression or operation not allowed.";
}
}
} catch (error) {
console.error('Error processing message:', error);
if (isEval) {
return `An error occurred while processing the expression.\n\`\`\`${error}\`\`\``;
}
// Send the error message to the thread of the original message
await sendErrorToSlack(error, client, message.channel, message.ts);
}
}
app.message(/^(?!!)[^!].*$/, async ({message, say, client}) => {
if (message.channel !== process.env.COUNTING_GAME_CHANNEL_ID) return;
messageQueue.push({message, say, client});
// Process the queue
lock.acquire('messageProcessing', async (done) => {
while (messageQueue.length > 0) {
const {message, say, client} = messageQueue.shift();
await processAndRespond(message, say, client);
}
done();
}, (err, ret) => {
if (err) {
console.error('Error processing message queue:', err);
}
});
});
app.command('/counting-stats', async ({command, ack, say, client}) => {
await ack();
const statsMessage = await getStatsMessage(client, statsManager.getStats());
await client.chat.postEphemeral({
channel: command.channel_id,
user: command.user_id,
text: statsMessage
});
});
app.command('/counting-help', async ({command, ack, say, client}) => {
await ack();
await client.chat.postEphemeral({
channel: command.channel_id,
user: command.user_id,
text: getHelpMessage()
});
});
app.command('/counting-eval', async ({command, ack, say, client}) => {
await ack();
const evalExpression = command.text.trim(); // The expression to evaluate
const evalMessage = {text: evalExpression, user: command.user_id};
const result = await processAndRespond(evalMessage, say, client, true);
if (result) {
await client.chat.postEphemeral({
channel: command.channel_id,
user: command.user_id,
text: result
});
}
});
app.message('!help', async ({message, say}) => {
if (message.channel === process.env.COUNTING_GAME_CHANNEL_ID) {
await say(getHelpMessage());
}
});
app.message('!stats', async ({message, say, client}) => {
if (message.channel === process.env.COUNTING_GAME_CHANNEL_ID) {
const statsMessage = await getStatsMessage(client, statsManager.getStats());
await client.chat.postEphemeral({
channel: message.channel,
user: message.user,
text: statsMessage
});
}
});
// Add the !eval command
app.message(/^!eval (.+)$/, async ({message, say, client, context}) => {
if (message.channel !== process.env.COUNTING_GAME_CHANNEL_ID) return;
const evalExpression = context.matches[1].trim();
const evalMessage = {...message, text: evalExpression};
const result = await processAndRespond(evalMessage, say, client, true);
if (result) {
await client.chat.postEphemeral({
channel: message.channel,
user: message.user,
text: result
});
}
});
(async () => {
await statsManager.loadStats();
await app.start(process.env.PORT || 3000);
console.log('⚡️ Counting game bot is running!');
})();