fix(telegram): resolve fetch type errors with grammY Bot constructor

Add proper type assertion for ApiClientOptions["fetch"] to resolve
TypeScript compilation errors when passing fetch implementation to
grammY's Bot constructor. This follows the same pattern already used
in bot.ts.

Fixes #465
This commit is contained in:
Yurii Chukhlib
2026-01-08 19:31:06 +01:00
parent 7450aed663
commit d05de07fdd
2 changed files with 18 additions and 16 deletions

View File

@@ -1,4 +1,5 @@
import type { ReactionType, ReactionTypeEmoji } from "@grammyjs/types";
import type { ApiClientOptions } from "grammy";
import { Bot, InputFile } from "grammy";
import { loadConfig } from "../config/config.js";
import { formatErrorMessage } from "../infra/errors.js";
@@ -113,10 +114,10 @@ export async function sendMessageTelegram(
// Use provided api or create a new Bot instance. The nullish coalescing
// operator ensures api is always defined (Bot.api is always non-null).
const fetchImpl = resolveTelegramFetch();
const api =
opts.api ??
new Bot(token, fetchImpl ? { client: { fetch: fetchImpl } } : undefined)
.api;
const client: ApiClientOptions | undefined = fetchImpl
? { fetch: fetchImpl as unknown as ApiClientOptions["fetch"] }
: undefined;
const api = opts.api ?? new Bot(token, client ? { client } : undefined).api;
const mediaUrl = opts.mediaUrl?.trim();
// Build optional params for forum topics and reply threading.
@@ -271,10 +272,10 @@ export async function reactMessageTelegram(
const chatId = normalizeChatId(String(chatIdInput));
const messageId = normalizeMessageId(messageIdInput);
const fetchImpl = resolveTelegramFetch();
const api =
opts.api ??
new Bot(token, fetchImpl ? { client: { fetch: fetchImpl } } : undefined)
.api;
const client: ApiClientOptions | undefined = fetchImpl
? { fetch: fetchImpl as unknown as ApiClientOptions["fetch"] }
: undefined;
const api = opts.api ?? new Bot(token, client ? { client } : undefined).api;
const request = createTelegramRetryRunner({
retry: opts.retry,
configRetry: account.config.retry,

View File

@@ -1,3 +1,4 @@
import type { ApiClientOptions } from "grammy";
import { Bot } from "grammy";
import { resolveTelegramFetch } from "./fetch.js";
@@ -8,10 +9,10 @@ export async function setTelegramWebhook(opts: {
dropPendingUpdates?: boolean;
}) {
const fetchImpl = resolveTelegramFetch();
const bot = new Bot(
opts.token,
fetchImpl ? { client: { fetch: fetchImpl } } : undefined,
);
const client: ApiClientOptions | undefined = fetchImpl
? { fetch: fetchImpl as unknown as ApiClientOptions["fetch"] }
: undefined;
const bot = new Bot(opts.token, client ? { client } : undefined);
await bot.api.setWebhook(opts.url, {
secret_token: opts.secret,
drop_pending_updates: opts.dropPendingUpdates ?? false,
@@ -20,9 +21,9 @@ export async function setTelegramWebhook(opts: {
export async function deleteTelegramWebhook(opts: { token: string }) {
const fetchImpl = resolveTelegramFetch();
const bot = new Bot(
opts.token,
fetchImpl ? { client: { fetch: fetchImpl } } : undefined,
);
const client: ApiClientOptions | undefined = fetchImpl
? { fetch: fetchImpl as unknown as ApiClientOptions["fetch"] }
: undefined;
const bot = new Bot(opts.token, client ? { client } : undefined);
await bot.api.deleteWebhook();
}