mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 16:04:33 +00:00
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;
|
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 {
|
function createTextMessage(text: string): TextMessage {
|
||||||
return { type: "text", text };
|
return { type: "text", text };
|
||||||
}
|
}
|
||||||
@@ -189,16 +218,7 @@ export async function replyMessageLine(
|
|||||||
messages: Message[],
|
messages: Message[],
|
||||||
opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {},
|
opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {},
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const cfg = loadConfig();
|
const { account, client } = createLineMessagingClient(opts);
|
||||||
const account = resolveLineAccount({
|
|
||||||
cfg,
|
|
||||||
accountId: opts.accountId,
|
|
||||||
});
|
|
||||||
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
|
||||||
|
|
||||||
const client = new messagingApi.MessagingApiClient({
|
|
||||||
channelAccessToken: token,
|
|
||||||
});
|
|
||||||
|
|
||||||
await client.replyMessage({
|
await client.replyMessage({
|
||||||
replyToken,
|
replyToken,
|
||||||
@@ -225,17 +245,7 @@ export async function pushMessagesLine(
|
|||||||
throw new Error("Message must be non-empty for LINE sends");
|
throw new Error("Message must be non-empty for LINE sends");
|
||||||
}
|
}
|
||||||
|
|
||||||
const cfg = loadConfig();
|
const { account, client, chatId } = createLinePushContext(to, opts);
|
||||||
const account = resolveLineAccount({
|
|
||||||
cfg,
|
|
||||||
accountId: opts.accountId,
|
|
||||||
});
|
|
||||||
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
|
||||||
const chatId = normalizeTarget(to);
|
|
||||||
|
|
||||||
const client = new messagingApi.MessagingApiClient({
|
|
||||||
channelAccessToken: token,
|
|
||||||
});
|
|
||||||
|
|
||||||
await client
|
await client
|
||||||
.pushMessage({
|
.pushMessage({
|
||||||
@@ -283,17 +293,7 @@ export async function pushImageMessage(
|
|||||||
previewImageUrl?: string,
|
previewImageUrl?: string,
|
||||||
opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {},
|
opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {},
|
||||||
): Promise<LineSendResult> {
|
): Promise<LineSendResult> {
|
||||||
const cfg = loadConfig();
|
const { account, client, chatId } = createLinePushContext(to, opts);
|
||||||
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 imageMessage = createImageMessage(originalContentUrl, previewImageUrl);
|
const imageMessage = createImageMessage(originalContentUrl, previewImageUrl);
|
||||||
|
|
||||||
@@ -331,17 +331,7 @@ export async function pushLocationMessage(
|
|||||||
},
|
},
|
||||||
opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {},
|
opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {},
|
||||||
): Promise<LineSendResult> {
|
): Promise<LineSendResult> {
|
||||||
const cfg = loadConfig();
|
const { account, client, chatId } = createLinePushContext(to, opts);
|
||||||
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 locationMessage = createLocationMessage(location);
|
const locationMessage = createLocationMessage(location);
|
||||||
|
|
||||||
@@ -375,17 +365,7 @@ export async function pushFlexMessage(
|
|||||||
contents: FlexContainer,
|
contents: FlexContainer,
|
||||||
opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {},
|
opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {},
|
||||||
): Promise<LineSendResult> {
|
): Promise<LineSendResult> {
|
||||||
const cfg = loadConfig();
|
const { account, client, chatId } = createLinePushContext(to, opts);
|
||||||
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 flexMessage: FlexMessage = {
|
const flexMessage: FlexMessage = {
|
||||||
type: "flex",
|
type: "flex",
|
||||||
@@ -427,17 +407,7 @@ export async function pushTemplateMessage(
|
|||||||
template: TemplateMessage,
|
template: TemplateMessage,
|
||||||
opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {},
|
opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {},
|
||||||
): Promise<LineSendResult> {
|
): Promise<LineSendResult> {
|
||||||
const cfg = loadConfig();
|
const { account, client, chatId } = createLinePushContext(to, opts);
|
||||||
const account = resolveLineAccount({
|
|
||||||
cfg,
|
|
||||||
accountId: opts.accountId,
|
|
||||||
});
|
|
||||||
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
|
||||||
const chatId = normalizeTarget(to);
|
|
||||||
|
|
||||||
const client = new messagingApi.MessagingApiClient({
|
|
||||||
channelAccessToken: token,
|
|
||||||
});
|
|
||||||
|
|
||||||
await client.pushMessage({
|
await client.pushMessage({
|
||||||
to: chatId,
|
to: chatId,
|
||||||
@@ -469,17 +439,7 @@ export async function pushTextMessageWithQuickReplies(
|
|||||||
quickReplyLabels: string[],
|
quickReplyLabels: string[],
|
||||||
opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {},
|
opts: { channelAccessToken?: string; accountId?: string; verbose?: boolean } = {},
|
||||||
): Promise<LineSendResult> {
|
): Promise<LineSendResult> {
|
||||||
const cfg = loadConfig();
|
const { account, client, chatId } = createLinePushContext(to, opts);
|
||||||
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 message = createTextMessageWithQuickReplies(text, quickReplyLabels);
|
const message = createTextMessageWithQuickReplies(text, quickReplyLabels);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user