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

Commit

Permalink
mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspermayone committed Jul 2, 2024
1 parent 4b3597b commit 9a93fb5
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 107 deletions.
9 changes: 8 additions & 1 deletion manifest.yml → manifest-hc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ features:
oauth_config:
scopes:
bot:
- app_mentions:read
- channels:history
- channels:join
- app_mentions:read
- channels:read
- chat:write
- chat:write.customize
- chat:write.public
- groups:history
- users.profile:read
settings:
event_subscriptions:
request_url: https://0ab4-65-19-76-238.ngrok-free.app/slack/events
bot_events:
- message.channels
- message.groups
org_deploy_enabled: true
socket_mode_enabled: false
token_rotation_enabled: false
27 changes: 27 additions & 0 deletions manifest-pb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
display_information:
name: Magic Mirror
features:
bot_user:
display_name: Magic Mirror
always_online: false
oauth_config:
scopes:
bot:
- app_mentions:read
- channels:history
- channels:join
- channels:read
- chat:write
- chat:write.customize
- chat:write.public
- groups:history
- users.profile:read
settings:
event_subscriptions:
request_url: https://47d4-65-19-76-238.ngrok-free.app/slack/events
bot_events:
- message.channels
- message.groups
org_deploy_enabled: true
socket_mode_enabled: false
token_rotation_enabled: false
9 changes: 9 additions & 0 deletions ngrokconfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tunnels:
pbreciver:
proto: http
addr: 3000
# domain: heron-selected-literally.ngrok-free.app
hcreciver:
proto: http
addr: 3001
# domain: demo.inconshreveable.com
134 changes: 53 additions & 81 deletions src/functions/mirror.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { blog } from "../util/Logger";

