Skip to content

Commit 15e9bea

Browse files
authored
refactor(telegram): unify interactive keyboard rendering (#128728)
* refactor(telegram): unify interactive keyboard rendering * chore(telegram): ratchet removed keyboard assertion
1 parent 898b1ce commit 15e9bea

3 files changed

Lines changed: 60 additions & 37 deletions

File tree

‎config/assertion-safety-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ extensions/telegram/src/bot/body-helpers.ts 1
12791279
extensions/telegram/src/bot/delivery.replies.ts 1
12801280
extensions/telegram/src/bot/delivery.resolve-media.ts 1
12811281
extensions/telegram/src/bot/helpers.ts 2
1282-
extensions/telegram/src/button-types.ts 2
1282+
extensions/telegram/src/button-types.ts 1
12831283
extensions/telegram/src/callback-query-answer-state.ts 1
12841284
extensions/telegram/src/channel-actions.ts 1
12851285
extensions/telegram/src/channel.ts 1

‎extensions/telegram/src/button-types.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,55 @@ describe("buildTelegramInteractiveButtons callback limits", () => {
5353
});
5454
});
5555

56+
describe("resolveTelegramInlineButtons precedence", () => {
57+
it("returns explicit buttons without reading lower-priority payloads", () => {
58+
const buttons = [[{ text: "Explicit", callback_data: "explicit" }]];
59+
const params = {
60+
buttons,
61+
get interactive(): never {
62+
throw new Error("unexpected interactive normalization");
63+
},
64+
get presentation(): never {
65+
throw new Error("unexpected presentation normalization");
66+
},
67+
};
68+
69+
expect(resolveTelegramInlineButtons(params)).toBe(buttons);
70+
});
71+
72+
it.each([
73+
{
74+
description: "the legacy payload contains only text",
75+
interactive: { blocks: [{ type: "text", text: "Legacy heading" }] },
76+
expectedDropped: [],
77+
},
78+
{
79+
description: "every legacy callback exceeds the Telegram byte limit",
80+
interactive: {
81+
blocks: [{ type: "buttons", buttons: [{ label: "Oversized", value: "x".repeat(65) }] }],
82+
},
83+
expectedDropped: [
84+
{ label: "Oversized", reason: "callback_data_too_long", callbackDataBytes: 65 },
85+
],
86+
},
87+
])("falls back to presentation buttons when $description", ({ interactive, expectedDropped }) => {
88+
const dropped: Array<{ label: string; reason: string; callbackDataBytes?: number }> = [];
89+
90+
expect(
91+
resolveTelegramInlineButtons(
92+
{
93+
interactive,
94+
presentation: {
95+
blocks: [{ type: "buttons", buttons: [{ label: "Fallback", value: "fallback" }] }],
96+
},
97+
},
98+
{ onDroppedControl: (control) => dropped.push(control) },
99+
),
100+
).toEqual([[{ text: "Fallback", callback_data: "fallback", style: undefined }]]);
101+
expect(dropped).toEqual(expectedDropped);
102+
});
103+
});
104+
56105
describe("buildTelegramPresentationButtons", () => {
57106
it("builds inline buttons from presentation blocks", () => {
58107
expect(

‎extensions/telegram/src/button-types.ts‎

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Telegram plugin module implements button types behavior.
22
import { parseExecApprovalCommandText } from "openclaw/plugin-sdk/approval-reply-runtime";
33
import {
4-
reduceLegacyInteractiveReply,
4+
legacyInteractiveReplyToPresentation,
55
isMessagePresentationInteractiveBlock,
66
normalizeMessagePresentation,
77
normalizeLegacyInteractiveReply,
88
renderMessagePresentationFallbackText,
99
resolveMessagePresentationButtonAction,
10-
type LegacyInteractiveReply,
1110
type MessagePresentation,
1211
type MessagePresentationButton,
1312
} from "openclaw/plugin-sdk/interactive-runtime";
@@ -228,38 +227,6 @@ function chunkInteractiveButtons(
228227
}
229228
}
230229

231-
/**
232-
* @deprecated Use buildTelegramPresentationButtons with MessagePresentation.
233-
*/
234-
function buildTelegramInteractiveButtons(
235-
interactive?: LegacyInteractiveReply,
236-
options?: TelegramButtonBuildOptions,
237-
): TelegramInlineButtons | undefined {
238-
const rows = reduceLegacyInteractiveReply(
239-
interactive,
240-
[] as TelegramInlineButton[][],
241-
(state, block) => {
242-
if (block.type === "buttons") {
243-
chunkInteractiveButtons(block.buttons, state, options);
244-
return state;
245-
}
246-
if (block.type === "select") {
247-
chunkInteractiveButtons(
248-
block.options.map((option) => ({
249-
label: option.label,
250-
action: option.action,
251-
value: option.value,
252-
})),
253-
state,
254-
options,
255-
);
256-
}
257-
return state;
258-
},
259-
);
260-
return rows.length > 0 ? rows : undefined;
261-
}
262-
263230
/** Convert portable presentation controls to Telegram inline keyboard rows. */
264231
export function buildTelegramPresentationButtons(
265232
presentation?: MessagePresentation,
@@ -296,9 +263,16 @@ export function resolveTelegramInlineButtons(
296263
},
297264
options?: TelegramButtonBuildOptions,
298265
): TelegramInlineButtons | undefined {
266+
if (params.buttons) {
267+
return params.buttons;
268+
}
269+
270+
const interactive = normalizeLegacyInteractiveReply(params.interactive);
299271
return (
300-
params.buttons ??
301-
buildTelegramInteractiveButtons(normalizeLegacyInteractiveReply(params.interactive), options) ??
272+
buildTelegramPresentationButtons(
273+
interactive ? legacyInteractiveReplyToPresentation(interactive) : undefined,
274+
options,
275+
) ??
302276
buildTelegramPresentationButtons(normalizeMessagePresentation(params.presentation), options)
303277
);
304278
}

0 commit comments

Comments
 (0)