@@ -31,7 +31,12 @@ import {
3131 resolveDefaultTelegramAccountId ,
3232 resolveTelegramPollActionGateState ,
3333} from "./accounts.js" ;
34- import { resolveTelegramInlineButtons } from "./button-types.js" ;
34+ import { TELEGRAM_CALLBACK_DATA_MAX_BYTES } from "./approval-callback-data.js" ;
35+ import {
36+ resolveTelegramInlineButtons ,
37+ type TelegramButtonBuildOptions ,
38+ type TelegramDroppedControl ,
39+ } from "./button-types.js" ;
3540import { telegramInboundEventDelivery } from "./inbound-event-delivery.js" ;
3641import {
3742 resolveTelegramInlineButtonsScope ,
@@ -219,7 +224,7 @@ function readTelegramSendMediaUrls(params: Record<string, unknown>) {
219224function resolveTelegramButtonsFromParams (
220225 params : Record < string , unknown > ,
221226 presentation = normalizeMessagePresentation ( params . presentation ) ,
222- options ?: { allowWebAppButtons ?: boolean } ,
227+ options ?: TelegramButtonBuildOptions ,
223228) {
224229 return resolveTelegramInlineButtons (
225230 {
@@ -272,7 +277,59 @@ function readTelegramSendContent(params: {
272277 if ( content == null && ! params . mediaUrl && ! params . hasButtons && ! params . hasLocation ) {
273278 throw new Error ( "content required." ) ;
274279 }
275- return content ?? "" ;
280+ return {
281+ content : content ?? "" ,
282+ hasExplicitContent : explicitContent != null ,
283+ } ;
284+ }
285+
286+ function renderTelegramDroppedControlFallback ( controls : readonly TelegramDroppedControl [ ] ) : string {
287+ return renderMessagePresentationFallbackText ( {
288+ presentation : {
289+ blocks : [
290+ {
291+ type : "buttons" ,
292+ buttons : controls . map ( ( control ) => ( { label : control . label , value : "unavailable" } ) ) ,
293+ } ,
294+ ] ,
295+ } ,
296+ } ) ;
297+ }
298+
299+ function appendTelegramDroppedControlFallback (
300+ text : string ,
301+ controls : readonly TelegramDroppedControl [ ] ,
302+ ) : string {
303+ const fallback = renderTelegramDroppedControlFallback ( controls ) ;
304+ if ( ! fallback || text === fallback || text . endsWith ( `\n\n${ fallback } ` ) ) {
305+ return text ;
306+ }
307+ return [ text , fallback ] . filter ( Boolean ) . join ( "\n\n" ) ;
308+ }
309+
310+ function buildTelegramControlDegradation (
311+ controls : readonly TelegramDroppedControl [ ] ,
312+ fallbackDelivered : boolean ,
313+ ) {
314+ if ( controls . length === 0 ) {
315+ return undefined ;
316+ }
317+ const reasons = [ ...new Set ( controls . map ( ( control ) => control . reason ) ) ] ;
318+ const hasOverflow = reasons . includes ( "callback_data_too_long" ) ;
319+ return {
320+ warning : fallbackDelivered
321+ ? `Telegram delivered ${ controls . length } unencodable control${ controls . length === 1 ? "" : "s" } as readable text.`
322+ : `Telegram could not deliver ${ controls . length } control${ controls . length === 1 ? "" : "s" } .` ,
323+ degradedDelivery : {
324+ droppedControls : controls . length ,
325+ fallback : fallbackDelivered ? "text" : "not_delivered" ,
326+ reasons,
327+ ...( hasOverflow ? { callbackDataLimitBytes : TELEGRAM_CALLBACK_DATA_MAX_BYTES } : { } ) ,
328+ guidance : hasOverflow
329+ ? `Shorten callback data to at most ${ TELEGRAM_CALLBACK_DATA_MAX_BYTES } UTF-8 bytes and retry if clickable controls are required.`
330+ : "Retry with a supported control action if clickable controls are required." ,
331+ } ,
332+ } ;
276333}
277334
278335function normalizeTelegramDeliveryPin ( params : Record < string , unknown > ) {
@@ -482,19 +539,34 @@ export async function handleTelegramAction(
482539 const firstMediaUrl = mediaUrls [ 0 ] ;
483540 const location = normalizeOutboundLocation ( params . location ) ;
484541 const presentation = normalizeMessagePresentation ( params . presentation ) ;
542+ const droppedControls : TelegramDroppedControl [ ] = [ ] ;
485543 const buttons = resolveTelegramButtonsFromParams ( params , presentation , {
486544 allowWebAppButtons : resolveTelegramTargetChatType ( to ) === "direct" ,
545+ onDroppedControl : ( control ) => droppedControls . push ( control ) ,
487546 } ) ;
488- const content = readTelegramSendContent ( {
547+ const resolvedContent = readTelegramSendContent ( {
489548 args : params ,
490549 mediaUrl : firstMediaUrl ,
491550 hasButtons : Array . isArray ( buttons ) && buttons . length > 0 ,
492551 hasLocation : Boolean ( location ) ,
493552 interactive : params . interactive ,
494553 presentation,
495554 } ) ;
555+ const content =
556+ droppedControls . length > 0 && resolvedContent . hasExplicitContent
557+ ? appendTelegramDroppedControlFallback ( resolvedContent . content , droppedControls )
558+ : resolvedContent . content ;
559+ const droppedControlFallback =
560+ droppedControls . length > 0 ? renderTelegramDroppedControlFallback ( droppedControls ) : "" ;
561+ const hasOnlyDroppedControlFallback =
562+ ! resolvedContent . hasExplicitContent &&
563+ droppedControlFallback . length > 0 &&
564+ content . trim ( ) === droppedControlFallback . trim ( ) ;
496565 const asVideoNote = readBooleanParam ( params , "asVideoNote" ) ?? false ;
497- if ( location && ( content . trim ( ) || mediaUrls . length > 0 || asVideoNote ) ) {
566+ if (
567+ location &&
568+ ( ( content . trim ( ) && ! hasOnlyDroppedControlFallback ) || mediaUrls . length > 0 || asVideoNote )
569+ ) {
498570 throw new Error ( "Telegram location sends cannot be combined with message text or media." ) ;
499571 }
500572 if ( asVideoNote && mediaUrls . length !== 1 ) {
@@ -602,6 +674,7 @@ export async function handleTelegramAction(
602674 ok : true ,
603675 messageId : result . messageId ,
604676 chatId : result . chatId ,
677+ ...buildTelegramControlDegradation ( droppedControls , Boolean ( content . trim ( ) ) ) ,
605678 } ) ;
606679 }
607680
@@ -738,15 +811,28 @@ export async function handleTelegramAction(
738811 accountId,
739812 context : options ,
740813 } ) ;
741- const content =
814+ let content =
742815 readStringParam ( params , "content" , { allowEmpty : false } ) ??
743816 readStringParam ( params , "message" , { allowEmpty : false } ) ;
744817 // Telegram treats an explicit empty caption as a request to remove it.
745- const caption = readStringParam ( params , "caption" , { allowEmpty : true } ) ;
818+ let caption = readStringParam ( params , "caption" , { allowEmpty : true } ) ;
819+ const droppedControls : TelegramDroppedControl [ ] = [ ] ;
746820 const buttons = resolveTelegramButtonsFromParams ( params , undefined , {
747821 allowWebAppButtons : resolveTelegramTargetChatType ( chatId ?? "" ) === "direct" ,
822+ onDroppedControl : ( control ) => droppedControls . push ( control ) ,
748823 } ) ;
824+ if ( droppedControls . length > 0 ) {
825+ if ( caption != null ) {
826+ caption = appendTelegramDroppedControlFallback ( caption , droppedControls ) ;
827+ } else if ( content != null ) {
828+ content = appendTelegramDroppedControlFallback ( content , droppedControls ) ;
829+ }
830+ }
749831 if ( content == null && caption == null && buttons === undefined ) {
832+ const degradation = buildTelegramControlDegradation ( droppedControls , false ) ;
833+ if ( degradation ) {
834+ return jsonResult ( { ok : false , ...degradation } ) ;
835+ }
750836 throw new Error ( "content required." ) ;
751837 }
752838 if ( buttons !== undefined ) {
@@ -782,6 +868,7 @@ export async function handleTelegramAction(
782868 ok : true ,
783869 messageId : result . messageId ,
784870 chatId : result . chatId ,
871+ ...buildTelegramControlDegradation ( droppedControls , false ) ,
785872 } ) ;
786873 }
787874 const result = await telegramActionRuntime . editMessageTelegram (
@@ -801,6 +888,7 @@ export async function handleTelegramAction(
801888 ok : true ,
802889 messageId : result . messageId ,
803890 chatId : result . chatId ,
891+ ...buildTelegramControlDegradation ( droppedControls , true ) ,
804892 } ) ;
805893 }
806894
0 commit comments