mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-21 01:23:40 +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 MarkdownIt from "markdown-it";
|
||||||
import type { RouteTarget, Env, NeutralMessage } from "../../types";
|
import type { RouteTarget, Env, NeutralMessage } from "../../types";
|
||||||
import type { PlatformDriver, SendResult } from "../types";
|
import type { PlatformDriver, SendResult } from "../types";
|
||||||
import { sendMessage, sendPhoto, editMessageText, editMessageCaption } from "./rest";
|
import { sendMessage, editMessageText } from "./rest";
|
||||||
import { renderNeutralMessage } from "./render";
|
import { renderNeutralMessage } from "./render";
|
||||||
|
|
||||||
const markdown = new MarkdownIt({ html: false, linkify: false, breaks: false, typographer: false });
|
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()}`;
|
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 {
|
export class TelegramDriver implements PlatformDriver {
|
||||||
readonly id = "telegram";
|
readonly id = "telegram";
|
||||||
|
|
||||||
@@ -33,16 +46,7 @@ export class TelegramDriver implements PlatformDriver {
|
|||||||
}
|
}
|
||||||
const token = env.TELEGRAM_TOKEN ?? "";
|
const token = env.TELEGRAM_TOKEN ?? "";
|
||||||
const text = renderNeutralMessage(message);
|
const text = renderNeutralMessage(message);
|
||||||
const avatar = message.author?.iconUrl;
|
return sendMessage(token, chatId, text, target.topicId, previewUrl(message, env));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async edit(
|
async edit(
|
||||||
@@ -57,15 +61,15 @@ export class TelegramDriver implements PlatformDriver {
|
|||||||
}
|
}
|
||||||
const token = env.TELEGRAM_TOKEN ?? "";
|
const token = env.TELEGRAM_TOKEN ?? "";
|
||||||
const text = renderNeutralMessage(message);
|
const text = renderNeutralMessage(message);
|
||||||
const avatar = message.author?.iconUrl;
|
// A photo sent by an older version has no text to edit; Telegram's error
|
||||||
const richHeaderHost = env.TELEGRAM_RICH_HEADER_HOST ?? env.BASE_URL;
|
// then makes dispatch send a fresh message instead.
|
||||||
if (avatar && richHeaderHost) {
|
return editMessageText(
|
||||||
const rhUrl = richHeaderUrl(message, avatar, richHeaderHost);
|
token,
|
||||||
return editMessageText(token, chatId, messageId, text, target.topicId, rhUrl);
|
chatId,
|
||||||
}
|
messageId,
|
||||||
if (avatar) {
|
text,
|
||||||
return editMessageCaption(token, chatId, messageId, text, target.topicId);
|
target.topicId,
|
||||||
}
|
previewUrl(message, env),
|
||||||
return editMessageText(token, chatId, messageId, text, target.topicId);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+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 capturedInit: RequestInit | undefined;
|
||||||
|
let capturedUrl = "";
|
||||||
mockFetch((url, init) => {
|
mockFetch((url, init) => {
|
||||||
|
capturedUrl = url;
|
||||||
capturedInit = init;
|
capturedInit = init;
|
||||||
return new Response(JSON.stringify({ ok: true, result: { message_id: 9 } }), { status: 200 });
|
return new Response(JSON.stringify({ ok: true, result: { message_id: 9 } }), { status: 200 });
|
||||||
});
|
});
|
||||||
@@ -242,8 +244,15 @@ describe("TelegramDriver", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
expect(result.ok).toBe(true);
|
expect(result.ok).toBe(true);
|
||||||
const body = JSON.parse(String(capturedInit!.body)) as Record<string, unknown>;
|
// A text message (never a captioned photo), so later edits keep the full
|
||||||
expect(body.photo).toBe("https://avatars.githubusercontent.com/u/1?v=4&s=64");
|
// 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 () => {
|
it("sends a plain message when the author has no icon", async () => {
|
||||||
|
|||||||
Reference in new issue
Block a user