refactor(line): share messaging client setup
This commit is contained in:
112
src/line/send.ts
112
src/line/send.ts
@@ -52,6 +52,35 @@ function normalizeTarget(to: string): string {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function createLineMessagingClient(opts: { channelAccessToken?: string; accountId?: string }): {
|
||||
account: ReturnType<typeof resolveLineAccount>;
|
||||
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<typeof resolveLineAccount>;
|
||||
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<void> {
|
||||
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<LineSendResult> {
|
||||
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<LineSendResult> {
|
||||
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<LineSendResult> {
|
||||
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<LineSendResult> {
|
||||
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<LineSendResult> {
|
||||
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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user