Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
100 lines (85 loc) · 3.19 KB
/
Copy pathmain.js
File metadata and controls
100 lines (85 loc) · 3.19 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
import bolt from '@slack/bolt';
if (!process.env.TOKEN || !process.env.SLACK_SIGNING_SECRET || !process.env.SLACK_TEAM ) {
console.log('Error: Specify TOKEN in environment');
console.log('or Error: Specify SLACK_SIGNING_SECRET in environment');
console.log('or Error: Specify SLACK_TEAM in environment');
process.exit(1);
}
const app = new bolt.App({
token: process.env.TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
});
app.message(async ({ message, client }) => {
console.log(message);
// 自分のメッセージには反応しない
if (message.subtype === 'bot_message' || message.bot_id) {
return;
}
const channelId = message.channel;
const eventTs = message.ts;
const chatGetPermalinkResponse = await app.client.chat.getPermalink({
channel: channelId,
message_ts: eventTs
});
const postLink = chatGetPermalinkResponse.permalink;
try {
// チャンネル情報の取得
const channelInfo = await client.conversations.info({
channel: channelId
});
const channelName = channelInfo.channel.name;
// times_チャンネルの処理
if (channelName.match('^times_.+')) {
repostTo('#timeline', client, postLink, message);
}
// exceptionsチャンネルの処理
if (channelName.match('^exceptions$')) {
sortExceptionIntoAppropriateChannel(client, postLink, message);
}
// 日報チャンネルの処理
if (channelName.match('^日報_(.*)$') && (channelName.match(/_/g) || []).length === 1) {
repostTo('#日報_all', client, postLink, message);
}
if (channelName.match('^日報_(.*)_.+')) {
const matcher = channelName.match('^日報_(.*)_.+');
repostTo(`#日報_${matcher[1]}`, client, postLink, message);
repostTo('#日報_all', client, postLink, message);
}
} catch (error) {
console.error(`Error processing message: ${error}`);
}
});
// 特定のチャンネルに再投稿する関数
async function repostTo(channel, client, postLink, message) {
console.log(message);
try {
await client.chat.postMessage({
text: `${postLink}`,
channel: channel,
unfurl_links: true
});
} catch (error) {
console.error(`Error reposting message: ${error}`);
}
}
// exceptionsチャンネルの内容を適切なチャンネルに振り分ける関数
async function sortExceptionIntoAppropriateChannel(client, postLink, message) {
let repostToTargetChannelPostfix = 'others';
if (message.text.match(/\/manage\//)) {
repostToTargetChannelPostfix = 'manage';
} else if (message.text.match(/\/business\//)) {
repostToTargetChannelPostfix = 'business';
} else if (message.text.match(/\/for_team_manage\//)) {
repostToTargetChannelPostfix = 'for_team_manage';
} else if (message.text.match(/\/works\//)) {
repostToTargetChannelPostfix = 'works';
} else if (message.text.match(/\/cgc\//) || message.text.match(/\/poh\//) || message.text.match(/\/codechronicle\//)) {
repostToTargetChannelPostfix = 'games';
}
repostTo(`#exceptions_${repostToTargetChannelPostfix}`, client, postLink, message);
}
// アプリの起動
(async () => {
await app.start(process.env.PORT || 3000);
console.log('⚡️ Bolt app is running!');
})();