Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
chore: rip out a bunch of stuff to make way for slack edge
Browse files Browse the repository at this point in the history
  • Loading branch information
taciturnaxolotl committed Jul 1, 2024
1 parent 84710ed commit f85265a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 127 deletions.
Binary file modified bun.lockb
Binary file not shown.
21 changes: 20 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"keywords": [],
"author": "Jasper Mayone <[email protected]>",
"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",
Expand All @@ -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",
Expand All @@ -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"
},
Expand All @@ -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"
}
}
<<<<<<< HEAD
}
=======
}
>>>>>>> 707274a (chore: rip out a bunch of stuff to make way for slack edge)
152 changes: 29 additions & 123 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,140 +1,54 @@
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";
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 * * * * *",
Expand All @@ -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 };
3 changes: 0 additions & 3 deletions src/lib/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f85265a

Please sign in to comment.