From 2dc81216bd5f1dc437f296d356cc501569fb5c2d Mon Sep 17 00:00:00 2001 From: ender Date: Mon, 6 May 2024 20:24:07 +0200 Subject: [PATCH] broadcast refactor --- src/bot.ts | 88 ++++++++++++++++++++++++++++++++++++++++++++++----- src/config.ts | 8 +++-- src/main.ts | 55 +++----------------------------- 3 files changed, 91 insertions(+), 60 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 30f2f85..43bc04d 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -10,6 +10,7 @@ import { Parameter, Translation, User, + WSBroadcast, WSCommand, WSCommandPayload, WSMessage, @@ -32,7 +33,7 @@ import { WebSocket } from 'ws'; import * as plugins from './plugins/index'; import * as cron from 'node-cron'; import { Actions } from './actions'; -import { db } from './main'; +import { bots, db, wss } from './main'; export class Bot { platform: string; @@ -433,6 +434,56 @@ export class Bot { this.sendMessage(msg.conversation, content, type, reply, extra); } + sendBroadcast(json: WSBroadcast): void { + const broadcast: WSBroadcast = json; + const conversation = broadcast.message.conversation; + if (conversation.id === 'alerts') { + conversation.id = this.config.alertsConversationId; + conversation.title = 'Alerts'; + } else if (conversation.id === 'admin') { + conversation.id = this.config.adminConversationId; + conversation.title = 'Admin'; + } else if (conversation.id === 'owner') { + conversation.id = this.config.owner; + conversation.title = 'Owner'; + } + const message: WSMessage = { + bot: broadcast.bot, + platform: broadcast.platform, + type: 'message', + message: new Message( + null, + conversation, + this.user, + broadcast.message.content, + broadcast.message.type, + now(), + null, + broadcast.message.extra, + ), + }; + if (Array.isArray(broadcast.target)) { + this.broadcastHandler(broadcast.message); + broadcast.target.forEach((target) => { + if (bots[target]) { + bots[target].send(JSON.stringify(message)); + } + }); + } else if (broadcast.target === '*' || broadcast.target === 'all') { + wss.clients.forEach((client) => { + //if (client !== ws) { + this.broadcastHandler(broadcast.message); + client.send(JSON.stringify(message)); + //} + }); + } else { + this.broadcastHandler(broadcast.message); + if (bots[broadcast.target]) { + bots[broadcast.target].send(JSON.stringify(message)); + } + } + } + sendAlert(text: string, language = 'javascript'): void { if ( this.config.alertsConversationId && @@ -440,15 +491,24 @@ export class Bot { ) { const message = new Message( null, - new Conversation(this.config.alertsConversationId, 'Alerts'), + new Conversation('alerts'), this.user, - `${text}`, + `${this.user.firstName} (@${this.user.username}) [${this.user.id}]\n${text}`, 'text', null, null, { format: 'HTML', preview: false }, ); - this.send(message); + const broadcast: WSBroadcast = { + bot: this.config.name, + target: this.config.alertsTarget, + platform: this.config.alertsPlatform, + type: 'broadcast', + message, + }; + + //this.send(message); + this.sendBroadcast(broadcast); } } @@ -459,15 +519,27 @@ export class Bot { ) { const message = new Message( null, - new Conversation(this.config.adminConversationId, 'Admin'), + new Conversation('admin'), this.user, - text, + `${this.user.firstName} (@${this.user.username}) [${this.user.id}]\n${text}`, 'text', null, null, - { format: 'HTML', preview: false }, + { + format: 'HTML', + preview: false, + }, ); - this.send(message); + const broadcast: WSBroadcast = { + bot: this.config.name, + target: this.config.alertsTarget, + platform: this.config.alertsPlatform, + type: 'broadcast', + message, + }; + + //this.send(message); + this.sendBroadcast(broadcast); } } } diff --git a/src/config.ts b/src/config.ts index 1ad0a11..82ec16e 100644 --- a/src/config.ts +++ b/src/config.ts @@ -11,8 +11,10 @@ export class Config { plugins?: string | string[]; excludedPlugins?: string[]; translation?: string; - adminConversationId?: string; + alertsPlatform?: string; + alertsTarget?: string; alertsConversationId?: string; + adminConversationId?: string; apiKeys?: ApiKeys; constructor() { @@ -24,8 +26,10 @@ export class Config { this.plugins = '*'; this.excludedPlugins = []; this.translation = 'default'; - this.adminConversationId = null; + this.alertsPlatform = null; + this.alertsTarget = null; this.alertsConversationId = null; + this.adminConversationId = null; this.apiKeys = { telegramBotToken: null, telegramPhoneNumber: null, diff --git a/src/main.ts b/src/main.ts index d6ec2e8..75b85aa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,11 +1,11 @@ import { WebSocketServer, WebSocket } from 'ws'; -import { BotSocket, Message, MongoDatabases, WSBroadcast, WSInit, WSMessage, WSPong } from './types'; -import { catchException, logger, now } from './utils'; +import { BotSocket, MongoDatabases, WSInit, WSMessage, WSPong } from './types'; +import { catchException, logger } from './utils'; import { Bot } from './bot'; import { MongoClient } from 'mongodb'; let mongo: MongoClient; -const wss: WebSocketServer = new WebSocketServer({ port: 8080 }); +export const wss: WebSocketServer = new WebSocketServer({ port: 8080 }); const close = () => { logger.info(`🟡 Closing connection for ${wss.clients.size} client(s)...`); @@ -30,7 +30,7 @@ process.on('exit', () => { logger.info('❎ Exit process'); }); -const bots: BotSocket = {}; +export const bots: BotSocket = {}; export const db: MongoDatabases = {}; const start = () => { @@ -83,42 +83,7 @@ const start = () => { }; ws.send(JSON.stringify(pong)); } else if (json.type === 'broadcast') { - const broadcast: WSBroadcast = json; - const message: WSMessage = { - bot: broadcast.bot, - platform: broadcast.platform, - type: 'message', - message: new Message( - null, - broadcast.message.conversation, - bot.user, - broadcast.message.content, - broadcast.message.type, - now(), - null, - broadcast.message.extra, - ), - }; - if (Array.isArray(broadcast.target)) { - bot.broadcastHandler(broadcast.message); - broadcast.target.forEach((target) => { - if (bots[target]) { - bots[target].send(JSON.stringify(message)); - } - }); - } else if (broadcast.target === '*' || broadcast.target === 'all') { - wss.clients.forEach((client) => { - if (client !== ws) { - bot.broadcastHandler(broadcast.message); - client.send(JSON.stringify(message)); - } - }); - } else { - bot.broadcastHandler(broadcast.message); - if (bots[broadcast.target]) { - bots[broadcast.target].send(JSON.stringify(message)); - } - } + bot.sendBroadcast(json); } else { logger.warning(`Unsupported data: ${data}`); } @@ -139,13 +104,3 @@ MongoClient.connect(process.env.MONGODB_URI, { await start(); }); - -/*export const db = new Database(); -db.events.once('loaded', async () => { - await start(); - db.events.on('update:configs', async () => { - await start(); - }); -}); -db.init(); -*/