@@ -12,6 +12,8 @@ import TelegramBot from './telegram_bot.js';
1212export default class TelegramExecutionContext {
1313 /** Cache for business connection owners */
1414 private static businessOwners = new Map < string , number > ( ) ;
15+ /** Cache for dead business connections */
16+ private static poisonedConnections = new Set < string > ( ) ;
1517
1618 /** an instance of the telegram bot */
1719 bot : TelegramBot ;
@@ -161,11 +163,14 @@ export default class TelegramExecutionContext {
161163 params : any ,
162164 apiMethod : ( botApi : string , data : any ) => Promise < T >
163165 ) : Promise < T | null > {
164- // If we have a business connection, validate the peer first to avoid redundant retries
165- if ( params . business_connection_id ) {
166- const connectionId = params . business_connection_id . toString ( ) ;
167- let ownerId = TelegramExecutionContext . businessOwners . get ( connectionId ) ;
166+ const connectionId = params . business_connection_id ?. toString ( ) ;
167+
168+ if ( connectionId ) {
169+ if ( TelegramExecutionContext . poisonedConnections . has ( connectionId ) ) {
170+ return null ;
171+ }
168172
173+ let ownerId = TelegramExecutionContext . businessOwners . get ( connectionId ) ;
169174 if ( ownerId === undefined ) {
170175 try {
171176 const response = await this . api . getBusinessConnection ( this . bot . api . toString ( ) , connectionId ) ;
@@ -176,21 +181,21 @@ export default class TelegramExecutionContext {
176181 if ( ownerId ) {
177182 TelegramExecutionContext . businessOwners . set ( connectionId , ownerId ) ;
178183 }
179- // If the bot cannot reply via this connection, we shouldn't attempt it
180184 if ( json . result . can_reply === false ) {
181- console . warn ( 'Bot cannot reply via this business connection' ) ;
185+ TelegramExecutionContext . poisonedConnections . add ( connectionId ) ;
182186 return null ;
183187 }
184188 }
185189 }
186190 } catch ( e ) {
187- console . warn ( 'Failed to fetch business connection info:' , e ) ;
191+ if ( e instanceof Error && e . message === 'BUSINESS_CONNECTION_INVALID' ) {
192+ TelegramExecutionContext . poisonedConnections . add ( connectionId ) ;
193+ return null ;
194+ }
188195 }
189196 }
190197
191- // If the chat_id is the owner of the connection, we cannot use the business connection
192198 if ( ownerId !== undefined && params . chat_id . toString ( ) === ownerId . toString ( ) ) {
193- console . warn ( 'Cannot reply to business account owner via business connection' ) ;
194199 return null ;
195200 }
196201 }
@@ -200,24 +205,19 @@ export default class TelegramExecutionContext {
200205 } catch ( e ) {
201206 if ( e instanceof Error ) {
202207 if ( e . message === 'BUSINESS_CONNECTION_INVALID' ) {
203- console . warn ( 'Business connection invalid, cannot deliver message' ) ;
208+ if ( connectionId ) {
209+ TelegramExecutionContext . poisonedConnections . add ( connectionId ) ;
210+ }
204211 return null ;
205212 }
206213 if ( e . message === 'PEER_ID_INVALID' ) {
207- console . error ( 'Peer invalid, cannot deliver message' ) ;
208214 return null ;
209215 }
210216 }
211217 throw e ;
212218 }
213219 }
214220
215- /**
216- * Reply to the last message with a video
217- * @param video - string to a video on the internet or a file_id on telegram
218- * @param options - any additional options to pass to sendVideo
219- * @returns Promise with the API response
220- */
221221 async replyVideo ( video : string , options : Record < string , number | string | boolean > = { } ) {
222222 const params : any = {
223223 ...options ,
0 commit comments