refactor: lint cleanups and helpers

This commit is contained in:
Peter Steinberger
2025-12-23 00:28:40 +00:00
parent f5837dff9c
commit 918cbdcf03
39 changed files with 679 additions and 338 deletions

View File

@@ -12,6 +12,7 @@ import type { ReplyPayload } from "../auto-reply/types.js";
import { loadConfig } from "../config/config.js";
import { resolveStorePath, updateLastRoute } from "../config/sessions.js";
import { danger, logVerbose } from "../globals.js";
import { formatErrorMessage } from "../infra/errors.js";
import { getChildLogger } from "../logging.js";
import { mediaKindFromMime } from "../media/constants.js";
import { detectMime } from "../media/mime.js";
@@ -341,11 +342,10 @@ async function sendTelegramText(
try {
await bot.api.sendMessage(chatId, text, { parse_mode: "Markdown" });
} catch (err) {
if (PARSE_ERR_RE.test(String(err ?? ""))) {
const errText = formatErrorMessage(err);
if (PARSE_ERR_RE.test(errText)) {
runtime.log?.(
`telegram markdown parse failed; retrying without formatting: ${String(
err,
)}`,
`telegram markdown parse failed; retrying without formatting: ${errText}`,
);
await bot.api.sendMessage(chatId, text);
return;

View File

@@ -52,7 +52,7 @@ export async function monitorTelegramProvider(opts: MonitorTelegramOpts = {}) {
// Long polling
const stopOnAbort = () => {
if (opts.abortSignal?.aborted) bot.stop();
if (opts.abortSignal?.aborted) void bot.stop();
};
opts.abortSignal?.addEventListener("abort", stopOnAbort, { once: true });
try {

View File

@@ -3,6 +3,8 @@ import { ProxyAgent } from "undici";
export function makeProxyFetch(proxyUrl: string): typeof fetch {
const agent = new ProxyAgent(proxyUrl);
return (input: RequestInfo | URL, init?: RequestInit) =>
fetch(input, { ...(init ?? {}), dispatcher: agent });
return (input: RequestInfo | URL, init?: RequestInit) => {
const base = init ? { ...init } : {};
return fetch(input, { ...base, dispatcher: agent });
};
}

View File

@@ -1,6 +1,6 @@
// @ts-nocheck
import { Bot, InputFile } from "grammy";
import { formatErrorMessage } from "../infra/errors.js";
import { mediaKindFromMime } from "../media/constants.js";
import { loadWebMedia } from "../web/media.js";
@@ -76,16 +76,17 @@ export async function sendMessageTelegram(
return await fn();
} catch (err) {
lastErr = err;
const errText = formatErrorMessage(err);
const terminal =
attempt === 3 ||
!/429|timeout|connect|reset|closed|unavailable|temporarily/i.test(
String(err ?? ""),
errText,
);
if (terminal) break;
const backoff = 400 * attempt;
if (opts.verbose) {
console.warn(
`telegram send retry ${attempt}/2 for ${label} in ${backoff}ms: ${String(err)}`,
`telegram send retry ${attempt}/2 for ${label} in ${backoff}ms: ${errText}`,
);
}
await sleep(backoff);
@@ -95,7 +96,7 @@ export async function sendMessageTelegram(
};
const wrapChatNotFound = (err: unknown) => {
if (!/400: Bad Request: chat not found/i.test(String(err ?? "")))
if (!/400: Bad Request: chat not found/i.test(formatErrorMessage(err)))
return err;
return new Error(
[
@@ -161,10 +162,11 @@ export async function sendMessageTelegram(
).catch(async (err) => {
// Telegram rejects malformed Markdown (e.g., unbalanced '_' or '*').
// When that happens, fall back to plain text so the message still delivers.
if (PARSE_ERR_RE.test(String(err ?? ""))) {
const errText = formatErrorMessage(err);
if (PARSE_ERR_RE.test(errText)) {
if (opts.verbose) {
console.warn(
`telegram markdown parse failed, retrying as plain text: ${String(err)}`,
`telegram markdown parse failed, retrying as plain text: ${errText}`,
);
}
return await sendWithRetry(

View File

@@ -1,7 +1,7 @@
import { createServer } from "node:http";
import { webhookCallback } from "grammy";
import { formatErrorMessage } from "../infra/errors.js";
import type { RuntimeEnv } from "../runtime.js";
import { defaultRuntime } from "../runtime.js";
import { createTelegramBot } from "./bot.js";
@@ -43,7 +43,16 @@ export async function startTelegramWebhook(opts: {
res.end();
return;
}
handler(req, res);
const handled = handler(req, res);
if (handled && typeof (handled as Promise<void>).catch === "function") {
void (handled as Promise<void>).catch((err) => {
runtime.log?.(
`Telegram webhook handler failed: ${formatErrorMessage(err)}`,
);
if (!res.headersSent) res.writeHead(500);
res.end();
});
}
});
const publicUrl =
@@ -59,7 +68,7 @@ export async function startTelegramWebhook(opts: {
const shutdown = () => {
server.close();
bot.stop();
void bot.stop();
};
if (opts.abortSignal) {
opts.abortSignal.addEventListener("abort", shutdown, { once: true });