feat: use Telegram native Rich Message for assistant replies
Co-authored-by: MuziIsabel <MuziIsabel@users.noreply.github.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import { catalogFromLiveAgents, loadCatalog, truncateButton, visibleSessionsForW
|
||||
import { TelegramClient, } from './client.js';
|
||||
import { MSG, LAST_CB, parseCommand } from './commands.js';
|
||||
import { markdownToHtml, splitMessage } from './format.js';
|
||||
import { splitRichMarkdown } from './rich-format.js';
|
||||
import { formatLastTurn, loadLastTurn } from './history.js';
|
||||
import { describeAgent, displayLabel } from './label.js';
|
||||
import { formatModel, loadSessionModels, selectSessionModel, } from './models.js';
|
||||
@@ -28,6 +29,10 @@ function contentToText(content) {
|
||||
.map((block) => block.text)
|
||||
.join('');
|
||||
}
|
||||
function isRichUnsupportedError(err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
return /method not found|unknown method|not found|404|bad request|can't parse|rich message/i.test(message);
|
||||
}
|
||||
export class TelegramBridge {
|
||||
ctx;
|
||||
token;
|
||||
@@ -36,6 +41,7 @@ export class TelegramBridge {
|
||||
client;
|
||||
sleep;
|
||||
maxMessageLength;
|
||||
renderingMode;
|
||||
bindings = new Map();
|
||||
pickers = new Map();
|
||||
/** chatId → model awaiting reasoning-effort pick (kept outside picker so list refreshes won't drop it). */
|
||||
@@ -55,6 +61,7 @@ export class TelegramBridge {
|
||||
});
|
||||
this.sleep = options.sleep ?? defaultSleep;
|
||||
this.maxMessageLength = options.maxMessageLength ?? 4096;
|
||||
this.renderingMode = options.rendering === 'html' ? 'html' : 'rich';
|
||||
}
|
||||
start() {
|
||||
this.disposeSessionListener?.();
|
||||
@@ -603,6 +610,29 @@ export class TelegramBridge {
|
||||
}
|
||||
}
|
||||
async deliver(chatId, markdown) {
|
||||
if (this.renderingMode === 'html') {
|
||||
await this.deliverHtml(chatId, markdown);
|
||||
return;
|
||||
}
|
||||
let sentAny = false;
|
||||
try {
|
||||
const chunks = splitRichMarkdown(markdown);
|
||||
for (const chunk of chunks) {
|
||||
await this.client.sendRichMessage(chatId, chunk);
|
||||
sentAny = true;
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
if (!sentAny && isRichUnsupportedError(err)) {
|
||||
this.ctx.logger.warn('dsh-telegram-channel: Rich Message API unavailable, falling back to HTML rendering');
|
||||
this.renderingMode = 'html';
|
||||
await this.deliverHtml(chatId, markdown);
|
||||
return;
|
||||
}
|
||||
this.ctx.logger.error(this.redact(err));
|
||||
}
|
||||
}
|
||||
async deliverHtml(chatId, markdown) {
|
||||
const chunks = splitMessage(markdown, this.maxMessageLength);
|
||||
for (const chunk of chunks) {
|
||||
const html = markdownToHtml(chunk);
|
||||
|
||||
Reference in New Issue
Block a user