Skip to content

Commit 492b884

Browse files
fix(line): skip invalid location messages before delivery (#118064)
Co-authored-by: Tak Hoffman <[email protected]>
1 parent b5a7bc2 commit 492b884

9 files changed

Lines changed: 152 additions & 38 deletions

‎extensions/line/src/auto-reply-delivery.test-helpers.ts‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ export const createImageMessage = (url: string) => ({
2828
previewImageUrl: url,
2929
});
3030

31-
const createLocationMessage = (location: {
32-
title: string;
33-
address: string;
34-
latitude: number;
35-
longitude: number;
36-
}) => ({
37-
type: "location" as const,
38-
...location,
39-
});
31+
const createLocationMessage: LineAutoReplyDeps["createLocationMessage"] = (location) =>
32+
location.title.trim() && location.address.trim()
33+
? {
34+
type: "location" as const,
35+
...location,
36+
}
37+
: null;
4038

4139
export function createDeps(overrides?: Partial<LineAutoReplyDeps>) {
4240
const replyMessageLine = vi.fn<LineAutoReplyDeps["replyMessageLine"]>(async () => ({}));

‎extensions/line/src/auto-reply-delivery.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,39 @@ describe("deliverLineAutoReply", () => {
101101
expect(result.visibleReplySent).toBe(true);
102102
});
103103

104+
it.each([
105+
{ name: "title", title: " ", address: "1 Main Street" },
106+
{ name: "address", title: "Meet here", address: " " },
107+
])("skips a direct location with a blank $name while delivering text", async (location) => {
108+
const lineData = {
109+
location: { ...location, latitude: 35.6895, longitude: 139.6917 },
110+
};
111+
const createLocationMessage = vi.fn<LineAutoReplyDeps["createLocationMessage"]>((value) =>
112+
value.title.trim() && value.address.trim()
113+
? {
114+
type: "location" as const,
115+
...value,
116+
}
117+
: null,
118+
);
119+
const { deps, replyMessageLine } = createDeps({ createLocationMessage });
120+
121+
const result = await deliverLineAutoReply({
122+
...baseDeliveryParams,
123+
payload: { text: "Meet me there.", channelData: { line: lineData } },
124+
lineData,
125+
deps,
126+
});
127+
128+
expect(replyMessageLine).toHaveBeenCalledExactlyOnceWith(
129+
"token",
130+
[{ type: "text", text: "Meet me there." }],
131+
{ cfg: LINE_TEST_CFG, accountId: "acc" },
132+
);
133+
expect(createLocationMessage).toHaveBeenCalledOnce();
134+
expect(result.visibleReplySent).toBe(true);
135+
});
136+
104137
it("keeps an extracted markdown table on the reply token alongside text", async () => {
105138
// Tables are lifted out of the text into their own Flex bubble, which is the
106139
// shape that used to reach the quota-bound push path and vanish on a 429.

‎extensions/line/src/auto-reply-delivery.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type LineAutoReplyDeps = {
4141
address: string;
4242
latitude: number;
4343
longitude: number;
44-
}) => messagingApi.LocationMessage;
44+
}) => messagingApi.LocationMessage | null;
4545
replyMessageLine: (
4646
replyToken: string,
4747
messages: messagingApi.Message[],
@@ -241,7 +241,10 @@ export async function deliverLineAutoReply(params: {
241241
}
242242

243243
if (lineData.location) {
244-
richMessages.push(deps.createLocationMessage(lineData.location));
244+
const locationMessage = deps.createLocationMessage(lineData.location);
245+
if (locationMessage) {
246+
richMessages.push(locationMessage);
247+
}
245248
}
246249

247250
// Inbound auto-replies bypass the channel outbound adapter, so enforce the

‎extensions/line/src/channel.sendPayload.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,71 @@ describe("line outbound sendPayload", () => {
217217
expect(mocks.pushMessagesLine).not.toHaveBeenCalled();
218218
});
219219

220+
it.each([
221+
{ name: "title", title: " ", address: "1 Main Street" },
222+
{ name: "address", title: "Meet here", address: " " },
223+
])("skips a direct location with a blank $name while delivering text", async (location) => {
224+
const { runtime, mocks } = createRuntime();
225+
setLineRuntime(runtime);
226+
227+
await lineOutboundAdapter.sendPayload!({
228+
to: "line:user:U123",
229+
text: "Meet me there.",
230+
payload: {
231+
text: "Meet me there.",
232+
channelData: {
233+
line: {
234+
location: { ...location, latitude: 35.6895, longitude: 139.6917 },
235+
},
236+
},
237+
},
238+
accountId: "default",
239+
cfg: { channels: { line: {} } } as OpenClawConfig,
240+
});
241+
242+
expect(mocks.pushLocationMessage).not.toHaveBeenCalled();
243+
expect(mocks.pushMessageLine).toHaveBeenCalledWith(
244+
"line:user:U123",
245+
"Meet me there.",
246+
expect.any(Object),
247+
);
248+
});
249+
250+
it("omits an invalid direct location from the quick-reply inline batch", async () => {
251+
const { runtime, mocks } = createRuntime();
252+
setLineRuntime(runtime);
253+
254+
await lineOutboundAdapter.sendPayload!({
255+
to: "line:user:U123",
256+
text: "",
257+
payload: {
258+
text: "",
259+
channelData: {
260+
line: {
261+
quickReplies: ["Continue"],
262+
location: {
263+
title: "Meet here",
264+
address: " ",
265+
latitude: 35.6895,
266+
longitude: 139.6917,
267+
},
268+
},
269+
},
270+
},
271+
accountId: "default",
272+
cfg: { channels: { line: {} } } as OpenClawConfig,
273+
});
274+
275+
expect(mocks.pushLocationMessage).not.toHaveBeenCalled();
276+
expect(mocks.pushMessagesLine).not.toHaveBeenCalled();
277+
expect(mocks.pushTextMessageWithQuickReplies).toHaveBeenCalledWith(
278+
"line:user:U123",
279+
expect.stringContaining("Continue"),
280+
["Continue"],
281+
expect.any(Object),
282+
);
283+
});
284+
220285
it("preserves the finalized receipt when its delivery observer rejects", async () => {
221286
const { runtime } = createRuntime();
222287
setLineRuntime(runtime);

‎extensions/line/src/outbound.runtime.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export { processLineMessage } from "./markdown-to-line.js";
33
export {
44
createFlexMessage,
5+
createLocationMessage,
56
createQuickReplyItems,
67
pushFlexMessage,
78
pushLocationMessage,

‎extensions/line/src/outbound.ts‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
1515
import { resolveOutboundMediaUrls } from "openclaw/plugin-sdk/reply-payload";
1616
import { sanitizeAssistantVisibleText } from "openclaw/plugin-sdk/text-chunking";
17-
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
1817
import type { ChannelPlugin, ResolvedLineAccount } from "./channel-api.js";
1918
import {
2019
buildLineMediaMessage,
@@ -38,6 +37,9 @@ export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>
3837
const outboundRuntime = await loadLineOutboundRuntime();
3938
const lineData = (payload.channelData?.line as LineChannelData | undefined) ?? {};
4039
const lineRuntime = runtime.channel.line;
40+
const location = lineData.location;
41+
const locationMessage = location ? outboundRuntime.createLocationMessage(location) : null;
42+
const validLocation = locationMessage ? location : undefined;
4143
const sendText = lineRuntime?.pushMessageLine ?? outboundRuntime.pushMessageLine;
4244
const sendBatch = lineRuntime?.pushMessagesLine ?? outboundRuntime.pushMessagesLine;
4345
const sendFlex = lineRuntime?.pushFlexMessage ?? outboundRuntime.pushFlexMessage;
@@ -181,9 +183,9 @@ export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>
181183
}
182184
}
183185

184-
if (lineData.location) {
186+
if (validLocation) {
185187
await recordResult(
186-
sendLocation(to, lineData.location, {
188+
sendLocation(to, validLocation, {
187189
verbose: false,
188190
cfg,
189191
accountId: accountId ?? undefined,
@@ -281,14 +283,8 @@ export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>
281283
quickReplyMessages.push(template);
282284
}
283285
}
284-
if (lineData.location) {
285-
quickReplyMessages.push({
286-
type: "location",
287-
title: truncateUtf16Safe(lineData.location.title, 100),
288-
address: truncateUtf16Safe(lineData.location.address, 100),
289-
latitude: lineData.location.latitude,
290-
longitude: lineData.location.longitude,
291-
});
286+
if (locationMessage) {
287+
quickReplyMessages.push(locationMessage);
292288
}
293289
for (const flexMsg of processed.flexMessages) {
294290
quickReplyMessages.push(

‎extensions/line/src/reply-payload-transform.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ describe("parseLineDirectives", () => {
103103
text: "[[location: Place | Address | invalid | 139.7]]",
104104
location: undefined,
105105
},
106+
{
107+
text: "Meet me there. [[location: Blue Bottle | | 35.6895 | 139.6917]]",
108+
location: undefined,
109+
outputText: "Meet me there.",
110+
},
111+
{
112+
text: "[[location: | Tokyo, Japan | 35.6812 | 139.7671]]",
113+
location: {
114+
title: "Location",
115+
address: "Tokyo, Japan",
116+
latitude: 35.6812,
117+
longitude: 139.7671,
118+
},
119+
},
106120
{
107121
text: "[[location: New | New Addr | 35.6 | 139.7]]",
108122
channelData: { line: { location: existing } },

‎extensions/line/src/reply-payload-transform.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ export function parseLineDirectives(payload: ReplyPayload): ReplyPayload {
8484
const lonStr = expectDefined(parts[3], "location longitude field");
8585
const latitude = parseStrictFiniteNumber(latStr);
8686
const longitude = parseStrictFiniteNumber(lonStr);
87-
if (latitude !== undefined && longitude !== undefined) {
87+
if (address && latitude !== undefined && longitude !== undefined) {
8888
lineData.location = {
8989
title: title || "Location",
90-
address: address || "",
90+
address,
9191
latitude,
9292
longitude,
9393
};

‎extensions/line/src/send.ts‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { messageAction, normalizeLineMessageActions } from "./actions.js";
1414
import { resolveLineChannelAccessToken } from "./channel-access-token.js";
1515
import { validateLineMediaUrl } from "./outbound-media.js";
1616
import { createLineSendReceipt } from "./send-receipt.js";
17-
import type { LineOutboundMediaKind, LineSendResult } from "./types.js";
17+
import type { LineChannelData, LineOutboundMediaKind, LineSendResult } from "./types.js";
1818

1919
type Message = messagingApi.Message;
2020
type TextMessage = messagingApi.TextMessage;
@@ -26,6 +26,7 @@ type FlexContainer = messagingApi.FlexContainer;
2626
type TemplateMessage = messagingApi.TemplateMessage;
2727
type QuickReply = messagingApi.QuickReply;
2828
type QuickReplyItem = messagingApi.QuickReplyItem;
29+
type LineLocation = NonNullable<LineChannelData["location"]>;
2930

3031
const userProfileCache = new Map<
3132
string,
@@ -241,12 +242,16 @@ export function createAudioMessage(originalContentUrl: string, durationMs: numbe
241242
};
242243
}
243244

244-
export function createLocationMessage(location: {
245-
title: string;
246-
address: string;
247-
latitude: number;
248-
longitude: number;
249-
}): LocationMessage {
245+
function isValidLineLocation(location: LineLocation): boolean {
246+
// LINE rejects either blank required field atomically, so every delivery path
247+
// must use this gate before adding a location to a provider request.
248+
return location.title.trim().length > 0 && location.address.trim().length > 0;
249+
}
250+
251+
export function createLocationMessage(location: LineLocation): LocationMessage | null {
252+
if (!isValidLineLocation(location)) {
253+
return null;
254+
}
250255
return {
251256
type: "location",
252257
title: truncateUtf16Safe(location.title, 100),
@@ -503,15 +508,14 @@ export async function pushImageMessage(
503508

504509
export async function pushLocationMessage(
505510
to: string,
506-
location: {
507-
title: string;
508-
address: string;
509-
latitude: number;
510-
longitude: number;
511-
},
511+
location: LineLocation,
512512
opts: LinePushOpts,
513513
): Promise<LineSendResult> {
514-
return pushLineMessages(to, [createLocationMessage(location)], opts, {
514+
const message = createLocationMessage(location);
515+
if (!message) {
516+
throw new Error("LINE location title and address must be non-empty");
517+
}
518+
return pushLineMessages(to, [message], opts, {
515519
verboseMessage: (chatId) => `line: pushed location to ${chatId}`,
516520
});
517521
}

0 commit comments

Comments
 (0)