Skip to content

Commit 69cea57

Browse files
authored
fix(telegram): fail closed on missing topic threads (#83381)
* fix(telegram): fail closed on missing topic threads * docs(changelog): reference telegram topic cleanup
1 parent 58e1351 commit 69cea57

6 files changed

Lines changed: 140 additions & 318 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Docs: https://docs.openclaw.ai
3434
- CLI/help: keep `gateway`, `doctor`, `status`, and `health` help registration out of action/runtime imports so subcommand `--help` stays lightweight in constrained terminals. Fixes #83228. Thanks @dfguerrerom.
3535
- Cron/Discord: keep explicit announce runs in message-tool-only source-reply mode so scheduled agent turns post once instead of also echoing through automatic visible replies. Fixes #83261. Thanks @Theralley.
3636
- Telegram: preserve forum-topic origin targets in inbound, audio-preflight, and skipped-message hook contexts so follow-up delivery stays bound to the originating topic. Fixes #83302. Thanks @M00zyx.
37+
- Telegram: fail topic sends closed when Telegram reports `message thread not found` instead of retrying without `message_thread_id` into the base chat. Refs #83302.
3738
- Mac app: align the Sessions settings pane with the standard Settings page gutter and row spacing.
3839
- OpenAI/Codex: stop rejecting available `openai-codex` GPT-5.1, GPT-5.2, and GPT-5.3 model refs during config validation, while keeping removed Spark aliases suppressed. Fixes #83303.
3940
- Plugins/xAI: complete OAuth-backed xAI login and sidecar auth fixes, including guarded loopback callback CORS handling, video generation polling/defaults, and native-host User-Agent attribution. (#83322) Thanks @Jaaneek.

‎extensions/telegram/src/draft-stream.test.ts‎

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -145,27 +145,32 @@ describe("createTelegramDraftStream", () => {
145145
}
146146
});
147147

148-
it("does not retry DM message preview sends without the topic id", async () => {
149-
const api = createMockDraftApi();
150-
api.sendMessage.mockRejectedValueOnce(new Error("400: Bad Request: message thread not found"));
151-
const warn = vi.fn();
152-
const stream = createDraftStream(api, {
153-
thread: { id: 42, scope: "dm" },
154-
warn,
155-
});
148+
it.each(["forum", "dm"] as const)(
149+
"does not retry %s message preview sends without the topic id",
150+
async (scope) => {
151+
const api = createMockDraftApi();
152+
api.sendMessage.mockRejectedValueOnce(
153+
new Error("400: Bad Request: message thread not found"),
154+
);
155+
const warn = vi.fn();
156+
const stream = createDraftStream(api, {
157+
thread: { id: 42, scope },
158+
warn,
159+
});
156160

157-
stream.update("Hello");
158-
await stream.flush();
161+
stream.update("Hello");
162+
await stream.flush();
159163

160-
expect(api.sendMessage).toHaveBeenCalledTimes(1);
161-
expect(api.sendMessage).toHaveBeenCalledWith(123, "Hello", { message_thread_id: 42 });
162-
expect(warn).toHaveBeenCalledWith(
163-
"telegram stream preview failed: 400: Bad Request: message thread not found",
164-
);
165-
expect(
166-
warn.mock.calls.some(([message]) => String(message).includes("retrying without thread")),
167-
).toBe(false);
168-
});
164+
expect(api.sendMessage).toHaveBeenCalledTimes(1);
165+
expect(api.sendMessage).toHaveBeenCalledWith(123, "Hello", { message_thread_id: 42 });
166+
expect(warn).toHaveBeenCalledWith(
167+
"telegram stream preview failed: 400: Bad Request: message thread not found",
168+
);
169+
expect(
170+
warn.mock.calls.some(([message]) => String(message).includes("retrying without thread")),
171+
).toBe(false);
172+
},
173+
);
169174

170175
it("keeps allow_sending_without_reply on message previews that target a reply", async () => {
171176
const api = createMockDraftApi();

‎extensions/telegram/src/draft-stream.ts‎

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,6 @@ import { normalizeTelegramReplyToMessageId } from "./outbound-params.js";
1010

1111
const TELEGRAM_STREAM_MAX_CHARS = 4096;
1212
const DEFAULT_THROTTLE_MS = 1000;
13-
const THREAD_NOT_FOUND_RE = /400:\s*Bad Request:\s*message thread not found/i;
14-
15-
type TelegramSendMessageParams = Parameters<Bot["api"]["sendMessage"]>[2];
16-
17-
function hasNumericMessageThreadId(
18-
params: TelegramSendMessageParams | undefined,
19-
): params is TelegramSendMessageParams & { message_thread_id: number } {
20-
return (
21-
typeof params === "object" &&
22-
params !== null &&
23-
typeof (params as { message_thread_id?: unknown }).message_thread_id === "number"
24-
);
25-
}
2613

2714
export type TelegramDraftStream = {
2815
update: (text: string) => void;
@@ -109,7 +96,6 @@ export function createTelegramDraftStream(params: {
10996
const minInitialChars = params.minInitialChars;
11097
const chatId = params.chatId;
11198
const threadParams = buildTelegramThreadParams(params.thread);
112-
const allowThreadlessRetry = params.thread?.scope !== "dm";
11399
const replyToMessageId = normalizeTelegramReplyToMessageId(params.replyToMessageId);
114100
const replyParams =
115101
replyToMessageId != null
@@ -136,39 +122,17 @@ export function createTelegramDraftStream(params: {
136122
renderedParseMode: "HTML" | undefined;
137123
sendGeneration: number;
138124
};
139-
const sendRenderedMessageWithThreadFallback = async (sendArgs: {
125+
const sendRenderedMessage = async (sendArgs: {
140126
renderedText: string;
141127
renderedParseMode: "HTML" | undefined;
142-
fallbackWarnMessage: string;
143128
}) => {
144129
const sendParams = sendArgs.renderedParseMode
145130
? {
146131
...replyParams,
147132
parse_mode: sendArgs.renderedParseMode,
148133
}
149134
: replyParams;
150-
const usedThreadParams = hasNumericMessageThreadId(sendParams);
151-
try {
152-
return {
153-
sent: await params.api.sendMessage(chatId, sendArgs.renderedText, sendParams),
154-
usedThreadParams,
155-
};
156-
} catch (err) {
157-
if (!allowThreadlessRetry || !usedThreadParams || !THREAD_NOT_FOUND_RE.test(String(err))) {
158-
throw err;
159-
}
160-
const threadlessParams: TelegramSendMessageParams = { ...sendParams };
161-
delete threadlessParams.message_thread_id;
162-
params.warn?.(sendArgs.fallbackWarnMessage);
163-
return {
164-
sent: await params.api.sendMessage(
165-
chatId,
166-
sendArgs.renderedText,
167-
Object.keys(threadlessParams).length > 0 ? threadlessParams : undefined,
168-
),
169-
usedThreadParams: false,
170-
};
171-
}
135+
return await params.api.sendMessage(chatId, sendArgs.renderedText, sendParams);
172136
};
173137
const sendMessageTransportPreview = async ({
174138
renderedText,
@@ -187,14 +151,12 @@ export function createTelegramDraftStream(params: {
187151
return true;
188152
}
189153
messageSendAttempted = true;
190-
let sent: Awaited<ReturnType<typeof sendRenderedMessageWithThreadFallback>>["sent"];
154+
let sent: Awaited<ReturnType<typeof sendRenderedMessage>>;
191155
try {
192-
({ sent } = await sendRenderedMessageWithThreadFallback({
156+
sent = await sendRenderedMessage({
193157
renderedText,
194158
renderedParseMode,
195-
fallbackWarnMessage:
196-
"telegram stream preview send failed with message_thread_id, retrying without thread",
197-
}));
159+
});
198160
} catch (err) {
199161
if (isSafeToRetrySendError(err) || isTelegramClientRejection(err)) {
200162
messageSendAttempted = false;

‎extensions/telegram/src/reply-parameters.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export function resolveTelegramSendThreadSpec(params: {
2727
if (messageThreadId == null) {
2828
return undefined;
2929
}
30-
// Telegram supports DM topics; keep direct chat thread IDs and rely on
31-
// thread-not-found retry fallback when a plain DM rejects them.
30+
// Telegram supports DM topics; keep direct chat thread IDs and let invalid
31+
// topics fail closed instead of sending to the base chat.
3232
return {
3333
id: messageThreadId,
3434
scope: params.chatType === "direct" ? "dm" : "forum",

0 commit comments

Comments
 (0)