-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (70 loc) · 2.26 KB
/
index.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
const { IncomingWebhook } = require('@slack/webhook');
const postChannel = {
safe: {
webhookUrl: process.env.SAFE_CHANNEL_WEBHOOK_URL,
channelId: process.env.SAFE_CHANNEL_ID,
},
nsfw: {
webhookUrl: process.env.NSFW_CHANNEL_WEBHOOK_URL,
channelId: process.env.NSFW_CHANNEL_ID,
},
};
exports.koresuki = (req, res) => {
// Cross Origin
{
res.set('Access-Control-Allow-Origin', '*');
if (req.method === 'OPTIONS') {
// Send response to OPTIONS requests
res.set('Access-Control-Allow-Methods', 'GET');
res.set('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Max-Age', '3600');
res.status(204).send('');
return
}
}
(async () => {
const { command, text } = req.body;
const postedUserName = req.body.user_name;
if (!/https:\/\/(twitter|x)\.com\/\w+\/status\/\d+/.test(text)) {
res.status(200).json({
response_type: 'ephemeral',
text: 'メッセージ中にTwitterのURLが存在しないため処理を中断します',
});
return;
}
let postChannelId = null;
try {
switch (command) {
case '/koresuki':
postChannelId = postChannel.safe.channelId;
await new IncomingWebhook(postChannel.safe.webhookUrl).send({
text: `${postedUserName} さんから: \n${text}`,
unfurl_links: true,
unfurl_media: true,
});
break;
case '/nsfw':
postChannelId = postChannel.nsfw.channelId;
await new IncomingWebhook(postChannel.safe.webhookUrl).send({
text: `${postedUserName} さんが <#${postChannelId}> に Twitter URL を投稿しました`,
});
await new IncomingWebhook(postChannel.nsfw.webhookUrl).send({
text: `${postedUserName} さんから: \n${text}`,
});
break;
default:
break;
}
} catch (err) {
res.status(200).json({
response_type: 'ephemeral',
text: `投稿に失敗しました\n\`\`\`\n${err}\`\`\``,
});
return;
}
res.status(200).json({
response_type: 'ephemeral',
text: `下記の内容 を <#${postChannelId}> に投稿しました\n\`\`\`\n${text}\`\`\``,
});
})().then();
};