Skip to content

Commit ba36291

Browse files
committed
refactor(telegram): dedupe prompt media paths
1 parent b63acf5 commit ba36291

3 files changed

Lines changed: 38 additions & 68 deletions

File tree

‎extensions/telegram/src/bot-handlers.runtime.ts‎

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Telegram plugin module implements bot handlers behavior.
22
import { randomUUID } from "node:crypto";
3-
import path from "node:path";
43
import type { Message, ReactionTypeEmoji } from "grammy/types";
54
import { parseExecApprovalCommandText } from "openclaw/plugin-sdk/approval-reply-runtime";
65
import { resolveChannelConfigWrites } from "openclaw/plugin-sdk/channel-config-helpers";
@@ -165,41 +164,9 @@ import {
165164
isTelegramEditTargetMissingError,
166165
isTelegramMessageHasNoTextError,
167166
} from "./network-errors.js";
167+
import { resolveTelegramPromptMediaPath } from "./prompt-media-path.js";
168168
import { buildInlineKeyboard } from "./send.js";
169169

170-
function resolveTelegramPromptMediaPath(mediaPath: string): string | undefined {
171-
const toInboundMediaPath = (id: string): string | undefined => {
172-
if (
173-
!id ||
174-
id === "." ||
175-
id === ".." ||
176-
id.includes("/") ||
177-
id.includes("\\") ||
178-
id.includes("\0")
179-
) {
180-
return undefined;
181-
}
182-
return `media://inbound/${encodeURIComponent(id)}`;
183-
};
184-
const decodeInboundMediaId = (id: string): string | undefined => {
185-
try {
186-
return decodeURIComponent(id);
187-
} catch {
188-
return undefined;
189-
}
190-
};
191-
const canonicalMatch = /^media:\/\/inbound\/([^/\\]+)$/i.exec(mediaPath);
192-
if (canonicalMatch?.[1]) {
193-
const id = decodeInboundMediaId(canonicalMatch[1]);
194-
return id ? toInboundMediaPath(id) : undefined;
195-
}
196-
const normalized = mediaPath.replace(/\\/g, "/");
197-
if (!normalized.includes("/media/inbound/")) {
198-
return undefined;
199-
}
200-
return toInboundMediaPath(path.posix.basename(normalized));
201-
}
202-
203170
export const registerTelegramHandlers = ({
204171
cfg,
205172
accountId,

‎extensions/telegram/src/bot-message-context.session.ts‎

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Telegram plugin module implements bot message context.session behavior.
2-
import path from "node:path";
32
import {
43
type BuildChannelInboundEventContextParams,
54
type BuildChannelInboundEventContextAsyncParams,
@@ -34,6 +33,7 @@ import type {
3433
TelegramMessageContextSessionRuntimeOverrides,
3534
TelegramPromptContextEntry,
3635
} from "./bot-message-context.types.js";
36+
import { resolveTelegramPromptMediaPath } from "./prompt-media-path.js";
3737

3838
type TelegramMentionFacts = NonNullable<
3939
NonNullable<BuildChannelInboundEventContextParams["access"]>["mentions"]
@@ -145,39 +145,6 @@ function stripReplyChainForwarded(entry: TelegramReplyChainEntry): TelegramReply
145145
return withoutForwarded;
146146
}
147147

148-
function resolveTelegramPromptMediaPath(mediaPath: string): string | undefined {
149-
const toInboundMediaPath = (id: string): string | undefined => {
150-
if (
151-
!id ||
152-
id === "." ||
153-
id === ".." ||
154-
id.includes("/") ||
155-
id.includes("\\") ||
156-
id.includes("\0")
157-
) {
158-
return undefined;
159-
}
160-
return `media://inbound/${encodeURIComponent(id)}`;
161-
};
162-
const decodeInboundMediaId = (id: string): string | undefined => {
163-
try {
164-
return decodeURIComponent(id);
165-
} catch {
166-
return undefined;
167-
}
168-
};
169-
const canonicalMatch = /^media:\/\/inbound\/([^/\\]+)$/i.exec(mediaPath);
170-
if (canonicalMatch?.[1]) {
171-
const id = decodeInboundMediaId(canonicalMatch[1]);
172-
return id ? toInboundMediaPath(id) : undefined;
173-
}
174-
const normalized = mediaPath.replace(/\\/g, "/");
175-
if (!normalized.includes("/media/inbound/")) {
176-
return undefined;
177-
}
178-
return toInboundMediaPath(path.posix.basename(normalized));
179-
}
180-
181148
function formatReplyChainEntry(entry: TelegramReplyChainEntry, index: number): string {
182149
const forwardedAt = timestampMsToIsoString(entry.forwardedDate);
183150
const mediaPath = entry.mediaPath ? resolveTelegramPromptMediaPath(entry.mediaPath) : undefined;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import path from "node:path";
2+
3+
function toInboundMediaPath(id: string): string | undefined {
4+
if (
5+
!id ||
6+
id === "." ||
7+
id === ".." ||
8+
id.includes("/") ||
9+
id.includes("\\") ||
10+
id.includes("\0")
11+
) {
12+
return undefined;
13+
}
14+
return `media://inbound/${encodeURIComponent(id)}`;
15+
}
16+
17+
function decodeInboundMediaId(id: string): string | undefined {
18+
try {
19+
return decodeURIComponent(id);
20+
} catch {
21+
return undefined;
22+
}
23+
}
24+
25+
export function resolveTelegramPromptMediaPath(mediaPath: string): string | undefined {
26+
const canonicalMatch = /^media:\/\/inbound\/([^/\\]+)$/i.exec(mediaPath);
27+
if (canonicalMatch?.[1]) {
28+
const id = decodeInboundMediaId(canonicalMatch[1]);
29+
return id ? toInboundMediaPath(id) : undefined;
30+
}
31+
const normalized = mediaPath.replace(/\\/g, "/");
32+
if (!normalized.includes("/media/inbound/")) {
33+
return undefined;
34+
}
35+
return toInboundMediaPath(path.posix.basename(normalized));
36+
}

0 commit comments

Comments
 (0)