-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from Decatur-Robotics/slack-for-other-teams
- Loading branch information
Showing
13 changed files
with
3,461 additions
and
536 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { join } from "path"; | ||
import { SlashCommand, AckFn, RespondArguments, RespondFn } from '@slack/bolt'; | ||
import { createServer } from "https"; | ||
import { parse } from "url"; | ||
import next from "next"; | ||
import fs from "fs"; | ||
import { App } from "@slack/bolt"; | ||
import SlackCommands from "./lib/SlackCommands"; | ||
import { IncomingMessage, ServerResponse } from "http"; | ||
import { WebClientEvent } from "@slack/web-api"; | ||
|
||
console.log("Starting server..."); | ||
|
||
const dev = process.env.NODE_ENV !== "production"; | ||
const port = 443; | ||
const app = next({ dev }); | ||
const handle = app.getRequestHandler(); | ||
|
||
console.log("Constants set"); | ||
|
||
const httpsOptions = { | ||
key: dev | ||
? fs.readFileSync("./certs/localhost-key.pem") | ||
: fs.readFileSync("./certs/production-key.pem"), | ||
cert: dev | ||
? fs.readFileSync("./certs/localhost.pem") | ||
: fs.readFileSync("./certs/production.pem"), | ||
}; | ||
|
||
console.log("HTTPS options set"); | ||
|
||
app.prepare().then(() => { | ||
console.log("App prepared. Creating server..."); | ||
|
||
try { | ||
const server = createServer(httpsOptions, async (req: IncomingMessage, res: ServerResponse<IncomingMessage>) => { | ||
if (!req.url) | ||
return; | ||
|
||
const parsedUrl = parse(req.url, true); | ||
const { pathname } = parsedUrl; | ||
|
||
if (pathname && (pathname === '/sw.js' || /^\/(workbox|worker|fallback)-\w+\.js$/.test(pathname))) { | ||
const filePath = join(__dirname, '.next', pathname); | ||
(app as any).serveStatic(req, res, filePath); | ||
} else { | ||
handle(req, res, parsedUrl); | ||
} | ||
}).listen(port, () => { | ||
console.log( | ||
process.env.NODE_ENV + | ||
" HTTPS Server Running At: https://localhost:" + | ||
port, | ||
); | ||
}).on("error", (err: Error) => { | ||
console.log(err); | ||
throw err; | ||
}); | ||
|
||
console.log("Server created. Listening: " + server.listening); | ||
} catch (err) { | ||
console.log(err); | ||
throw err; | ||
} | ||
}); | ||
|
||
console.log("App preparing..."); | ||
|
||
// Slack bot | ||
|
||
const slackApp = new App({ | ||
token: process.env.SLACK_BOT_TOKEN, | ||
signingSecret: process.env.SLACK_SIGNING_SECRET, | ||
socketMode: true, | ||
appToken: process.env.SLACK_APP_TOKEN, | ||
}); | ||
|
||
slackApp.command(/\/.*/, async (props: { command: SlashCommand, ack: AckFn<string | RespondArguments>, respond: RespondFn }) => { | ||
const { command, ack, respond } = props; | ||
|
||
const commandName = command.command.replace("/", ""); | ||
const handler = SlackCommands[commandName]; | ||
|
||
if (handler) { | ||
handler(command, ack, respond); | ||
} | ||
else { | ||
await ack(); | ||
await respond(`Command not found: ` + commandName); | ||
} | ||
}); | ||
|
||
async function startSlackApp() { | ||
await slackApp.start(port); | ||
console.log("Slack bot is running!"); | ||
} | ||
startSlackApp(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { AckFn, RespondArguments, RespondFn, SlashCommand } from "@slack/bolt"; | ||
import { Db, ObjectId } from "mongodb"; | ||
import { Collections, getDatabase } from "./MongoDB"; | ||
import { Team, User } from "./Types"; | ||
|
||
type SlackCommandDict = { | ||
[command: string]: | ||
(command: SlashCommand, acknowledge: AckFn<string | RespondArguments>, respond: RespondFn) => Promise<void> | ||
}; | ||
|
||
const SlackCommands: SlackCommandDict = { | ||
"link-notifications": async (command, acknowledge, respond) => { | ||
await acknowledge(); | ||
|
||
if (!command.text || isNaN(+command.text)) { | ||
await respond("Please provide a team number."); | ||
return; | ||
} | ||
|
||
const teamNumber = +command.text; | ||
const userId = command.user_id; | ||
|
||
const db = await getDatabase(); | ||
|
||
const userPromise = db.findObject<User>(Collections.Users, { slackId: userId }); | ||
const teamPromise = db.findObject<Team>(Collections.Teams, { number: teamNumber }); | ||
|
||
const user = await userPromise; | ||
const team = await teamPromise; | ||
|
||
console.log(user, team); | ||
|
||
if (!user) { | ||
await respond("You are not registered in Gearbox. Please register first at https://4026.org."); | ||
return; | ||
} | ||
|
||
if (!team) { | ||
await respond(`Team ${teamNumber} does not exist.`); | ||
return; | ||
} | ||
|
||
if (!team.owners.includes(user._id!.toString())) { | ||
await respond(`You are not an owner of team ${teamNumber}.`); | ||
return; | ||
} | ||
|
||
await db.updateObjectById<Team>(Collections.Teams, new ObjectId(team._id!), { | ||
slackChannel: command.channel_id | ||
}); | ||
|
||
db.client?.close(); | ||
await respond(`Linked channel for team ${teamNumber}. Make sure to run /invite @Gearbox to the channel to receive notifications.`); | ||
} | ||
} | ||
|
||
export default SlackCommands; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.