mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 00:48:27 +00:00
refactor(telegram): simplify send/dispatch/target handling (#17819)
Merged via /review-pr -> /prepare-pr -> /merge-pr.
Prepared head SHA: fcb7aeeca3
Co-authored-by: obviyus <22031114+obviyus@users.noreply.github.com>
Co-authored-by: obviyus <22031114+obviyus@users.noreply.github.com>
Reviewed-by: @obviyus
This commit is contained in:
@@ -26,6 +26,7 @@ import { cacheSticker, getCachedSticker } from "../sticker-cache.js";
|
||||
import { resolveTelegramVoiceSend } from "../voice.js";
|
||||
import {
|
||||
buildTelegramThreadParams,
|
||||
resolveTelegramMediaPlaceholder,
|
||||
resolveTelegramReplyId,
|
||||
type TelegramThreadSpec,
|
||||
} from "./helpers.js";
|
||||
@@ -429,16 +430,7 @@ export async function resolveMedia(
|
||||
throw new Error("fetch is not available; set channels.telegram.proxy in config");
|
||||
}
|
||||
const saved = await downloadAndSaveTelegramFile(file.file_path, fetchImpl);
|
||||
let placeholder = "<media:document>";
|
||||
if (msg.photo) {
|
||||
placeholder = "<media:image>";
|
||||
} else if (msg.video) {
|
||||
placeholder = "<media:video>";
|
||||
} else if (msg.video_note) {
|
||||
placeholder = "<media:video>";
|
||||
} else if (msg.audio || msg.voice) {
|
||||
placeholder = "<media:audio>";
|
||||
}
|
||||
const placeholder = resolveTelegramMediaPlaceholder(msg) ?? "<media:document>";
|
||||
return { path: saved.path, contentType: saved.contentType, placeholder };
|
||||
}
|
||||
|
||||
|
||||
@@ -197,6 +197,33 @@ export function buildSenderName(msg: Message) {
|
||||
return name || undefined;
|
||||
}
|
||||
|
||||
export function resolveTelegramMediaPlaceholder(
|
||||
msg:
|
||||
| Pick<Message, "photo" | "video" | "video_note" | "audio" | "voice" | "document" | "sticker">
|
||||
| undefined
|
||||
| null,
|
||||
): string | undefined {
|
||||
if (!msg) {
|
||||
return undefined;
|
||||
}
|
||||
if (msg.photo) {
|
||||
return "<media:image>";
|
||||
}
|
||||
if (msg.video || msg.video_note) {
|
||||
return "<media:video>";
|
||||
}
|
||||
if (msg.audio || msg.voice) {
|
||||
return "<media:audio>";
|
||||
}
|
||||
if (msg.document) {
|
||||
return "<media:document>";
|
||||
}
|
||||
if (msg.sticker) {
|
||||
return "<media:sticker>";
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function buildSenderLabel(msg: Message, senderId?: number | string) {
|
||||
const name = buildSenderName(msg);
|
||||
const username = msg.from?.username ? `@${msg.from.username}` : undefined;
|
||||
@@ -318,15 +345,8 @@ export function describeReplyTarget(msg: Message): TelegramReplyTarget | null {
|
||||
const replyBody = (replyLike.text ?? replyLike.caption ?? "").trim();
|
||||
body = replyBody;
|
||||
if (!body) {
|
||||
if (replyLike.photo) {
|
||||
body = "<media:image>";
|
||||
} else if (replyLike.video) {
|
||||
body = "<media:video>";
|
||||
} else if (replyLike.audio || replyLike.voice) {
|
||||
body = "<media:audio>";
|
||||
} else if (replyLike.document) {
|
||||
body = "<media:document>";
|
||||
} else {
|
||||
body = resolveTelegramMediaPlaceholder(replyLike) ?? "";
|
||||
if (!body) {
|
||||
const locationData = extractTelegramLocation(replyLike);
|
||||
if (locationData) {
|
||||
body = formatLocationText(locationData);
|
||||
|
||||
Reference in New Issue
Block a user