import { Main } from './Main'; const PREFIX = '[Twilio]'; export class TwilioHandler { private _Main: Main; client; constructor(Main: Main) { this._Main = Main; } load() { this.client = require('twilio')( this._Main.Config.twilio.accountSid, this._Main.Config.twilio.authToken ); } async resetError(category: TwilioCategories) { if (!this.lastErrors.has(category)) return; this.lastErrors.delete(category); } private lastErrors: Map = new Map(); private errorLog: string[] = []; private errorTimeout: NodeJS.Timeout; async sendError(category: TwilioCategories, error: string) { if (this.lastErrors.get(category) === error) return; this.lastErrors.set(category, error); if (error == null) return; this.errorLog.push(`${category}: ${error}`); clearTimeout(this.errorTimeout); this.errorTimeout = setTimeout(async () => { const errorMessage = this.errorLog .map((error) => `- ${error}`) .join('\n'); this.errorLog = []; console.log(PREFIX, `Sending to Twilio:\n`, errorMessage); const promises = this._Main.Config.twilio.toNumbers.map( (toNumber) => this.sendMessage(toNumber, errorMessage) ); await Promise.all(promises); }, 15000); } sendMessage(to: string, message: string) { return new Promise((resolve) => { this.client.messages .create({ body: message, from: `${this._Main.Config.twilio.fromNumber}`, to: to, }) .then((message: any) => { console.log(`Twilio message sent with SID: ${message.sid}`); resolve(true); }) .catch((error: any) => { console.error('Error sending Twilio message:', error); resolve(false); }); }); } } type TwilioCategories = 'CameraRunner' | 'UnityRunner' | 'UnityWebSocket';