diff --git a/manifest.yml b/manifest-hc.yml similarity index 66% rename from manifest.yml rename to manifest-hc.yml index 085b8c4..981c6e2 100644 --- a/manifest.yml +++ b/manifest-hc.yml @@ -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 diff --git a/manifest-pb.yml b/manifest-pb.yml new file mode 100644 index 0000000..add038e --- /dev/null +++ b/manifest-pb.yml @@ -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 diff --git a/ngrokconfig.yml b/ngrokconfig.yml new file mode 100644 index 0000000..55204d4 --- /dev/null +++ b/ngrokconfig.yml @@ -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 diff --git a/src/functions/mirror.ts b/src/functions/mirror.ts index 01499d6..34e5d12 100644 --- a/src/functions/mirror.ts +++ b/src/functions/mirror.ts @@ -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" || @@ -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"); diff --git a/src/index.ts b/src/index.ts index 30327e6..30912d9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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}`) ); }); @@ -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 }; diff --git a/src/util/Logger.ts b/src/util/Logger.ts index 7ac6913..f9e95fe 100644 --- a/src/util/Logger.ts +++ b/src/util/Logger.ts @@ -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"; @@ -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);