Skip to content

Commit a483d83

Browse files
authored
refactor(line): reuse outbound send options (#131675)
1 parent 9ba97a4 commit a483d83

1 file changed

Lines changed: 12 additions & 69 deletions

File tree

‎extensions/line/src/outbound.ts‎

Lines changed: 12 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>
6262
const buildTemplate =
6363
lineRuntime?.buildTemplateMessageFromPayload ??
6464
outboundRuntime.buildTemplateMessageFromPayload;
65+
const sendOptions = { verbose: false, cfg, accountId: accountId ?? undefined };
6566

6667
let lastResult: LineSendResult | null = null;
6768
const recordResult = async (
@@ -102,13 +103,7 @@ export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>
102103
}
103104
for (let i = 0; i < messages.length; i += 5) {
104105
const batch = messages.slice(i, i + 5) as unknown as Parameters<typeof sendBatch>[1];
105-
await recordResult(
106-
sendBatch(to, batch, {
107-
verbose: false,
108-
cfg,
109-
accountId: accountId ?? undefined,
110-
}),
111-
);
106+
await recordResult(sendBatch(to, batch, sendOptions));
112107
}
113108
};
114109

@@ -117,13 +112,7 @@ export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>
117112
await sendMessageBatch([{ type: "text", text, quickReply }]);
118113
return;
119114
}
120-
await recordResult(
121-
sendQuickReplies(to, text, quickReplies, {
122-
verbose: false,
123-
cfg,
124-
accountId: accountId ?? undefined,
125-
}),
126-
);
115+
await recordResult(sendQuickReplies(to, text, quickReplies, sendOptions));
127116
};
128117

129118
const processed = payload.text
@@ -167,25 +156,21 @@ export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>
167156
if (!useLineSpecificMedia) {
168157
await recordResult(
169158
(lineRuntime?.sendMessageLine ?? outboundRuntime.sendMessageLine)(to, "", {
170-
verbose: false,
159+
...sendOptions,
171160
mediaUrl: trimmed,
172-
cfg,
173-
accountId: accountId ?? undefined,
174161
}),
175162
);
176163
continue;
177164
}
178165
const resolved = await resolveLineOutboundMedia(trimmed, mediaOptions);
179166
await recordResult(
180167
(lineRuntime?.sendMessageLine ?? outboundRuntime.sendMessageLine)(to, "", {
181-
verbose: false,
168+
...sendOptions,
182169
mediaUrl: resolved.mediaUrl,
183170
mediaKind: resolved.mediaKind,
184171
previewImageUrl: resolved.previewImageUrl,
185172
durationMs: resolved.durationMs,
186173
trackingId: resolved.trackingId,
187-
cfg,
188-
accountId: accountId ?? undefined,
189174
}),
190175
);
191176
}
@@ -194,47 +179,23 @@ export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>
194179
if (!shouldSendQuickRepliesInline) {
195180
if (lineData.flexMessage) {
196181
const flexContents = lineData.flexMessage.contents as Parameters<typeof sendFlex>[2];
197-
await recordResult(
198-
sendFlex(to, lineData.flexMessage.altText, flexContents, {
199-
verbose: false,
200-
cfg,
201-
accountId: accountId ?? undefined,
202-
}),
203-
);
182+
await recordResult(sendFlex(to, lineData.flexMessage.altText, flexContents, sendOptions));
204183
}
205184

206185
if (lineData.templateMessage) {
207186
const template = buildTemplate(lineData.templateMessage);
208187
if (template) {
209-
await recordResult(
210-
sendTemplate(to, template, {
211-
verbose: false,
212-
cfg,
213-
accountId: accountId ?? undefined,
214-
}),
215-
);
188+
await recordResult(sendTemplate(to, template, sendOptions));
216189
}
217190
}
218191

219192
if (location) {
220-
await recordResult(
221-
sendLocation(to, location, {
222-
verbose: false,
223-
cfg,
224-
accountId: accountId ?? undefined,
225-
}),
226-
);
193+
await recordResult(sendLocation(to, location, sendOptions));
227194
}
228195

229196
if (!orderedMessages) {
230197
for (const flexMsg of processed.flexMessages) {
231-
await recordResult(
232-
sendFlex(to, flexMsg.altText, flexMsg.contents, {
233-
verbose: false,
234-
cfg,
235-
accountId: accountId ?? undefined,
236-
}),
237-
);
198+
await recordResult(sendFlex(to, flexMsg.altText, flexMsg.contents, sendOptions));
238199
}
239200
}
240201
}
@@ -251,24 +212,12 @@ export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>
251212
if (isLast && quickReply) {
252213
await sendMessageBatch([{ ...message, quickReply }]);
253214
} else {
254-
await recordResult(
255-
sendFlex(to, message.altText, message.contents, {
256-
verbose: false,
257-
cfg,
258-
accountId: accountId ?? undefined,
259-
}),
260-
);
215+
await recordResult(sendFlex(to, message.altText, message.contents, sendOptions));
261216
}
262217
} else if (isLast && hasQuickReplies) {
263218
await sendTextWithQuickReply(message.text);
264219
} else {
265-
await recordResult(
266-
sendText(to, message.text, {
267-
verbose: false,
268-
cfg,
269-
accountId: accountId ?? undefined,
270-
}),
271-
);
220+
await recordResult(sendText(to, message.text, sendOptions));
272221
}
273222
}
274223
} else if (chunks.length > 0) {
@@ -277,13 +226,7 @@ export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>
277226
if (isLast && hasQuickReplies) {
278227
await sendTextWithQuickReply(chunk);
279228
} else {
280-
await recordResult(
281-
sendText(to, chunk, {
282-
verbose: false,
283-
cfg,
284-
accountId: accountId ?? undefined,
285-
}),
286-
);
229+
await recordResult(sendText(to, chunk, sendOptions));
287230
}
288231
}
289232
} else if (shouldSendQuickRepliesInline) {

0 commit comments

Comments
 (0)