mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-20 17:13:49 +00:00
fix(telegram): keep editable messages as text
Guessing photo versus text from message content makes later edits use the wrong API and a smaller caption budget. Always send text and use the avatar as a link preview.
This commit is contained in:
1 parent
867d2af6ac
commit
dd45e13cb1
2 files changed
+37
-24
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import MarkdownIt from "markdown-it";
|
||||
import type { RouteTarget, Env, NeutralMessage } from "../../types";
|
||||
import type { PlatformDriver, SendResult } from "../types";
|
||||
import { sendMessage, sendPhoto, editMessageText, editMessageCaption } from "./rest";
|
||||
import { sendMessage, editMessageText } from "./rest";
|
||||
import { renderNeutralMessage } from "./render";
|
||||
|
||||
const markdown = new MarkdownIt({ html: false, linkify: false, breaks: false, typographer: false });
|
||||
@@ -23,6 +23,19 @@ function richHeaderUrl(message: NeutralMessage, avatar: string, host: string): s
|
||||
return `${host.replace(/\/+$/, "")}/api/richheader?${params.toString()}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The avatar shown above the text as a link preview: the rich-header card
|
||||
* when a host serves it, the plain (small) avatar image otherwise. Messages
|
||||
* are always text messages (never photos with captions), so every edit is an
|
||||
* `editMessageText` with the full 4096-character budget.
|
||||
*/
|
||||
function previewUrl(message: NeutralMessage, env: Env): string | undefined {
|
||||
const avatar = message.author?.iconUrl;
|
||||
if (!avatar) return undefined;
|
||||
const richHeaderHost = env.TELEGRAM_RICH_HEADER_HOST ?? env.BASE_URL;
|
||||
return richHeaderHost ? richHeaderUrl(message, avatar, richHeaderHost) : smallAvatar(avatar);
|
||||
}
|
||||
|
||||
export class TelegramDriver implements PlatformDriver {
|
||||
readonly id = "telegram";
|
||||
|
||||
@@ -33,16 +46,7 @@ export class TelegramDriver implements PlatformDriver {
|
||||
}
|
||||
const token = env.TELEGRAM_TOKEN ?? "";
|
||||
const text = renderNeutralMessage(message);
|
||||
const avatar = message.author?.iconUrl;
|
||||
const richHeaderHost = env.TELEGRAM_RICH_HEADER_HOST ?? env.BASE_URL;
|
||||
if (avatar && richHeaderHost) {
|
||||
const rhUrl = richHeaderUrl(message, avatar, richHeaderHost);
|
||||
return sendMessage(token, chatId, text, target.topicId, rhUrl);
|
||||
}
|
||||
if (avatar) {
|
||||
return sendPhoto(token, chatId, smallAvatar(avatar), text, target.topicId);
|
||||
}
|
||||
return sendMessage(token, chatId, text, target.topicId);
|
||||
return sendMessage(token, chatId, text, target.topicId, previewUrl(message, env));
|
||||
}
|
||||
|
||||
async edit(
|
||||
@@ -57,15 +61,15 @@ export class TelegramDriver implements PlatformDriver {
|
||||
}
|
||||
const token = env.TELEGRAM_TOKEN ?? "";
|
||||
const text = renderNeutralMessage(message);
|
||||
const avatar = message.author?.iconUrl;
|
||||
const richHeaderHost = env.TELEGRAM_RICH_HEADER_HOST ?? env.BASE_URL;
|
||||
if (avatar && richHeaderHost) {
|
||||
const rhUrl = richHeaderUrl(message, avatar, richHeaderHost);
|
||||
return editMessageText(token, chatId, messageId, text, target.topicId, rhUrl);
|
||||
}
|
||||
if (avatar) {
|
||||
return editMessageCaption(token, chatId, messageId, text, target.topicId);
|
||||
}
|
||||
return editMessageText(token, chatId, messageId, text, target.topicId);
|
||||
// A photo sent by an older version has no text to edit; Telegram's error
|
||||
// then makes dispatch send a fresh message instead.
|
||||
return editMessageText(
|
||||
token,
|
||||
chatId,
|
||||
messageId,
|
||||
text,
|
||||
target.topicId,
|
||||
previewUrl(message, env),
|
||||
);
|
||||
}
|
||||
}
|
||||
+12
-3
@@ -224,9 +224,11 @@ describe("TelegramDriver", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("sends a small avatar photo (s=64) when the author has an icon", async () => {
|
||||
it("shows a small avatar (s=64) as the link preview of a text message", async () => {
|
||||
let capturedInit: RequestInit | undefined;
|
||||
let capturedUrl = "";
|
||||
mockFetch((url, init) => {
|
||||
capturedUrl = url;
|
||||
capturedInit = init;
|
||||
return new Response(JSON.stringify({ ok: true, result: { message_id: 9 } }), { status: 200 });
|
||||
});
|
||||
@@ -242,8 +244,15 @@ describe("TelegramDriver", () => {
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
const body = JSON.parse(String(capturedInit!.body)) as Record<string, unknown>;
|
||||
expect(body.photo).toBe("https://avatars.githubusercontent.com/u/1?v=4&s=64");
|
||||
// A text message (never a captioned photo), so later edits keep the full
|
||||
// 4096-character budget.
|
||||
expect(capturedUrl).toContain("/sendMessage");
|
||||
const body = JSON.parse(String(capturedInit!.body)) as {
|
||||
link_preview_options?: { url?: string };
|
||||
};
|
||||
expect(body.link_preview_options?.url).toBe(
|
||||
"https://avatars.githubusercontent.com/u/1?v=4&s=64",
|
||||
);
|
||||
});
|
||||
|
||||
it("sends a plain message when the author has no icon", async () => {
|
||||
|
||||
Reference in new issue
Block a user