From abb4b7c91cead6ea44f4996b0579892c393d13ea Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 13:49:37 +0000 Subject: [PATCH] refactor(line): share messaging client setup --- src/line/send.ts | 112 +++++++++++++++-------------------------------- 1 file changed, 36 insertions(+), 76 deletions(-) diff --git a/src/line/send.ts b/src/line/send.ts index 622cd1335..7f091f6c4 100644 --- a/src/line/send.ts +++ b/src/line/send.ts @@ -52,6 +52,35 @@ function normalizeTarget(to: string): string { return normalized; } +function createLineMessagingClient(opts: { channelAccessToken?: string; accountId?: string }): { + account: ReturnType; + client: messagingApi.MessagingApiClient; +} { + const cfg = loadConfig(); + const account = resolveLineAccount({ + cfg, + accountId: opts.accountId, + }); + const token = resolveLineChannelAccessToken(opts.channelAccessToken, account); + const client = new messagingApi.MessagingApiClient({ + channelAccessToken: token, + }); + return { account, client }; +} + +function createLinePushContext( + to: string, + opts: { channelAccessToken?: string; accountId?: string }, +): { + account: ReturnType; + client: messagingApi.MessagingApiClient; + chatId: string; +} { + const { account, client } = createLineMessagingClient(opts); + const chatId = normalizeTarget(to); + return { account, client, chatId }; +} + function createTextMessage(text: string): TextMessage { return { type: "text", text }; } @@ -189,16 +218,7 @@ export async function replyMessageLine( messages: Message[], opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {}, ): Promise { - const cfg = loadConfig(); - const account = resolveLineAccount({ - cfg, - accountId: opts.accountId, - }); - const token = resolveLineChannelAccessToken(opts.channelAccessToken, account); - - const client = new messagingApi.MessagingApiClient({ - channelAccessToken: token, - }); + const { account, client } = createLineMessagingClient(opts); await client.replyMessage({ replyToken, @@ -225,17 +245,7 @@ export async function pushMessagesLine( throw new Error("Message must be non-empty for LINE sends"); } - const cfg = loadConfig(); - const account = resolveLineAccount({ - cfg, - accountId: opts.accountId, - }); - const token = resolveLineChannelAccessToken(opts.channelAccessToken, account); - const chatId = normalizeTarget(to); - - const client = new messagingApi.MessagingApiClient({ - channelAccessToken: token, - }); + const { account, client, chatId } = createLinePushContext(to, opts); await client .pushMessage({ @@ -283,17 +293,7 @@ export async function pushImageMessage( previewImageUrl?: string, opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {}, ): Promise { - const cfg = loadConfig(); - const account = resolveLineAccount({ - cfg, - accountId: opts.accountId, - }); - const token = resolveLineChannelAccessToken(opts.channelAccessToken, account); - const chatId = normalizeTarget(to); - - const client = new messagingApi.MessagingApiClient({ - channelAccessToken: token, - }); + const { account, client, chatId } = createLinePushContext(to, opts); const imageMessage = createImageMessage(originalContentUrl, previewImageUrl); @@ -331,17 +331,7 @@ export async function pushLocationMessage( }, opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {}, ): Promise { - const cfg = loadConfig(); - const account = resolveLineAccount({ - cfg, - accountId: opts.accountId, - }); - const token = resolveLineChannelAccessToken(opts.channelAccessToken, account); - const chatId = normalizeTarget(to); - - const client = new messagingApi.MessagingApiClient({ - channelAccessToken: token, - }); + const { account, client, chatId } = createLinePushContext(to, opts); const locationMessage = createLocationMessage(location); @@ -375,17 +365,7 @@ export async function pushFlexMessage( contents: FlexContainer, opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {}, ): Promise { - const cfg = loadConfig(); - const account = resolveLineAccount({ - cfg, - accountId: opts.accountId, - }); - const token = resolveLineChannelAccessToken(opts.channelAccessToken, account); - const chatId = normalizeTarget(to); - - const client = new messagingApi.MessagingApiClient({ - channelAccessToken: token, - }); + const { account, client, chatId } = createLinePushContext(to, opts); const flexMessage: FlexMessage = { type: "flex", @@ -427,17 +407,7 @@ export async function pushTemplateMessage( template: TemplateMessage, opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {}, ): Promise { - const cfg = loadConfig(); - const account = resolveLineAccount({ - cfg, - accountId: opts.accountId, - }); - const token = resolveLineChannelAccessToken(opts.channelAccessToken, account); - const chatId = normalizeTarget(to); - - const client = new messagingApi.MessagingApiClient({ - channelAccessToken: token, - }); + const { account, client, chatId } = createLinePushContext(to, opts); await client.pushMessage({ to: chatId, @@ -469,17 +439,7 @@ export async function pushTextMessageWithQuickReplies( quickReplyLabels: string[], opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {}, ): Promise { - const cfg = loadConfig(); - const account = resolveLineAccount({ - cfg, - accountId: opts.accountId, - }); - const token = resolveLineChannelAccessToken(opts.channelAccessToken, account); - const chatId = normalizeTarget(to); - - const client = new messagingApi.MessagingApiClient({ - channelAccessToken: token, - }); + const { account, client, chatId } = createLinePushContext(to, opts); const message = createTextMessageWithQuickReplies(text, quickReplyLabels);