refactor(telegram): dedupe non-idempotent request setup

This commit is contained in:
Peter Steinberger
2026-03-08 01:09:36 +00:00
parent 7e59803df2
commit f866e57de3

View File

@@ -436,6 +436,24 @@ function createRequestWithChatNotFound(params: {
});
}
function createTelegramNonIdempotentRequestWithDiag(params: {
cfg: ReturnType<typeof loadConfig>;
account: ResolvedTelegramAccount;
retry?: RetryConfig;
verbose?: boolean;
useApiErrorLogging?: boolean;
}): TelegramRequestWithDiag {
return createTelegramRequestWithDiag({
cfg: params.cfg,
account: params.account,
retry: params.retry,
verbose: params.verbose,
useApiErrorLogging: params.useApiErrorLogging,
shouldRetry: (err) => isSafeToRetrySendError(err),
strictShouldRetry: true,
});
}
export function buildInlineKeyboard(
buttons?: TelegramSendOpts["buttons"],
): InlineKeyboardMarkup | undefined {
@@ -489,13 +507,11 @@ export async function sendMessageTelegram(
quoteText: opts.quoteText,
});
const hasThreadParams = Object.keys(threadParams).length > 0;
const requestWithDiag = createTelegramRequestWithDiag({
const requestWithDiag = createTelegramNonIdempotentRequestWithDiag({
cfg,
account,
retry: opts.retry,
verbose: opts.verbose,
shouldRetry: (err) => isSafeToRetrySendError(err),
strictShouldRetry: true,
});
const requestWithChatNotFound = createRequestWithChatNotFound({
requestWithDiag,
@@ -1097,13 +1113,11 @@ export async function sendPollTelegram(
// Build poll options as simple strings (Grammy accepts string[] or InputPollOption[])
const pollOptions = normalizedPoll.options;
const requestWithDiag = createTelegramRequestWithDiag({
const requestWithDiag = createTelegramNonIdempotentRequestWithDiag({
cfg,
account,
retry: opts.retry,
verbose: opts.verbose,
shouldRetry: (err) => isSafeToRetrySendError(err),
strictShouldRetry: true,
});
const requestWithChatNotFound = createRequestWithChatNotFound({
requestWithDiag,
@@ -1218,22 +1232,12 @@ export async function createForumTopicTelegram(
verbose: opts.verbose,
});
const request = createTelegramRetryRunner({
const requestWithDiag = createTelegramNonIdempotentRequestWithDiag({
cfg,
account,
retry: opts.retry,
configRetry: account.config.retry,
verbose: opts.verbose,
shouldRetry: (err) => isSafeToRetrySendError(err),
strictShouldRetry: true,
});
const logHttpError = createTelegramHttpLogger(cfg);
const requestWithDiag = <T>(fn: () => Promise<T>, label?: string) =>
withTelegramApiErrorLogging({
operation: label ?? "request",
fn: () => request(fn, label),
}).catch((err) => {
logHttpError(label ?? "request", err);
throw err;
});
const extra: Record<string, unknown> = {};
if (opts.iconColor != null) {