merlyn/server/utils/boot/index.js
Sean Hatfield 192ca411f2
Telegram bot connector (#5190)
* wip telegram bot connector

* encrypt bot token, reorg telegram bot modules, secure pairing codes

* offload telegram chat to background worker, add @agent support with chart png rendering, reconnect ui

* refactor telegram bot settings page into subcomponents

* response.locals for mum, telemetry for connecting to telegram

* simplify telegram command registration

* improve telegram bot ux: rework switch/history/resume commands

* add voice, photo, and TTS support to telegram bot with long message handling

* lint

* rename external_connectors to external_communication_connectors, add voice response mode, persist chat workspace/thread selection

* lint

* fix telegram bot connect/disconnect bugs, kill telegram bot on multiuser mode enable

* add english translations

* fix qr code in light mode

* repatch migration

* WIP checkpoint

* pipeline overhaul for using response obj

* format functions

* fix comment block

* remove conditional dumpENV + lint

* remove .end() from sendStatus calls

* patch broken streaming where streaming only first chunk

* refactor

* use Ephemeral handler now

* show metrics and citations in real GUI

* bugfixes

* prevent MuM persistence, UI cleanup, styling for status

* add new workspace flow in UI
Add thread chat count
fix 69 byte payload callback limit bug

* handle pagination for workspaces, threads, and models

* modularize commands and navigation

* add /proof support for citation recall

* handle backlog message spam

* support abort of response streams

* code cleanup

* spam prevention

* fix translations, update voice typing indicator, fix token bug

* frontend refactor, update tips on /status and voice response improvements

* collapse agent though blocks

* support images

* Fix mime issues with audio from other devices

* fix config issue post server stop

* persist image on agentic chats

* 5189 i18n (#5245)

* i18n translations
connect #5189

* prune translations

* fix errors

* fix translation gaps

---------

Co-authored-by: Timothy Carambat <rambat1010@gmail.com>
2026-03-23 15:10:21 -07:00

97 lines
3.4 KiB
JavaScript

const { Telemetry } = require("../../models/telemetry");
const { BackgroundService } = require("../BackgroundWorkers");
const { EncryptionManager } = require("../EncryptionManager");
const { CommunicationKey } = require("../comKey");
const setupTelemetry = require("../telemetry");
const eagerLoadContextWindows = require("./eagerLoadContextWindows");
const markOnboarded = require("./markOnboarded");
const { PushNotifications } = require("../PushNotifications");
const { TelegramBotService } = require("../telegramBot");
// Testing SSL? You can make a self signed certificate and point the ENVs to that location
// make a directory in server called 'sslcert' - cd into it
// - openssl genrsa -aes256 -passout pass:gsahdg -out server.pass.key 4096
// - openssl rsa -passin pass:gsahdg -in server.pass.key -out server.key
// - rm server.pass.key
// - openssl req -new -key server.key -out server.csr
// Update .env keys with the correct values and boot. These are temporary and not real SSL certs - only use for local.
// Test with https://localhost:3001/api/ping
// build and copy frontend to server/public with correct API_BASE and start server in prod model and all should be ok
function bootSSL(app, port = 3001) {
try {
console.log(
`\x1b[33m[SSL BOOT ENABLED]\x1b[0m Loading the certificate and key for HTTPS mode...`
);
const fs = require("fs");
const https = require("https");
const privateKey = fs.readFileSync(process.env.HTTPS_KEY_PATH);
const certificate = fs.readFileSync(process.env.HTTPS_CERT_PATH);
const credentials = { key: privateKey, cert: certificate };
const server = https.createServer(credentials, app);
server
.listen(port, async () => {
await markOnboarded();
await setupTelemetry();
new CommunicationKey(true);
new EncryptionManager();
new BackgroundService().boot();
await eagerLoadContextWindows();
await PushNotifications.setupPushNotificationService();
await TelegramBotService.bootIfActive();
console.log(`Primary server in HTTPS mode listening on port ${port}`);
})
.on("error", catchSigTerms);
require("@mintplex-labs/express-ws").default(app, server);
return { app, server };
} catch (e) {
console.error(
`\x1b[31m[SSL BOOT FAILED]\x1b[0m ${e.message} - falling back to HTTP boot.`,
{
ENABLE_HTTPS: process.env.ENABLE_HTTPS,
HTTPS_KEY_PATH: process.env.HTTPS_KEY_PATH,
HTTPS_CERT_PATH: process.env.HTTPS_CERT_PATH,
stacktrace: e.stack,
}
);
return bootHTTP(app, port);
}
}
function bootHTTP(app, port = 3001) {
if (!app) throw new Error('No "app" defined - crashing!');
app
.listen(port, async () => {
await markOnboarded();
await setupTelemetry();
new CommunicationKey(true);
new EncryptionManager();
new BackgroundService().boot();
await eagerLoadContextWindows();
await PushNotifications.setupPushNotificationService();
await TelegramBotService.bootIfActive();
console.log(`Primary server in HTTP mode listening on port ${port}`);
})
.on("error", catchSigTerms);
return { app, server: null };
}
function catchSigTerms() {
process.once("SIGUSR2", function () {
Telemetry.flush();
process.kill(process.pid, "SIGUSR2");
});
process.on("SIGINT", function () {
Telemetry.flush();
process.kill(process.pid, "SIGINT");
});
}
module.exports = {
bootHTTP,
bootSSL,
};