|
| 1 | +require("dotenv").config(); |
| 2 | +const { App } = require("@slack/bolt"); |
| 3 | +const fs = require("fs"); |
| 4 | +const app = new App({ |
| 5 | + token: process.env.SLACK_BOT_TOKEN, |
| 6 | + signingSecret: process.env.SLACK_SIGNING_SECRET, |
| 7 | + socketMode: true, |
| 8 | + appToken: process.env.SLACK_APP_TOKEN, |
| 9 | +}); |
| 10 | + |
| 11 | +(async () => { |
| 12 | + await app.start(process.env.PORT || 3000); |
| 13 | + async function pull() { |
| 14 | + var blocks = []; |
| 15 | + let sPromises = fs |
| 16 | + .readdirSync("./sections") |
| 17 | + .filter((str) => str.endsWith(".js")) |
| 18 | + .sort() |
| 19 | + .map(async (fn) => { |
| 20 | + const section = await require(`./sections/${fn}`); |
| 21 | + const text = (await section.render({ app })).trim(); |
| 22 | + blocks.push({ |
| 23 | + type: "section", |
| 24 | + text: { |
| 25 | + type: "plain_text", |
| 26 | + text: section.title, |
| 27 | + emoji: true, |
| 28 | + }, |
| 29 | + }); |
| 30 | + blocks.push({ |
| 31 | + type: "section", |
| 32 | + text: { |
| 33 | + type: "mrkdwn", |
| 34 | + text: text, |
| 35 | + }, |
| 36 | + }); |
| 37 | + blocks.push({ |
| 38 | + type: "divider", |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + await Promise.all(sPromises); |
| 43 | + |
| 44 | + const data = await app.client.conversations.history({ |
| 45 | + channel: process.env.SLACK_CHANNEL, |
| 46 | + }); |
| 47 | + const { messages } = data; |
| 48 | + |
| 49 | + await Promise.all( |
| 50 | + messages.map((message) => |
| 51 | + app.client.chat |
| 52 | + .delete({ |
| 53 | + token: process.env.SLACK_USER_TOKEN, |
| 54 | + channel: process.env.SLACK_CHANNEL, |
| 55 | + ts: message?.ts, |
| 56 | + thread_ts: message?.thread_ts, |
| 57 | + }) |
| 58 | + .catch((e) => { |
| 59 | + console.warn(e); |
| 60 | + }), |
| 61 | + ), |
| 62 | + ); |
| 63 | + blocks.push({ |
| 64 | + type: "section", |
| 65 | + text: { |
| 66 | + type: "mrkdwn", |
| 67 | + text: `Last Updated on ${new Date().toLocaleString("en-US", { timeZone: "America/New_York", timeStyle: "short", dateStyle: "long" })} (EST)`, |
| 68 | + }, |
| 69 | + }); |
| 70 | + app.client.chat.postMessage({ |
| 71 | + channel: process.env.SLACK_CHANNEL, |
| 72 | + blocks, |
| 73 | + }); |
| 74 | + } |
| 75 | + pull(); |
| 76 | + setInterval(pull, 1000 * 30); |
| 77 | + console.log("⚡️ Bolt app is running!"); |
| 78 | +})(); |
0 commit comments