This repository was archived by the owner on May 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
83 lines (61 loc) · 2.4 KB
/
server.js
File metadata and controls
83 lines (61 loc) · 2.4 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
const { App } = require('@slack/bolt');
const async = require('async');
const app = new App({
signingSecret: process.env.SLACK_SIGNING_SECRET,
token: process.env.SLACK_BOT_TOKEN,
userToken: process.env.SLACK_USER_TOKEN
});
//Listen for messages in channels
app.message(async ({ message, context, say }) => {
var regexString = /(https:\/\/)([^.]+)(\.)(slack.com)(\/archives\/)([^\/]+)(\/p)([^.+-]+)/gm;
//Regex for link to a Slack message
//Checks if message is part of a thread, if it contains more than 3 replies and if the parent message contains a link to a Slack message
if (message.thread_ts !== undefined) {
try {
// Call the chat.scheduleMessage method with a token
var result = await app.client.channels.replies({
// The token you used to initialize your app is stored in the `context` object
token: process.env.SLACK_USER_TOKEN,
channel: message.channel,
thread_ts: message.thread_ts
});
if ((result.messages[0].reply_count == 3) && regexString.test(result.messages[0].attachments[0].from_url)) {
var eventText = result.messages[0].attachments[0].from_url;
var regexString = /(https:\/\/)([^.]+)(\.)(slack.com)(\/archives\/)([^\/]+)(\/p)([^.+-]+)/gm;
var textChannel = "Looks like this discussion is continued in another thread: ";
var match = regexString.exec(eventText);
var sourceChannel = match[6];
var timestamp = match[8];
var len = timestamp.length;
var ts = timestamp.substring(0, len - 6) + "." + timestamp.substring(len - 6);
//Retrieve permalink where the message was cross-posted
try {
const permalink = await app.client.chat.getPermalink({
token: context.botToken,
channel: message.channel,
message_ts: result.messages[3].ts
})
app.client.chat.postMessage({
token: context.botToken,
channel: sourceChannel,
text: textChannel + permalink.permalink,
thread_ts: ts,
unfurl_media: true,
unfurl_links: true
})
console.log(permalink)
} catch (error) {
console.error(error);
}
};
}
catch (error) {
console.error(error);
}
}
});
(async () => {
// Start the app
await app.start(process.env.PORT || 3000);
console.log('⚡️ Bolt app is running!');
})();