diff --git a/bun.lockb b/bun.lockb index 003f4fe..3c3fdeb 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index cf8bf60..3f07130 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,10 @@ "keywords": [], "author": "Jasper Mayone ", "dependencies": { +<<<<<<< HEAD "@prisma/client": "^5.16.1", +======= +>>>>>>> 707274a (chore: rip out a bunch of stuff to make way for slack edge) "@slack/bolt": "^3.18.0", "@slack/web-api": "^7.0.4", "@types/express": "^4.17.21", @@ -29,6 +32,10 @@ "colors": "^1.4.0", "cron": "^3.1.7", "dotenv": "^16.4.5", +<<<<<<< HEAD +======= + "elysia": "^1.0.26", +>>>>>>> 707274a (chore: rip out a bunch of stuff to make way for slack edge) "express": "^4.19.2", "form-data": "^4.0.0", "js-yaml": "^4.1.0", @@ -37,6 +44,10 @@ "nodemon": "^3.0.3", "postgres": "^3.4.4", "response-time": "^2.3.2", +<<<<<<< HEAD +======= + "slack-edge": "^0.13.2", +>>>>>>> 707274a (chore: rip out a bunch of stuff to make way for slack edge) "tsx": "^4.9.3", "yaml": "^2.4.5" }, @@ -45,13 +56,21 @@ "@types/js-yaml": "^4.0.9", "@types/node-statsd": "^0.1.6", "@types/response-time": "^2.3.8", +<<<<<<< HEAD "prisma": "^5.16.1", "ts-node": "^10.9.2", "tsx": "^4.9.3", +======= + "ts-node": "^10.9.2", +>>>>>>> 707274a (chore: rip out a bunch of stuff to make way for slack edge) "typescript": "^5.4.5" }, "engines": { "node": ">=20.0.0", "pnpm": ">=9.4.0" } -} \ No newline at end of file +<<<<<<< HEAD +} +======= +} +>>>>>>> 707274a (chore: rip out a bunch of stuff to make way for slack edge) diff --git a/src/index.ts b/src/index.ts index f7cb58c..58c9738 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,8 @@ -import dotenv from "dotenv"; -dotenv.config(); - -import { PrismaClient } from "@prisma/client"; -import { App, ExpressReceiver } from "@slack/bolt"; -import axios from "axios"; +import { SlackApp } from "slack-edge"; import colors from "colors"; -import { CronJob } from "cron"; -import express from "express"; -import responseTime from "response-time"; +import CronJob from "cron"; + +import { Elysia } from "elysia"; import { indexEndpoint } from "./endpoints"; import { healthEndpoint } from "./endpoints/health"; @@ -15,126 +10,45 @@ import { t } from "./lib/templates"; import metrics from "./metrics"; import { blog, slog } from "./util/Logger"; -const receiver = new ExpressReceiver({ - signingSecret: process.env.SLACK_SIGNING_SECRET!, -}); - -const app = new App({ - token: process.env.SLACK_BOT_TOKEN!, - appToken: process.env.SLACK_APP_TOKEN!, - signingSecret: process.env.SLACK_SIGNING_SECRET!, - receiver, +const app = new SlackApp({ + env: { + SLACK_BOT_TOKEN: process.env.SLACK_BOT_TOKEN!, + SLACK_SIGNING_SECRET: process.env.SLACK_SIGNING_SECRET!, + SLACK_APP_TOKEN: process.env.SLACK_APP_TOKEN!, + SLACK_LOGGING_LEVEL: "INFO", + }, + startLazyListenerAfterAck: true }); -const prisma = new PrismaClient(); - -app.event(/.*/, async ({ event, client }) => { +app.event("team_join", async ({ context, payload }) => { try { - metrics.increment(`slack.event.${event.type}`); - switch (event.type) { - case "team_join": - break; - } + metrics.increment(`slack.event.${payload.type}`); } catch (error) { blog(`Error in event handler: ${error}`, "error"); metrics.increment("slack.event.error"); } }); -app.action(/.*?/, async (args) => { - try { - const { ack, respond, payload, client, body } = args; - const user = body.user.id; - - await ack(); +const elysia = new Elysia() + .get("/", indexEndpoint) + .get("/ping", healthEndpoint) + .get("/up", healthEndpoint) + .listen(3000); - // @ts-ignore - metrics.increment(`slack.action.${payload.value}`); - - // @ts-ignore - switch (payload.action_id) { - case "initial": - metrics.increment("slack.action.initial"); - break; - } - } catch (error) { - blog(`Error in action handler: ${error}`, "error"); - metrics.increment("slack.action.error"); - } -}); - -app.command(/.*?/, async ({ ack, body, client }) => { - try { - await ack(); - metrics.increment(`slack.command.${body.command}`); - // This is not used - } catch (error) { - blog(`Error in command handler: ${error}`, "error"); - metrics.increment("slack.command.error"); - } -}); - -receiver.router.use(express.json()); -receiver.router.get("/", indexEndpoint); -receiver.router.get("/ping", healthEndpoint); -receiver.router.get("/up", healthEndpoint); +export default { + port: 3000, + async fetch(request: Request) { + return await app.run(request); + }, +} -receiver.router.use( - responseTime((req, res, time) => { - const stat = (req.method + "/" + req.url?.split("/")[1]) - .toLowerCase() - .replace(/[:.]/g, "") - .replace(/\//g, "_"); +let env = process.env.NODE_ENV; +slog(t("app.startup", { environment: env }), "info"); - const httpCode = res.statusCode; - const timingStatKey = `http.response.${stat}`; - const codeStatKey = `http.response.${stat}.${httpCode}`; - metrics.timing(timingStatKey, time); - metrics.increment(codeStatKey, 1); - }) +console.log( + colors.bgCyan(`⚡️ Bolt app is running in env ${process.env.NODE_ENV}`) ); -app.use(async ({ payload, next }) => { - metrics.increment(`slack.request.${payload.type}`); - await next(); -}); - -// Add metric interceptors for axios -axios.interceptors.request.use((config: any) => { - config.metadata = { startTs: performance.now() }; - return config; -}); - -axios.interceptors.response.use((res: any) => { - const stat = (res.config.method + "/" + res.config.url?.split("/")[1]) - .toLowerCase() - .replace(/[:.]/g, "") - .replace(/\//g, "_"); - - const httpCode = res.status; - const timingStatKey = `http.request.${stat}`; - const codeStatKey = `http.request.${stat}.${httpCode}`; - metrics.timing( - timingStatKey, - performance.now() - res.config.metadata.startTs - ); - metrics.increment(codeStatKey, 1); - - return res; -}); - -const logStartup = async (app: App) => { - let env = process.env.NODE_ENV; - slog(t("app.startup", { environment: env }), "info"); -}; - -app.start(process.env.PORT || 3000).then(async () => { - await logStartup(app); - console.log( - colors.bgCyan(`⚡️ Bolt app is running in env ${process.env.NODE_ENV}`) - ); -}); - // Heartbeat new CronJob( "0 * * * * *", @@ -146,13 +60,5 @@ new CronJob( "America/New_York" ); -// new CronJob( -// "0 * * * * *", -// function() -// null, -// true, -// "America/New_York" -// ); - const client: any = app.client; export { app, client }; diff --git a/src/lib/templates.ts b/src/lib/templates.ts index 4d147ab..72c58f0 100644 --- a/src/lib/templates.ts +++ b/src/lib/templates.ts @@ -29,9 +29,6 @@ function flatten(obj: any, prefix: string = "") { const templates = flatten(templatesRaw); -const pfpFile = fs.readFileSync("./src/lib/arcadius.yaml", "utf8"); -export const pfps = parse(pfpFile); - export function t(template: template, data: data) { // return (randomChoice(templates[template]) as string).replace(/\${(.*?)}/g, (_, key) => (data as any)[key]) return t_format(t_fetch(template), data);