2026-01-09 13:38:51 +01:00
|
|
|
import { type ApiClientOptions, Bot } from "grammy";
|
2026-01-26 19:24:13 -05:00
|
|
|
import type { TelegramNetworkConfig } from "../config/types.telegram.js";
|
2026-01-08 04:40:29 +01:00
|
|
|
import { resolveTelegramFetch } from "./fetch.js";
|
2025-12-07 22:46:02 +01:00
|
|
|
|
|
|
|
|
export async function setTelegramWebhook(opts: {
|
|
|
|
|
token: string;
|
|
|
|
|
url: string;
|
|
|
|
|
secret?: string;
|
|
|
|
|
dropPendingUpdates?: boolean;
|
2026-01-26 19:24:13 -05:00
|
|
|
network?: TelegramNetworkConfig;
|
2025-12-07 22:46:02 +01:00
|
|
|
}) {
|
2026-01-26 19:24:13 -05:00
|
|
|
const fetchImpl = resolveTelegramFetch(undefined, { network: opts.network });
|
2026-01-08 15:16:53 +01:00
|
|
|
const client: ApiClientOptions | undefined = fetchImpl
|
|
|
|
|
? { fetch: fetchImpl as unknown as ApiClientOptions["fetch"] }
|
|
|
|
|
: undefined;
|
2026-01-09 13:57:06 +01:00
|
|
|
const bot = new Bot(opts.token, client ? { client } : undefined);
|
2025-12-07 22:46:02 +01:00
|
|
|
await bot.api.setWebhook(opts.url, {
|
|
|
|
|
secret_token: opts.secret,
|
|
|
|
|
drop_pending_updates: opts.dropPendingUpdates ?? false,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 19:24:13 -05:00
|
|
|
export async function deleteTelegramWebhook(opts: {
|
|
|
|
|
token: string;
|
|
|
|
|
network?: TelegramNetworkConfig;
|
|
|
|
|
}) {
|
|
|
|
|
const fetchImpl = resolveTelegramFetch(undefined, { network: opts.network });
|
2026-01-08 15:16:53 +01:00
|
|
|
const client: ApiClientOptions | undefined = fetchImpl
|
|
|
|
|
? { fetch: fetchImpl as unknown as ApiClientOptions["fetch"] }
|
|
|
|
|
: undefined;
|
2026-01-09 13:57:06 +01:00
|
|
|
const bot = new Bot(opts.token, client ? { client } : undefined);
|
2025-12-07 22:46:02 +01:00
|
|
|
await bot.api.deleteWebhook();
|
|
|
|
|
}
|