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

Commit

Permalink
kieran did this (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspermayone committed Jul 3, 2024
1 parent 536582f commit 5c3fb1d
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 24 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
"@prisma/client": "5.16.1",
"@slack/bolt": "^3.18.0",
"@slack/web-api": "^7.0.4",
"@types/bun": "^1.1.6",
"@types/express": "^4.17.21",
"airtable": "^0.12.2",
"async": "^3.2.5",
"axios": "^1.7.2",
"bottleneck": "^2.19.5",
"bun": "^1.1.17",
"colors": "^1.4.0",
"cron": "^3.1.7",
"dotenv": "^16.4.5",
Expand Down
21 changes: 21 additions & 0 deletions src/clients/hc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { SlackApp } from "slack-edge";

const isdev = process.env.NODE_ENV === "development";

const HCapp = new SlackApp({
env: {
SLACK_BOT_TOKEN: process.env.HC_SLACK_BOT_TOKEN!,
SLACK_APP_TOKEN: process.env.HC_SLACK_APP_TOKEN!,
SLACK_SIGNING_SECRET: process.env.HC_SLACK_SIGNING_SECRET!,
SLACK_LOGGING_LEVEL: isdev ? "DEBUG" : "INFO",
},
});

export { HCapp };

export default {
port: 3000,
async fetch(request: Request) {
return await HCapp.run(request);
},
};
14 changes: 14 additions & 0 deletions src/clients/pb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SlackApp } from "slack-edge";

const isdev = process.env.NODE_ENV === "development";

const PBapp = new SlackApp({
env: {
SLACK_BOT_TOKEN: process.env.PB_SLACK_BOT_TOKEN!,
SLACK_APP_TOKEN: process.env.PB_SLACK_APP_TOKEN!,
SLACK_SIGNING_SECRET: process.env.PB_SLACK_SIGNING_SECRET!,
SLACK_LOGGING_LEVEL: isdev ? "DEBUG" : "INFO",
},
});

export { PBapp };
93 changes: 69 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
import elysia from "elysia";
import { SlackAPIClient, SlackApp } from "slack-edge";
import { SlackAPIClient } from "slack-edge";

import { indexEndpoint } from "./endpoints";
import { healthEndpoint } from "./endpoints/health";
import { mirror } from "./functions/mirror";

const isdev = process.env.NODE_ENV === "development";
import { HCapp } from "./clients/hc";
import { PBapp } from "./clients/pb";

const PBapp = new SlackApp({
env: {
SLACK_BOT_TOKEN: process.env.PB_SLACK_BOT_TOKEN!,
SLACK_APP_TOKEN: process.env.PB_SLACK_APP_TOKEN!,
SLACK_SIGNING_SECRET: process.env.PB_SLACK_SIGNING_SECRET!,
SLACK_LOGGING_LEVEL: isdev ? "DEBUG" : "INFO",
},
});
const isdev = process.env.NODE_ENV === "development";
const version = require("../package.json").version;

const HCapp = new SlackApp({
env: {
SLACK_BOT_TOKEN: process.env.HC_SLACK_BOT_TOKEN!,
SLACK_APP_TOKEN: process.env.HC_SLACK_APP_TOKEN!,
SLACK_SIGNING_SECRET: process.env.HC_SLACK_SIGNING_SECRET!,
SLACK_LOGGING_LEVEL: isdev ? "DEBUG" : "INFO",
},
});
console.log(
"----------------------------------\nMagic Mirror Server\n----------------------------------\n"
);
console.log("🏗️ Starting Magic Mirror...");
console.log("📦 Loading Slack App...");
console.log("🔑 Loading environment variables...");

const PBclient: SlackAPIClient = PBapp.client;
const HCclient: SlackAPIClient = HCapp.client;

PBapp.anyMessage(async ({ payload, context }) => {
await mirror(PBclient, HCclient, payload);
});

HCapp.anyMessage(async ({ payload, context }) => {
await mirror(PBclient, HCclient, payload);
});
Expand All @@ -40,8 +29,64 @@ new elysia()
.get("/", () => indexEndpoint)
.get("/ping", () => healthEndpoint)
.get("/up", () => healthEndpoint)
.get("/pb", ({ request }) => PBapp.run(request))
.get("/hc", ({ request }) => HCapp.run(request))
.get("/pb", async ({ request }) => {
// read the readable stream as a string
const body = await request.text();
console.log("PB slack event received: ", JSON.parse(body).event.type);

// create a new request with the body as the request body
const newRequest = new Request(request.url, {
method: request.method,
headers: request.headers,
body,
});
return await PBapp.run(newRequest);
})
.post("/pb", async ({ request }) => {
// read the readable stream as a string
const body = await request.text();
console.log("PB slack event received: ", JSON.parse(body).event.type);

// create a new request with the body as the request body
const newRequest = new Request(request.url, {
method: request.method,
headers: request.headers,
body,
});
return await PBapp.run(newRequest);
})
.get("/hc", async ({ request }) => {
const body = await request.text();
console.log("HC slack event received: ", JSON.parse(body).event.type);

// create a new request with the body as the request body
const newRequest = new Request(request.url, {
method: request.method,
headers: request.headers,
body,
});
return await HCapp.run(newRequest);
})
.post("/hc", async ({ request }) => {
const body = await request.text();
console.log("HC slack event received: ", JSON.parse(body).event.type);

// create a new request with the body as the request body
const newRequest = new Request(request.url, {
method: request.method,
headers: request.headers,
body,
});
return await HCapp.run(newRequest);
})
.listen(3000);

console.log(
"🚀 Server Started in",
Bun.nanoseconds() / 1000000,
"milliseconds on version:",
version + "!",
"\n\n----------------------------------\n"
);

export { HCapp, HCclient, PBapp, PBclient };

0 comments on commit 5c3fb1d

Please sign in to comment.