Skip to content

Commit 62342d8

Browse files
committed
try to fix business_message
1 parent 0aeb958 commit 62342d8

2 files changed

Lines changed: 73 additions & 25 deletions

File tree

src/telegram_api.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,16 @@ export default class TelegramApi {
154154
let errorDescription = '';
155155
try {
156156
const json = (await cloned.json()) as { description?: string };
157-
errorDescription = json.description ? `: ${json.description}` : '';
157+
errorDescription = json.description || '';
158158
} catch {
159159
// ignore
160160
}
161-
throw new Error(`Telegram API error: ${String(response.status)} ${response.statusText}${errorDescription}`);
161+
162+
if (errorDescription.includes('BUSINESS_CONNECTION_INVALID')) {
163+
throw new Error('BUSINESS_CONNECTION_INVALID');
164+
}
165+
166+
throw new Error(`Telegram API error: ${String(response.status)} ${response.statusText}${errorDescription ? ': ' + errorDescription : ''}`);
162167
}
163168
const cloned = response.clone();
164169
try {

src/telegram_execution_context.ts

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,28 @@ export default class TelegramExecutionContext {
388388
const business_connection_id = this.update.business_message?.business_connection_id?.toString();
389389

390390
if (message_id) {
391-
return await this.api.editMessageText(this.bot.api.toString(), {
392-
chat_id: this.getChatId(),
393-
message_id,
394-
text: message,
395-
parse_mode,
396-
business_connection_id,
397-
...options,
398-
});
391+
try {
392+
return await this.api.editMessageText(this.bot.api.toString(), {
393+
chat_id: this.getChatId(),
394+
message_id,
395+
text: message,
396+
parse_mode,
397+
business_connection_id,
398+
...options,
399+
});
400+
} catch (e) {
401+
if (e instanceof Error && e.message === 'BUSINESS_CONNECTION_INVALID') {
402+
console.warn('Business connection invalid, retrying without business_connection_id');
403+
return await this.api.editMessageText(this.bot.api.toString(), {
404+
chat_id: this.getChatId(),
405+
message_id,
406+
text: message,
407+
parse_mode,
408+
...options,
409+
});
410+
}
411+
throw e;
412+
}
399413
}
400414

401415
if (this.update_type === 'guest_message') {
@@ -406,14 +420,30 @@ export default class TelegramExecutionContext {
406420
return await this.answerGuestQueryText(message, parse_mode);
407421
}
408422

409-
const response = await this.api.sendMessage(this.bot.api.toString(), {
410-
...options,
411-
chat_id: this.getChatId(),
412-
message_thread_id: this.getThreadId(),
413-
text: message,
414-
parse_mode,
415-
business_connection_id,
416-
});
423+
let response: Response;
424+
try {
425+
response = await this.api.sendMessage(this.bot.api.toString(), {
426+
...options,
427+
chat_id: this.getChatId(),
428+
message_thread_id: this.getThreadId(),
429+
text: message,
430+
parse_mode,
431+
business_connection_id,
432+
});
433+
} catch (e) {
434+
if (e instanceof Error && e.message === 'BUSINESS_CONNECTION_INVALID') {
435+
console.warn('Business connection invalid, retrying without business_connection_id');
436+
response = await this.api.sendMessage(this.bot.api.toString(), {
437+
...options,
438+
chat_id: this.getChatId(),
439+
message_thread_id: this.getThreadId(),
440+
text: message,
441+
parse_mode,
442+
});
443+
} else {
444+
throw e;
445+
}
446+
}
417447

418448
if (response.status === 200) {
419449
const cloned = response.clone();
@@ -463,13 +493,26 @@ export default class TelegramExecutionContext {
463493
case 'guest_message':
464494
return await this.answerGuestQueryText(message, parse_mode);
465495
case 'business_message':
466-
return await this.api.sendMessage(this.bot.api.toString(), {
467-
chat_id: this.getChatId(),
468-
message_thread_id: this.getThreadId(),
469-
text: message,
470-
business_connection_id: this.update.business_message?.business_connection_id?.toString() ?? '',
471-
parse_mode,
472-
});
496+
try {
497+
return await this.api.sendMessage(this.bot.api.toString(), {
498+
chat_id: this.getChatId(),
499+
message_thread_id: this.getThreadId(),
500+
text: message,
501+
business_connection_id: this.update.business_message?.business_connection_id?.toString() ?? '',
502+
parse_mode,
503+
});
504+
} catch (e) {
505+
if (e instanceof Error && e.message === 'BUSINESS_CONNECTION_INVALID') {
506+
console.warn('Business connection invalid, retrying without business_connection_id');
507+
return await this.api.sendMessage(this.bot.api.toString(), {
508+
chat_id: this.getChatId(),
509+
message_thread_id: this.getThreadId(),
510+
text: message,
511+
parse_mode,
512+
});
513+
}
514+
throw e;
515+
}
473516
case 'callback':
474517
if (this.update.callback_query?.message?.chat.id) {
475518
return await this.api.sendMessage(this.bot.api.toString(), {

0 commit comments

Comments
 (0)