67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
import { loadConfig } from "../config/config.js";
|
|
import type { RuntimeEnv } from "../runtime.js";
|
|
import { createTelegramBot } from "./bot.js";
|
|
import { makeProxyFetch } from "./proxy.js";
|
|
import { resolveTelegramToken } from "./token.js";
|
|
import { startTelegramWebhook } from "./webhook.js";
|
|
|
|
export type MonitorTelegramOpts = {
|
|
token?: string;
|
|
runtime?: RuntimeEnv;
|
|
abortSignal?: AbortSignal;
|
|
useWebhook?: boolean;
|
|
webhookPath?: string;
|
|
webhookPort?: number;
|
|
webhookSecret?: string;
|
|
proxyFetch?: typeof fetch;
|
|
webhookUrl?: string;
|
|
};
|
|
|
|
export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) {
|
|
const { token } = resolveTelegramToken(loadConfig(), {
|
|
envToken: opts.token,
|
|
});
|
|
if (!token) {
|
|
throw new Error(
|
|
"TELEGRAM_BOT_TOKEN or telegram.botToken/tokenFile is required for Telegram gateway",
|
|
);
|
|
}
|
|
|
|
const proxyFetch =
|
|
opts.proxyFetch ??
|
|
(loadConfig().telegram?.proxy
|
|
? makeProxyFetch(loadConfig().telegram?.proxy as string)
|
|
: undefined);
|
|
|
|
const bot = createTelegramBot({
|
|
token,
|
|
runtime: opts.runtime,
|
|
proxyFetch,
|
|
});
|
|
|
|
if (opts.useWebhook) {
|
|
await startTelegramWebhook({
|
|
token,
|
|
path: opts.webhookPath,
|
|
port: opts.webhookPort,
|
|
secret: opts.webhookSecret,
|
|
runtime: opts.runtime as RuntimeEnv,
|
|
fetch: proxyFetch,
|
|
abortSignal: opts.abortSignal,
|
|
publicUrl: opts.webhookUrl,
|
|
});
|
|
return;
|
|
}
|
|
|
|
// Long polling
|
|
const stopOnAbort = () => {
|
|
if (opts.abortSignal?.aborted) void bot.stop();
|
|
};
|
|
opts.abortSignal?.addEventListener("abort", stopOnAbort, { once: true });
|
|
try {
|
|
await bot.start();
|
|
} finally {
|
|
opts.abortSignal?.removeEventListener("abort", stopOnAbort);
|
|
}
|
|
}
|