Add Bun bundle docs and Telegram grammY support
This commit is contained in:
63
src/telegram/monitor.ts
Normal file
63
src/telegram/monitor.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { Bot } from "grammy";
|
||||
|
||||
import { loadConfig } from "../config/config.js";
|
||||
import type { RuntimeEnv } from "../runtime.js";
|
||||
import { createTelegramBot, createTelegramWebhookCallback } from "./bot.js";
|
||||
import { makeProxyFetch } from "./proxy.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 = (opts.token ?? process.env.TELEGRAM_BOT_TOKEN)?.trim();
|
||||
if (!token) {
|
||||
throw new Error("TELEGRAM_BOT_TOKEN or telegram.botToken is required for Telegram relay");
|
||||
}
|
||||
|
||||
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) bot.stop();
|
||||
};
|
||||
opts.abortSignal?.addEventListener("abort", stopOnAbort, { once: true });
|
||||
try {
|
||||
await bot.start();
|
||||
} finally {
|
||||
opts.abortSignal?.removeEventListener("abort", stopOnAbort);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user