Skip to content

Commit c2e351a

Browse files
committed
better handling of spotify link expansion limits
1 parent f7c82b2 commit c2e351a

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

actions/expand-link.ts

+26-13
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,41 @@ export async function expandLink(
9494

9595
// Limit description to 500 chars because Telegram rejects messages with more than 4096 characters.
9696
// 4096 seems a bit excessive to see in the chat so we'll just cut it off at 500.
97-
const limitedDescription = description.length > 500 ? description.slice(0, 500) + "..." : description;
97+
const maxCaptionLength = 500;
98+
99+
// Calculate template length first
100+
const template = await expandedMessageTemplate(
101+
ctx,
102+
userInfo.username,
103+
userInfo.userId,
104+
userInfo.firstName,
105+
userInfo.lastName,
106+
messageText,
107+
linkWithNoTrackers
108+
);
109+
110+
// Calculate remaining space for title and description (using 500 to be safe)
111+
const remainingSpace = Math.max(0, maxCaptionLength - template.length - 4); // 4 chars for "\n\n"
112+
const titleMaxLength = Math.min(50, Math.floor(remainingSpace * 0.3)); // Max 50 chars for title
113+
const descMaxLength = Math.floor(remainingSpace * 0.7); // Rest for description
114+
115+
const truncatedTitle = title.length > titleMaxLength ? title.slice(0, titleMaxLength) + "..." : title;
116+
const truncatedDesc =
117+
description.length > descMaxLength ? description.slice(0, descMaxLength) + "..." : description;
98118

99119
botReply = await ctx.api.sendPhoto(chatId, new InputFile(new URL(`https://wsrv.nl/?url=${image}&w=600`)), {
100120
...replyOptions,
101-
caption:
102-
(await expandedMessageTemplate(
103-
ctx,
104-
userInfo.username,
105-
userInfo.userId,
106-
userInfo.firstName,
107-
userInfo.lastName,
108-
messageText,
109-
linkWithNoTrackers
110-
)) + `\n\n<b>${title}</b>\n${limitedDescription}`,
121+
caption: template + `\n\n<b>${truncatedTitle}</b>\n${truncatedDesc}`,
111122
parse_mode: "HTML",
112123
});
113124

114125
if (audio) {
126+
// Also limit the audio caption
127+
const audioDesc = description.length > 250 ? description.slice(0, 250) + "..." : description;
115128
await ctx.api.sendAudio(chatId, new InputFile(new URL(audio)), {
116129
...replyOptions,
117-
title: title,
118-
caption: limitedDescription,
130+
title: truncatedTitle,
131+
caption: audioDesc,
119132
thumbnail: new InputFile(new URL(`https://wsrv.nl/?url=${image}&w=200&h=200`)),
120133
parse_mode: "HTML",
121134
reply_markup: {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "telegram-twitter-url-expand-bot",
3-
"version": "4.10.3",
3+
"version": "4.10.4",
44
"description": "Replaces Twitter, Instagram, Bluesky, TikTok, Reddit, Spotify,Hacker News, and Posts.cv links in Telegram chats + channels with an expanded preview and inline video.",
55
"main": "index.ts",
66
"repository": "https://github.com/pugson/telegram-twitter-url-expand-bot.git",

0 commit comments

Comments
 (0)