Skip to content

Commit 0ef738b

Browse files
fix(telegram): fall back for web apps outside DMs
1 parent 7e1aca9 commit 0ef738b

10 files changed

Lines changed: 150 additions & 33 deletions

‎extensions/telegram/src/bot-message-dispatch-delivery.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
type TelegramPromptContextSource,
5353
} from "./prompt-context-projection.js";
5454
import { editMessageTelegram } from "./send.js";
55+
import { resolveTelegramTargetChatType } from "./targets.js";
5556

5657
export function createTelegramDeliveryController(params: {
5758
bot: Bot;
@@ -560,7 +561,11 @@ export function createTelegramDeliveryController(params: {
560561
delete payloadForPlan.isReasoning;
561562
}
562563
const normalized = projectPayloadForDelivery(payloadForPlan);
563-
return normalized ? canonicalizeTelegramPresentationPayload(normalized) : undefined;
564+
return normalized
565+
? canonicalizeTelegramPresentationPayload(normalized, {
566+
allowWebAppButtons: resolveTelegramTargetChatType(String(context.chatId)) === "direct",
567+
})
568+
: undefined;
564569
},
565570
sendPayload,
566571
snapshot: deliveryState.snapshot,

‎extensions/telegram/src/bot/delivery.replies.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
} from "../rich-message.js";
5555
import { isTelegramHtmlParseError } from "../rich-plain-fallback.js";
5656
import { buildInlineKeyboard, reactMessageTelegram } from "../send.js";
57+
import { resolveTelegramTargetChatType } from "../targets.js";
5758
import { resolveTelegramVoiceSend } from "../voice.js";
5859
import {
5960
buildTelegramSendParams,
@@ -844,7 +845,9 @@ export async function deliverReplies(params: {
844845
}),
845846
);
846847
for (const originalReply of normalizedReplies) {
847-
let reply = canonicalizeTelegramPresentationPayload(originalReply);
848+
let reply = canonicalizeTelegramPresentationPayload(originalReply, {
849+
allowWebAppButtons: resolveTelegramTargetChatType(params.chatId) === "direct",
850+
});
848851
const mediaList = reply?.mediaUrls?.length
849852
? reply.mediaUrls
850853
: reply?.mediaUrl

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export function describeTelegramInteractiveButtonBehavior(): void {
3434
{ text: "Reject", callback_data: "reject", style: "danger" },
3535
],
3636
[
37-
{ text: "Launch", web_app: { url: "https://example.com/app" }, style: undefined },
3837
{ text: "Later", callback_data: "later", style: undefined },
3938
{ text: "Archive", callback_data: "archive", style: undefined },
4039
],

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

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -408,22 +408,25 @@ describe("buildTelegramPresentationButtons", () => {
408408

409409
it("renders typed and legacy URL and Web App actions natively", () => {
410410
expect(
411-
buildTelegramPresentationButtons({
412-
blocks: [
413-
{
414-
type: "buttons",
415-
buttons: [
416-
{ label: "Typed URL", action: { type: "url", url: "https://example.com/typed" } },
417-
{
418-
label: "Typed App",
419-
action: { type: "web-app", url: "https://example.com/app" },
420-
},
421-
{ label: "Legacy URL", url: "https://example.com/legacy" },
422-
{ label: "Legacy App", webApp: { url: "https://example.com/legacy-app" } },
423-
],
424-
},
425-
],
426-
}),
411+
buildTelegramPresentationButtons(
412+
{
413+
blocks: [
414+
{
415+
type: "buttons",
416+
buttons: [
417+
{ label: "Typed URL", action: { type: "url", url: "https://example.com/typed" } },
418+
{
419+
label: "Typed App",
420+
action: { type: "web-app", url: "https://example.com/app" },
421+
},
422+
{ label: "Legacy URL", url: "https://example.com/legacy" },
423+
{ label: "Legacy App", webApp: { url: "https://example.com/legacy-app" } },
424+
],
425+
},
426+
],
427+
},
428+
{ allowWebAppButtons: true },
429+
),
427430
).toEqual([
428431
[
429432
{ text: "Typed URL", url: "https://example.com/typed", style: undefined },
@@ -444,6 +447,21 @@ describe("buildTelegramPresentationButtons", () => {
444447
]);
445448
});
446449

450+
it("skips Web App actions unless a direct target was confirmed", () => {
451+
expect(
452+
buildTelegramPresentationButtons({
453+
blocks: [
454+
{
455+
type: "buttons",
456+
buttons: [
457+
{ label: "App", action: { type: "web-app", url: "https://example.com/app" } },
458+
],
459+
},
460+
],
461+
}),
462+
).toBeUndefined();
463+
});
464+
447465
it("skips hosted widget actions without a Telegram web app URL", () => {
448466
expect(
449467
buildTelegramPresentationButtons({

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function toTelegramButtonStyle(
4848
function toTelegramInlineButton(
4949
button: MessagePresentationButton,
5050
optionIndex: number,
51+
options?: { allowWebAppButtons?: boolean },
5152
): TelegramInlineButton | undefined {
5253
const style = toTelegramButtonStyle(button.style);
5354
const action = resolveMessagePresentationButtonAction(button);
@@ -58,7 +59,9 @@ function toTelegramInlineButton(
5859
return { text: button.label, url: action.url, style };
5960
}
6061
if (action.type === "web-app") {
61-
return action.url ? { text: button.label, web_app: { url: action.url }, style } : undefined;
62+
return options?.allowWebAppButtons === true && action.url
63+
? { text: button.label, web_app: { url: action.url }, style }
64+
: undefined;
6265
}
6366
if (action.type === "approval") {
6467
const callbackData = buildTelegramApprovalCallbackData(action);
@@ -99,12 +102,13 @@ function toTelegramInlineButton(
99102
function chunkInteractiveButtons(
100103
buttons: readonly MessagePresentationButton[],
101104
rows: TelegramInlineButton[][],
105+
options?: { allowWebAppButtons?: boolean },
102106
) {
103107
// Index is position in the question's options; core emits one buttons block in option order.
104108
for (let i = 0; i < buttons.length; i += TELEGRAM_INTERACTIVE_ROW_SIZE) {
105109
const row = buttons
106110
.slice(i, i + TELEGRAM_INTERACTIVE_ROW_SIZE)
107-
.map((button, offset) => toTelegramInlineButton(button, i + offset))
111+
.map((button, offset) => toTelegramInlineButton(button, i + offset, options))
108112
.filter((button): button is TelegramInlineButton => Boolean(button));
109113
if (row.length > 0) {
110114
rows.push(row);
@@ -145,14 +149,15 @@ function buildTelegramInteractiveButtons(
145149
/** Convert portable presentation controls to Telegram inline keyboard rows. */
146150
export function buildTelegramPresentationButtons(
147151
presentation?: MessagePresentation,
152+
options?: { allowWebAppButtons?: boolean },
148153
): TelegramInlineButtons | undefined {
149154
const rows: TelegramInlineButton[][] = [];
150155
for (const block of presentation?.blocks ?? []) {
151156
if (!isMessagePresentationInteractiveBlock(block)) {
152157
continue;
153158
}
154159
if (block.type === "buttons") {
155-
chunkInteractiveButtons(block.buttons, rows);
160+
chunkInteractiveButtons(block.buttons, rows, options);
156161
continue;
157162
}
158163
chunkInteractiveButtons(

‎extensions/telegram/src/interactive-fallback.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,41 @@ describe("canonicalizeTelegramPresentationPayload", () => {
125125
});
126126
});
127127

128+
it("uses native web_app only for a confirmed direct target", () => {
129+
const payload = {
130+
text: "Open app:",
131+
presentation: {
132+
blocks: [
133+
{
134+
type: "buttons" as const,
135+
buttons: [
136+
{
137+
label: "Launch",
138+
action: { type: "web-app" as const, url: "https://example.com/app" },
139+
},
140+
],
141+
},
142+
],
143+
},
144+
};
145+
146+
expect(
147+
canonicalizeTelegramPresentationPayload(payload, { allowWebAppButtons: true }),
148+
).toMatchObject({
149+
text: "Open app:",
150+
channelData: {
151+
telegram: {
152+
buttons: [[{ text: "Launch", web_app: { url: "https://example.com/app" } }]],
153+
},
154+
},
155+
});
156+
expect(canonicalizeTelegramPresentationPayload(payload, { allowWebAppButtons: false })).toEqual(
157+
{
158+
text: "Open app:\n\n- Launch: https://example.com/app",
159+
},
160+
);
161+
});
162+
128163
it("falls back presentation controls when explicit Telegram buttons take precedence", () => {
129164
const nativeButtons = [[{ text: "Native", callback_data: "native" }]];
130165
const result = canonicalizeTelegramPresentationPayload({

‎extensions/telegram/src/interactive-fallback.ts‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ export const TELEGRAM_PRESENTATION_CAPABILITIES = {
4040
},
4141
};
4242

43-
function canEncodeTelegramPresentationControl(block: MessagePresentationInteractiveBlock): boolean {
44-
return Boolean(buildTelegramPresentationButtons({ blocks: [block] })?.length);
43+
function canEncodeTelegramPresentationControl(
44+
block: MessagePresentationInteractiveBlock,
45+
options?: { allowWebAppButtons?: boolean },
46+
): boolean {
47+
return Boolean(buildTelegramPresentationButtons({ blocks: [block] }, options)?.length);
4548
}
4649

4750
function partitionTelegramPresentationBlocks(params: {
4851
presentation: MessagePresentation;
4952
presentationControlsSelected: boolean;
53+
allowWebAppButtons: boolean;
5054
}): {
5155
fallbackBlocks: MessagePresentation["blocks"];
5256
nativeControlBlocks: MessagePresentationInteractiveBlock[];
@@ -66,7 +70,10 @@ function partitionTelegramPresentationBlocks(params: {
6670
const nativeButtons: typeof block.buttons = [];
6771
const fallbackButtons: typeof block.buttons = [];
6872
for (const button of block.buttons) {
69-
const target = canEncodeTelegramPresentationControl({ type: "buttons", buttons: [button] })
73+
const target = canEncodeTelegramPresentationControl(
74+
{ type: "buttons", buttons: [button] },
75+
{ allowWebAppButtons: params.allowWebAppButtons },
76+
)
7077
? nativeButtons
7178
: fallbackButtons;
7279
target.push(button);
@@ -102,7 +109,10 @@ function partitionTelegramPresentationBlocks(params: {
102109
}
103110

104111
/** Convert portable presentation into the one Telegram payload shape used by every send funnel. */
105-
export function canonicalizeTelegramPresentationPayload(payload: ReplyPayload): ReplyPayload {
112+
export function canonicalizeTelegramPresentationPayload(
113+
payload: ReplyPayload,
114+
options?: { allowWebAppButtons?: boolean },
115+
): ReplyPayload {
106116
const normalizedPresentation = normalizeMessagePresentation(payload.presentation);
107117
const telegramData = payload.channelData?.telegram as
108118
| (Record<string, unknown> & {
@@ -131,10 +141,14 @@ export function canonicalizeTelegramPresentationPayload(payload: ReplyPayload):
131141
const { fallbackBlocks, nativeControlBlocks } = partitionTelegramPresentationBlocks({
132142
presentation,
133143
presentationControlsSelected,
144+
allowWebAppButtons: options?.allowWebAppButtons === true,
134145
});
135-
const presentationButtons = buildTelegramPresentationButtons({
136-
blocks: nativeControlBlocks,
137-
});
146+
const presentationButtons = buildTelegramPresentationButtons(
147+
{
148+
blocks: nativeControlBlocks,
149+
},
150+
options,
151+
);
138152
const buttons = existingButtons ?? presentationButtons;
139153

140154
const fallbackText = renderMessagePresentationFallbackText({

‎extensions/telegram/src/outbound-adapter.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ describe("telegramOutbound", () => {
488488
const rendered = await telegramOutbound.renderPresentation?.({
489489
payload: { text: "Open app:" },
490490
presentation,
491-
ctx: {} as never,
491+
ctx: { to: "12345" } as never,
492492
});
493493
if (!rendered) {
494494
throw new Error("expected rendered Telegram presentation");

‎extensions/telegram/src/outbound-adapter.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ export async function sendTelegramPayloadMessages(params: {
145145
payload: ReplyPayload;
146146
baseOpts: Omit<NonNullable<TelegramSendOpts>, "buttons" | "mediaUrl" | "quoteText">;
147147
}): Promise<Awaited<ReturnType<TelegramSendFn>>> {
148-
const payload = canonicalizeTelegramPresentationPayload(params.payload);
148+
const payload = canonicalizeTelegramPresentationPayload(params.payload, {
149+
allowWebAppButtons: parseTelegramTarget(params.to).chatType === "direct",
150+
});
149151
const telegramData = payload.channelData?.telegram as
150152
| {
151153
buttons?: TelegramInlineButtons;
@@ -315,8 +317,11 @@ export function createTelegramOutboundAdapter(
315317
batch: true,
316318
},
317319
},
318-
renderPresentation: ({ payload, presentation }) =>
319-
canonicalizeTelegramPresentationPayload({ ...payload, presentation }),
320+
renderPresentation: ({ payload, presentation, ctx }) =>
321+
canonicalizeTelegramPresentationPayload(
322+
{ ...payload, presentation },
323+
{ allowWebAppButtons: parseTelegramTarget(ctx.to ?? "").chatType === "direct" },
324+
),
320325
afterDeliverPayload: ({ cfg, target, payload, results }) => {
321326
const questionId = questionGatewayRuntime.readAskUserQuestionId(payload);
322327
const telegramResults = results.filter(
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, expect, it } from "vitest";
2+
import { telegramOutbound } from "./outbound-adapter.js";
3+
4+
const webAppPresentation = {
5+
blocks: [
6+
{
7+
type: "buttons" as const,
8+
buttons: [
9+
{
10+
label: "Launch",
11+
action: { type: "web-app" as const, url: "https://example.com/app" },
12+
},
13+
],
14+
},
15+
],
16+
};
17+
18+
describe("Telegram outbound web app presentation", () => {
19+
it.each(["-1001234567890", "@channelname"])(
20+
"falls back to a link for non-DM target %s",
21+
async (to) => {
22+
const rendered = await telegramOutbound.renderPresentation?.({
23+
payload: { text: "Open app:" },
24+
presentation: webAppPresentation,
25+
ctx: { to } as never,
26+
});
27+
28+
expect(rendered).toEqual({
29+
text: "Open app:\n\n- Launch: https://example.com/app",
30+
});
31+
},
32+
);
33+
});

0 commit comments

Comments
 (0)