-
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.
- Loading branch information
1 parent
04dc191
commit 50cc41b
Showing
17 changed files
with
320 additions
and
65 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
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,130 @@ | ||
import { WebClient } from "@slack/web-api"; | ||
import { postMessage } from "../utils/slack"; | ||
import * as environment from "../utils/env"; | ||
import { loggingChannel } from "../common/constants"; | ||
import { App } from "@slack/bolt"; | ||
|
||
/** | ||
* The levels of logging that can be used | ||
*/ | ||
export enum LogLevel { | ||
INFO = "INFO", | ||
WARNING = "WARN", | ||
ERROR = "ERROR", | ||
} | ||
|
||
/** | ||
* A class to handle logging to Slack | ||
*/ | ||
export class SlackLogger { | ||
private static instance: SlackLogger; | ||
slackClient: WebClient; | ||
|
||
/** | ||
* Singleton constructor for SlackLogger | ||
* @param slackClient The Slack Web API client to use for logging | ||
*/ | ||
private constructor(slackClient: WebClient) { | ||
this.slackClient = slackClient; | ||
} | ||
|
||
/** | ||
* Get the singleton instance of SlackLogger | ||
* @returns The singleton instance of SlackLogger | ||
*/ | ||
static getInstance(): SlackLogger { | ||
if (!SlackLogger.instance) { | ||
const app = new App({ | ||
token: environment.slackBotToken, | ||
signingSecret: environment.slackSigningSecret, | ||
socketMode: true, | ||
appToken: environment.slackAppToken, | ||
}); | ||
SlackLogger.instance = new SlackLogger(app.client); | ||
} | ||
return SlackLogger.instance; | ||
} | ||
|
||
/** | ||
* Log a message to the minerva logging channel in Slack | ||
* @param level The level of the log | ||
* @param message The message to log | ||
* @param codeBlockContent The content of the code block to log after the message, if any. | ||
* @param logToConsole Whether to log the message to the console in addition to Slack | ||
*/ | ||
private async log( | ||
level: LogLevel, | ||
message: string, | ||
codeBlockContent: unknown = "", | ||
logToConsole = true, | ||
): Promise<void> { | ||
const formattedMessage = this.formatMessage(level, message); | ||
|
||
if (logToConsole) { | ||
if (level === LogLevel.ERROR) { | ||
console.error(formattedMessage); | ||
console.error(codeBlockContent); | ||
} else { | ||
console.log(formattedMessage); | ||
console.log(codeBlockContent); | ||
} | ||
} | ||
|
||
let formattedSlackMessage = formattedMessage; | ||
|
||
if (codeBlockContent) { | ||
formattedSlackMessage += "\n" + SlackLogger.formatCodeBlockContent(codeBlockContent); | ||
} | ||
|
||
await postMessage(this.slackClient, loggingChannel, formattedSlackMessage); | ||
} | ||
|
||
/** | ||
* Log an info message to Slack | ||
* @param message The message to log | ||
* @param codeBlockContent The content of the code block to log after the message, if any. | ||
* @param logToConsole Whether to log the message to the console in addition to Slack | ||
*/ | ||
async info(message: string, codeBlockContent: unknown = "", logToConsole = true): Promise<void> { | ||
await this.log(LogLevel.INFO, message, codeBlockContent, logToConsole); | ||
} | ||
/** | ||
* Log a warning message to Slack | ||
* @param message The message to log | ||
* @param codeBlockContent The content of the code block to log after the message, if any. | ||
* @param logToConsole Whether to log the message to the console in addition to Slack | ||
*/ | ||
async warning(message: string, codeBlockContent: unknown = "", logToConsole = true): Promise<void> { | ||
await this.log(LogLevel.WARNING, message, codeBlockContent, logToConsole); | ||
} | ||
|
||
/** | ||
* Log an error message to Slack | ||
* @param message The message to log | ||
* @param codeBlockContent The content of the code block to log after the message, if any. Can be used to log the error content | ||
* @param logToConsole Whether to log the message to the console in addition to Slack | ||
*/ | ||
async error(message: string, codeBlockContent: unknown = "", logToConsole = true): Promise<void> { | ||
await this.log(LogLevel.ERROR, message, codeBlockContent, logToConsole); | ||
} | ||
|
||
/** | ||
* Formats the message into a log line | ||
* @param level The message level | ||
* @param message The message to log | ||
* @returns The formatted log line | ||
*/ | ||
private formatMessage(level: LogLevel, message: string): string { | ||
const timeStamp = new Date().toISOString(); | ||
return `[${timeStamp}] [${level}] ${message}`; | ||
} | ||
|
||
/** | ||
* Formats code block content for Slack | ||
* @param codeBlockContent The content of the code block | ||
* @returns The formatted code block content | ||
*/ | ||
private static formatCodeBlockContent(codeBlockContent: unknown): string { | ||
return `\`\`\`${codeBlockContent}\`\`\``; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,12 @@ | ||
import { slackBotToken } from "../../utils/env"; | ||
import { Middleware, SlackCommandMiddlewareArgs } from "@slack/bolt"; | ||
import { logCommandUsed } from "../../utils/logging"; | ||
import { postEphemeralMessage } from "../../utils/slack"; | ||
|
||
const message = `For more information, check out <https://github.com/waterloo-rocketry/minerva-rewrite/blob/main/README.md|minerva's README>.`; | ||
|
||
export const helpCommandHandler: Middleware<SlackCommandMiddlewareArgs> = async ({ command, ack, client }) => { | ||
await ack(); | ||
await logCommandUsed(command); | ||
|
||
await client.chat.postEphemeral({ | ||
token: slackBotToken, | ||
channel: command.channel_id, | ||
user: command.user_id, | ||
text: message, | ||
}); | ||
await postEphemeralMessage(client, command.channel_id, command.user_id, message); | ||
}; |
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
Oops, something went wrong.