mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-25 03:04:29 -04:00
refactor(line): share channel access token resolver
This commit is contained in:
14
src/line/channel-access-token.ts
Normal file
14
src/line/channel-access-token.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
export function resolveLineChannelAccessToken(
|
||||||
|
explicit: string | undefined,
|
||||||
|
params: { accountId: string; channelAccessToken: string },
|
||||||
|
): string {
|
||||||
|
if (explicit?.trim()) {
|
||||||
|
return explicit.trim();
|
||||||
|
}
|
||||||
|
if (!params.channelAccessToken) {
|
||||||
|
throw new Error(
|
||||||
|
`LINE channel access token missing for account "${params.accountId}" (set channels.line.channelAccessToken or LINE_CHANNEL_ACCESS_TOKEN).`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return params.channelAccessToken.trim();
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import { loadConfig } from "../config/config.js";
|
|||||||
import { logVerbose } from "../globals.js";
|
import { logVerbose } from "../globals.js";
|
||||||
import { resolveLineAccount } from "./accounts.js";
|
import { resolveLineAccount } from "./accounts.js";
|
||||||
import { datetimePickerAction, messageAction, postbackAction, uriAction } from "./actions.js";
|
import { datetimePickerAction, messageAction, postbackAction, uriAction } from "./actions.js";
|
||||||
|
import { resolveLineChannelAccessToken } from "./channel-access-token.js";
|
||||||
|
|
||||||
type RichMenuRequest = messagingApi.RichMenuRequest;
|
type RichMenuRequest = messagingApi.RichMenuRequest;
|
||||||
type RichMenuResponse = messagingApi.RichMenuResponse;
|
type RichMenuResponse = messagingApi.RichMenuResponse;
|
||||||
@@ -39,28 +40,13 @@ interface RichMenuOpts {
|
|||||||
verbose?: boolean;
|
verbose?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveToken(
|
|
||||||
explicit: string | undefined,
|
|
||||||
params: { accountId: string; channelAccessToken: string },
|
|
||||||
): string {
|
|
||||||
if (explicit?.trim()) {
|
|
||||||
return explicit.trim();
|
|
||||||
}
|
|
||||||
if (!params.channelAccessToken) {
|
|
||||||
throw new Error(
|
|
||||||
`LINE channel access token missing for account "${params.accountId}" (set channels.line.channelAccessToken or LINE_CHANNEL_ACCESS_TOKEN).`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return params.channelAccessToken.trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
function getClient(opts: RichMenuOpts = {}): messagingApi.MessagingApiClient {
|
function getClient(opts: RichMenuOpts = {}): messagingApi.MessagingApiClient {
|
||||||
const cfg = loadConfig();
|
const cfg = loadConfig();
|
||||||
const account = resolveLineAccount({
|
const account = resolveLineAccount({
|
||||||
cfg,
|
cfg,
|
||||||
accountId: opts.accountId,
|
accountId: opts.accountId,
|
||||||
});
|
});
|
||||||
const token = resolveToken(opts.channelAccessToken, account);
|
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
||||||
|
|
||||||
return new messagingApi.MessagingApiClient({
|
return new messagingApi.MessagingApiClient({
|
||||||
channelAccessToken: token,
|
channelAccessToken: token,
|
||||||
@@ -73,7 +59,7 @@ function getBlobClient(opts: RichMenuOpts = {}): messagingApi.MessagingApiBlobCl
|
|||||||
cfg,
|
cfg,
|
||||||
accountId: opts.accountId,
|
accountId: opts.accountId,
|
||||||
});
|
});
|
||||||
const token = resolveToken(opts.channelAccessToken, account);
|
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
||||||
|
|
||||||
return new messagingApi.MessagingApiBlobClient({
|
return new messagingApi.MessagingApiBlobClient({
|
||||||
channelAccessToken: token,
|
channelAccessToken: token,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { loadConfig } from "../config/config.js";
|
|||||||
import { logVerbose } from "../globals.js";
|
import { logVerbose } from "../globals.js";
|
||||||
import { recordChannelActivity } from "../infra/channel-activity.js";
|
import { recordChannelActivity } from "../infra/channel-activity.js";
|
||||||
import { resolveLineAccount } from "./accounts.js";
|
import { resolveLineAccount } from "./accounts.js";
|
||||||
|
import { resolveLineChannelAccessToken } from "./channel-access-token.js";
|
||||||
|
|
||||||
// Use the messaging API types directly
|
// Use the messaging API types directly
|
||||||
type Message = messagingApi.Message;
|
type Message = messagingApi.Message;
|
||||||
@@ -31,21 +32,6 @@ interface LineSendOpts {
|
|||||||
replyToken?: string;
|
replyToken?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveToken(
|
|
||||||
explicit: string | undefined,
|
|
||||||
params: { accountId: string; channelAccessToken: string },
|
|
||||||
): string {
|
|
||||||
if (explicit?.trim()) {
|
|
||||||
return explicit.trim();
|
|
||||||
}
|
|
||||||
if (!params.channelAccessToken) {
|
|
||||||
throw new Error(
|
|
||||||
`LINE channel access token missing for account "${params.accountId}" (set channels.line.channelAccessToken or LINE_CHANNEL_ACCESS_TOKEN).`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return params.channelAccessToken.trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeTarget(to: string): string {
|
function normalizeTarget(to: string): string {
|
||||||
const trimmed = to.trim();
|
const trimmed = to.trim();
|
||||||
if (!trimmed) {
|
if (!trimmed) {
|
||||||
@@ -121,7 +107,7 @@ export async function sendMessageLine(
|
|||||||
cfg,
|
cfg,
|
||||||
accountId: opts.accountId,
|
accountId: opts.accountId,
|
||||||
});
|
});
|
||||||
const token = resolveToken(opts.channelAccessToken, account);
|
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
||||||
const chatId = normalizeTarget(to);
|
const chatId = normalizeTarget(to);
|
||||||
|
|
||||||
const client = new messagingApi.MessagingApiClient({
|
const client = new messagingApi.MessagingApiClient({
|
||||||
@@ -208,7 +194,7 @@ export async function replyMessageLine(
|
|||||||
cfg,
|
cfg,
|
||||||
accountId: opts.accountId,
|
accountId: opts.accountId,
|
||||||
});
|
});
|
||||||
const token = resolveToken(opts.channelAccessToken, account);
|
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
||||||
|
|
||||||
const client = new messagingApi.MessagingApiClient({
|
const client = new messagingApi.MessagingApiClient({
|
||||||
channelAccessToken: token,
|
channelAccessToken: token,
|
||||||
@@ -244,7 +230,7 @@ export async function pushMessagesLine(
|
|||||||
cfg,
|
cfg,
|
||||||
accountId: opts.accountId,
|
accountId: opts.accountId,
|
||||||
});
|
});
|
||||||
const token = resolveToken(opts.channelAccessToken, account);
|
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
||||||
const chatId = normalizeTarget(to);
|
const chatId = normalizeTarget(to);
|
||||||
|
|
||||||
const client = new messagingApi.MessagingApiClient({
|
const client = new messagingApi.MessagingApiClient({
|
||||||
@@ -302,7 +288,7 @@ export async function pushImageMessage(
|
|||||||
cfg,
|
cfg,
|
||||||
accountId: opts.accountId,
|
accountId: opts.accountId,
|
||||||
});
|
});
|
||||||
const token = resolveToken(opts.channelAccessToken, account);
|
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
||||||
const chatId = normalizeTarget(to);
|
const chatId = normalizeTarget(to);
|
||||||
|
|
||||||
const client = new messagingApi.MessagingApiClient({
|
const client = new messagingApi.MessagingApiClient({
|
||||||
@@ -350,7 +336,7 @@ export async function pushLocationMessage(
|
|||||||
cfg,
|
cfg,
|
||||||
accountId: opts.accountId,
|
accountId: opts.accountId,
|
||||||
});
|
});
|
||||||
const token = resolveToken(opts.channelAccessToken, account);
|
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
||||||
const chatId = normalizeTarget(to);
|
const chatId = normalizeTarget(to);
|
||||||
|
|
||||||
const client = new messagingApi.MessagingApiClient({
|
const client = new messagingApi.MessagingApiClient({
|
||||||
@@ -394,7 +380,7 @@ export async function pushFlexMessage(
|
|||||||
cfg,
|
cfg,
|
||||||
accountId: opts.accountId,
|
accountId: opts.accountId,
|
||||||
});
|
});
|
||||||
const token = resolveToken(opts.channelAccessToken, account);
|
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
||||||
const chatId = normalizeTarget(to);
|
const chatId = normalizeTarget(to);
|
||||||
|
|
||||||
const client = new messagingApi.MessagingApiClient({
|
const client = new messagingApi.MessagingApiClient({
|
||||||
@@ -446,7 +432,7 @@ export async function pushTemplateMessage(
|
|||||||
cfg,
|
cfg,
|
||||||
accountId: opts.accountId,
|
accountId: opts.accountId,
|
||||||
});
|
});
|
||||||
const token = resolveToken(opts.channelAccessToken, account);
|
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
||||||
const chatId = normalizeTarget(to);
|
const chatId = normalizeTarget(to);
|
||||||
|
|
||||||
const client = new messagingApi.MessagingApiClient({
|
const client = new messagingApi.MessagingApiClient({
|
||||||
@@ -488,7 +474,7 @@ export async function pushTextMessageWithQuickReplies(
|
|||||||
cfg,
|
cfg,
|
||||||
accountId: opts.accountId,
|
accountId: opts.accountId,
|
||||||
});
|
});
|
||||||
const token = resolveToken(opts.channelAccessToken, account);
|
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
||||||
const chatId = normalizeTarget(to);
|
const chatId = normalizeTarget(to);
|
||||||
|
|
||||||
const client = new messagingApi.MessagingApiClient({
|
const client = new messagingApi.MessagingApiClient({
|
||||||
@@ -559,7 +545,7 @@ export async function showLoadingAnimation(
|
|||||||
cfg,
|
cfg,
|
||||||
accountId: opts.accountId,
|
accountId: opts.accountId,
|
||||||
});
|
});
|
||||||
const token = resolveToken(opts.channelAccessToken, account);
|
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
||||||
|
|
||||||
const client = new messagingApi.MessagingApiClient({
|
const client = new messagingApi.MessagingApiClient({
|
||||||
channelAccessToken: token,
|
channelAccessToken: token,
|
||||||
@@ -599,7 +585,7 @@ export async function getUserProfile(
|
|||||||
cfg,
|
cfg,
|
||||||
accountId: opts.accountId,
|
accountId: opts.accountId,
|
||||||
});
|
});
|
||||||
const token = resolveToken(opts.channelAccessToken, account);
|
const token = resolveLineChannelAccessToken(opts.channelAccessToken, account);
|
||||||
|
|
||||||
const client = new messagingApi.MessagingApiClient({
|
const client = new messagingApi.MessagingApiClient({
|
||||||
channelAccessToken: token,
|
channelAccessToken: token,
|
||||||
|
|||||||
Reference in New Issue
Block a user