export async function mirror(client, message) {
let hcTeam = "T0266FRGM";
let hcChannel_Test1 = "C069N64PW4A";
let pbTeam = "T07986PHP2R";
let pbChannel_MirrorTest1 = "C07ASSJGE2G";
let pbChannel_MirrorTest2 = "C07AHPB65P0";

export async function mirror(pbClient, hcClient, message) {
try {
if (
message.subtype === "bot_message" ||
Expand All @@ -10,104 +16,70 @@ export async function mirror(client, message) {
return;
}

console.log(message);

let channel = message.channel;
// let mirrorTest1 = "C07ASSJGE2G";
// let mirrorTest2 = "C07AHPB65P0";

// get user profile
let userProfile = await client.users.profile.get({
user: message.user,
});
let messageTeam = message.team!;
let messageChannel = message.channel!;

let profile = userProfile.profile;
let userpfp = profile.image_512;
let userRealName = profile.real_name;
if (messageTeam === pbTeam) {
let userProfile = await pbClient.users.profile.get({
user: message.user,
});

let channels = {
mirrorTest1: "C07ASSJGE2G",
mirrorTest2: "C07AHPB65P0",
};
let profile = userProfile.profile!;
let userpfp = profile.image_512!;
let userRealName = profile.real_name!;

let teams = {
purpleBubble: "T07986PHP2R",
hackClub: "",
};

let channelMapings = [
{
originChannel: channels.mirrorTest1,
originTeam: teams.purpleBubble,
mirrorChannel: channels.mirrorTest2,
mirrorTeam: teams.purpleBubble,
},
{
originChannel: channels.mirrorTest2,
originTeam: teams.purpleBubble,
mirrorChannel: channels.mirrorTest1,
mirrorTeam: teams.purpleBubble,
},
];
switch (messageChannel) {
case pbChannel_MirrorTest1:
hcClient.chat.postMessage({
username: userRealName,
icon_url: userpfp,
channel: hcChannel_Test1,
text: message.text,
blocks: message.blocks,
});

let channelsFlat = Object.values(channels);
break;

if (channelsFlat.includes(channel)) {
switch (channel) {
case channels.mirrorTest1:
let msg1 = client.chat.postMessage({
team: channelMapings.find((c) => c.originChannel === channel)!
.mirrorTeam,
case pbChannel_MirrorTest2:
hcClient.chat.postMessage({
username: userRealName,
icon_url: userpfp,
channel: channelMapings.find((c) => c.originChannel === channel)!
.mirrorChannel,
channel: hcChannel_Test1,
text: message.text,
blocks: message.blocks,
});

// await prisma.message.create({
// data: {
// user: message.user,
// originTs: message.ts,
// originTeam: message.team,
// originChannel: message.channel,
// mirrorTs: msg1.ts,
// mirrorTeam: msg1.team,
// mirrorChannel: msg1.channel,
// text: message.text,
// blocks: message.blocks,
// },
// });

break;
case channels.mirrorTest2:
let msg2 = client.chat.postMessage({
team: channelMapings.find((c) => c.originChannel === channel)!
.mirrorTeam,

default:
return;
}
} else if (messageTeam === hcTeam) {
let userProfile = await hcClient.users.profile.get({
user: message.user,
});

let profile = userProfile.profile!;
let userpfp = profile.image_512!;
let userRealName = profile.real_name!;

switch (messageChannel) {
case hcChannel_Test1:
pbClient.chat.postMessage({
username: userRealName,
icon_url: userpfp,
// set channel to be the mirror channel from the channelMappings array
channel: channelMapings.find((c) => c.originChannel === channel)!
.mirrorChannel,
channel: pbChannel_MirrorTest1,
text: message.text,
blocks: message.blocks,
});

// await prisma.message.create({
// data: {
// user: message.user,
// originTs: message.ts,
// originTeam: message.team,
// originChannel: message.channel,
// mirrorTs: msg2.ts,
// mirrorTeam: msg2.team,
// mirrorChannel: msg2.channel,
// text: message.text,
// blocks: message.blocks,
// },
// });

break;

default:
return;
}
} else {
return;
}
} catch (error) {
blog(`Error responding to message: ${error}`, "error");
Expand Down
69 changes: 46 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,64 @@ import express from "express";
import { indexEndpoint } from "./endpoints";
import { healthEndpoint } from "./endpoints/health";
import { mirror } from "./functions/mirror";
import { t } from "./lib/templates";
import { slog } from "./util/Logger";

const prisma = new PrismaClient();

const receiver = new ExpressReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET!,
const PBreceiver = new ExpressReceiver({
signingSecret: process.env.PB_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 HCreceiver = new ExpressReceiver({
signingSecret: process.env.HC_SLACK_SIGNING_SECRET!,
});

app.message(async ({ message, say }) => {
await mirror(app.client, message);
const PBapp = new App({
token: process.env.PB_SLACK_BOT_TOKEN!,
appToken: process.env.PB_SLACK_APP_TOKEN!,
signingSecret: process.env.PB_SLACK_SIGNING_SECRET!,
receiver: PBreceiver,
});

receiver.router.use(express.json());
receiver.router.get("/", indexEndpoint);
receiver.router.get("/ping", healthEndpoint);
receiver.router.get("/up", healthEndpoint);
const HCapp = new App({
token: process.env.HC_SLACK_BOT_TOKEN!,
appToken: process.env.HC_SLACK_APP_TOKEN!,
signingSecret: process.env.HC_SLACK_SIGNING_SECRET!,
receiver: HCreceiver,
});

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

PBapp.message(async ({ message, say }) => {
await mirror(PBclient, HCclient, message);
});

HCapp.message(async ({ message, say }) => {
await mirror(PBclient, HCclient, message);
});

const logStartup = async (app: App) => {
let env = process.env.NODE_ENV;
slog(t("app.startup", { environment: env }), "info");
};
PBreceiver.router.use(express.json());
PBreceiver.router.get("/", indexEndpoint);
PBreceiver.router.get("/ping", healthEndpoint);
PBreceiver.router.get("/up", healthEndpoint);

HCreceiver.router.use(express.json());
HCreceiver.router.get("/", indexEndpoint);
HCreceiver.router.get("/ping", healthEndpoint);
HCreceiver.router.get("/up", healthEndpoint);

PBapp.start(3000).then(async () => {
await slog("PB Bolt app is running", "startup");
console.log(
colors.bgCyan(`⚡️ PB Bolt app is running in env ${process.env.NODE_ENV}`)
);
});

app.start(process.env.PORT || 3000).then(async () => {
await logStartup(app);
HCapp.start(3001).then(async () => {
await slog("HC Bolt app is running", "startup");
console.log(
colors.bgCyan(`⚡️ Bolt app is running in env ${process.env.NODE_ENV}`)
colors.bgCyan(`⚡️ HC Bolt app is running in env ${process.env.NODE_ENV}`)
);
});

Expand All @@ -57,5 +81,4 @@ app.start(process.env.PORT || 3000).then(async () => {
// "America/New_York"
// );

const client: any = app.client;
export { app, client, prisma };
export { HCapp, HCclient, PBapp, PBclient, prisma };
4 changes: 2 additions & 2 deletions src/util/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as dotenv from "dotenv";
dotenv.config();

import { client } from "../index";
import { PBclient } from "../index";

import async from "async";
import Bottleneck from "bottleneck";
Expand All @@ -14,7 +14,7 @@ const limiter = new Bottleneck({

const messageQueue = async.queue(async (task, callback) => {
try {
await limiter.schedule(() => client.chat.postMessage(task));
await limiter.schedule(() => PBclient.chat.postMessage(task));
callback();
} catch (error) {
console.error("Error posting message:", error);
Expand Down

0 comments on commit 9a93fb5

Please sign in to comment.