UNPKG

@intuitionrobotics/thunderstorm

Version:
63 lines 2.63 kB
import { currentTimeMillies, Minute, Module, ServerErrorSeverity, ServerErrorSeverity_Ordinal } from "@intuitionrobotics/ts-common"; import { WebClient } from "@slack/web-api"; export class SlackModule_Class extends Module { // Created lazily on the first posted message (was init()); stays undefined // when no token is configured, same as before. _web; messageMap = {}; constructor() { super("SlackModule"); } get web() { if (!this.config.token) return undefined; return this._web ??= new WebClient(this.config.token, { rejectRateLimitedCalls: true, ...this.config.slackConfig }); } async postMessage(slackMessage) { const parameters = typeof slackMessage === 'string' ? { text: slackMessage, channel: this.config.defaultChannel } : slackMessage; const time = this.messageMap[parameters.text]; if (time && currentTimeMillies() - time < (this.config.throttlingTime || Minute)) return; try { return await this.postMessageImpl(parameters); } catch (e) { this.logError(`Error while sending a message to channel: ${parameters.channel}`, e); } } async postMessageImpl(message) { const web = this.web; if (!web) { // previously a silent NPE when no token was configured — now a logged no-op this.logWarning(`Slack token not configured — dropping message to channel: ${message.channel}`); return; } const res = await web.chat.postMessage(message); this.messageMap[message.text] = currentTimeMillies(); this.logDebug(`A message was posted to channel: ${message.channel} with message id ${res.ts} which contains the message ${message.text}`); } } export const SlackModule = new SlackModule_Class(); export class Slack_ServerApiError_Class extends Module { constructor() { super("Slack_ServerApiError"); this.setDefaultConfig({ exclude: [], minLevel: ServerErrorSeverity.Info }); } async __processApplicationError(errorLevel, message) { if (ServerErrorSeverity_Ordinal.indexOf(errorLevel) < ServerErrorSeverity_Ordinal.indexOf(this.config.minLevel)) return; for (const key of this.config.exclude || []) { if (message.includes(key)) return; } await SlackModule.postMessage(`\`\`\`${message}\`\`\``); } } export const Slack_ServerApiError = new Slack_ServerApiError_Class(); //# sourceMappingURL=SlackModule.js.map