refactor: share telegram caption splitting
This commit is contained in:
@@ -17,6 +17,7 @@ import { loadWebMedia } from "../web/media.js";
|
||||
import { resolveTelegramAccount } from "./accounts.js";
|
||||
import { resolveTelegramFetch } from "./fetch.js";
|
||||
import { markdownToTelegramHtml } from "./format.js";
|
||||
import { splitTelegramCaption } from "./caption.js";
|
||||
import { recordSentMessage } from "./sent-message-cache.js";
|
||||
import { parseTelegramTarget, stripTelegramInternalPrefixes } from "./targets.js";
|
||||
import { resolveTelegramVoiceSend } from "./voice.js";
|
||||
@@ -58,10 +59,6 @@ type TelegramReactionOpts = {
|
||||
|
||||
const PARSE_ERR_RE = /can't parse entities|parse entities|find end of the entity/i;
|
||||
|
||||
// Telegram limits media captions to 1024 characters.
|
||||
// Text beyond this must be sent as a separate follow-up message.
|
||||
const TELEGRAM_MAX_CAPTION_LENGTH = 1024;
|
||||
|
||||
function resolveToken(explicit: string | undefined, params: { accountId: string; token: string }) {
|
||||
if (explicit?.trim()) return explicit.trim();
|
||||
if (!params.token) {
|
||||
@@ -201,11 +198,10 @@ export async function sendMessageTelegram(
|
||||
});
|
||||
const fileName = media.fileName ?? (isGif ? "animation.gif" : inferFilename(kind)) ?? "file";
|
||||
const file = new InputFile(media.buffer, fileName);
|
||||
const trimmedText = text?.trim() || "";
|
||||
const { caption, followUpText } = splitTelegramCaption(text);
|
||||
// If text exceeds Telegram's caption limit, send media without caption
|
||||
// then send text as a separate follow-up message.
|
||||
const needsSeparateText = trimmedText.length > TELEGRAM_MAX_CAPTION_LENGTH;
|
||||
const caption = needsSeparateText ? undefined : trimmedText || undefined;
|
||||
const needsSeparateText = Boolean(followUpText);
|
||||
// When splitting, put reply_markup only on the follow-up text (the "main" content),
|
||||
// not on the media message.
|
||||
const mediaParams = hasThreadParams
|
||||
@@ -283,7 +279,7 @@ export async function sendMessageTelegram(
|
||||
|
||||
// If text was too long for a caption, send it as a separate follow-up message.
|
||||
// Use plain text to match caption behavior (captions don't use HTML conversion).
|
||||
if (needsSeparateText && trimmedText) {
|
||||
if (needsSeparateText && followUpText) {
|
||||
const textParams =
|
||||
hasThreadParams || replyMarkup
|
||||
? {
|
||||
@@ -294,8 +290,8 @@ export async function sendMessageTelegram(
|
||||
const textRes = await request(
|
||||
() =>
|
||||
textParams
|
||||
? api.sendMessage(chatId, trimmedText, textParams)
|
||||
: api.sendMessage(chatId, trimmedText),
|
||||
? api.sendMessage(chatId, followUpText, textParams)
|
||||
: api.sendMessage(chatId, followUpText),
|
||||
"message",
|
||||
).catch((err) => {
|
||||
throw wrapChatNotFound(err);
|
||||
|
||||
Reference in New Issue
Block a user