Skip to content

Commit b021ed5

Browse files
author
linyuan.yang
committed
支持 lark
1 parent 20d25d0 commit b021ed5

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

packages/channel.lark/src/LarkService.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@ export interface LarkHistoryMessage {
9696
content: MessageContent;
9797
}
9898

99+
/** 部署域:feishu = 国内飞书 (open.feishu.cn),lark = 海外 Lark (open.larksuite.com) */
100+
export type LarkDomain = 'feishu' | 'lark';
101+
99102
export interface LarkServiceOptions {
100103
appId: string;
101104
appSecret: string;
105+
/** 默认 'feishu' */
106+
domain?: LarkDomain;
102107
userIdType: LarkUserIdType;
103108
logger?: ILogger;
104109
filterEvent: (eventId: string) => Promise<boolean>;
@@ -112,6 +117,7 @@ export class LarkService implements IChannelService {
112117
private lastCallTime = 0;
113118
private readonly callInterval = 200; // ms
114119
private larkConfig: { appId: string, appSecret: string};
120+
private domain: LarkDomain;
115121
private larkClient: Lark.Client;
116122
private larkWsClient: Lark.WSClient;
117123
private logger?: ILogger;
@@ -132,6 +138,8 @@ export class LarkService implements IChannelService {
132138
this.userIdType = options.userIdType;
133139
this.logger = options.logger;
134140
this.larkConfig = { appId: options.appId, appSecret: options.appSecret };
141+
this.domain = options.domain ?? 'feishu';
142+
const sdkDomain = this.domain === 'lark' ? Lark.Domain.Lark : Lark.Domain.Feishu;
135143
this.loggerLevel = Lark.LoggerLevel.warn;
136144
this.larkLogger = options.logger ? {
137145
trace: (msg: string, ...args: any[]) => options.logger?.debug(msg, ...args),
@@ -140,8 +148,8 @@ export class LarkService implements IChannelService {
140148
warn: (msg: string, ...args: any[]) => options.logger?.warn(msg, ...args),
141149
error: (msg: string, ...args: any[]) => options.logger?.error(msg, ...args),
142150
} : undefined;
143-
this.larkClient = new Lark.Client({ ...this.larkConfig, logger: this.larkLogger, loggerLevel: this.loggerLevel });
144-
this.larkWsClient = new Lark.WSClient({ ...this.larkConfig, logger: this.larkLogger, loggerLevel: this.loggerLevel });
151+
this.larkClient = new Lark.Client({ ...this.larkConfig, domain: sdkDomain, logger: this.larkLogger, loggerLevel: this.loggerLevel });
152+
this.larkWsClient = new Lark.WSClient({ ...this.larkConfig, domain: sdkDomain, logger: this.larkLogger, loggerLevel: this.loggerLevel });
145153
}
146154

147155
createSessionHandler(session: SessionService): ChannelSessionHandler {
@@ -462,10 +470,10 @@ export class LarkService implements IChannelService {
462470
if (this.botOpenId) return this.botOpenId;
463471
try {
464472
const token = await this.getTenantAccessToken();
465-
const response = await fetch('https://open.feishu.cn/open-apis/bot/v3/info/', {
466-
headers: { Authorization: `Bearer ${token}` },
467-
});
468-
const data = await response.json() as any;
473+
const data = await this.larkClient.request({
474+
method: 'GET',
475+
url: '/open-apis/bot/v3/info',
476+
}, Lark.withTenantToken(token)) as any;
469477
if (data.code === 0 && data.bot?.open_id) {
470478
this.botOpenId = data.bot.open_id;
471479
this.logger?.debug(`Bot open_id: ${this.botOpenId}`);

packages/channel.lark/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { LarkService, LarkServiceOptions, LarkReceiveIdType, LarkUserIdType, type LarkUserInfo, type LarkChatInfo } from './LarkService';
1+
export { LarkService, LarkServiceOptions, LarkReceiveIdType, LarkUserIdType, type LarkUserInfo, type LarkChatInfo, type LarkDomain } from './LarkService';
22
export { LarkChatProvider } from './LarkChatProvider';
33
export { LarkSessionHandler, LarkMessageArgs, LarkActionArgs } from './LarkSessionHandler';
44
export { ToolCallStatus } from 'channel.base';

packages/channel.lark/src/plugin.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
ChannelPlugin, ChannelPluginContext, IChannelService, ConfigFieldType,
33
type MessageContent,
44
} from "channel.base";
5-
import { LarkService, LarkReceiveIdType, LarkUserIdType, type LarkUserInfo, type LarkChatInfo } from "./LarkService";
5+
import { LarkService, LarkReceiveIdType, LarkUserIdType, type LarkUserInfo, type LarkChatInfo, type LarkDomain } from "./LarkService";
66
import { LarkMessageArgs, LarkActionArgs } from "./LarkSessionHandler";
77

88
function buildLarkExtraInfo(userInfo: LarkUserInfo | undefined, chatId?: string, messageId?: string): string {
@@ -17,9 +17,15 @@ function buildLarkExtraInfo(userInfo: LarkUserInfo | undefined, chatId?: string,
1717
}
1818
export const larkPlugin: ChannelPlugin = {
1919
type: "lark",
20-
label: "飞书",
20+
label: "飞书 / Lark",
2121

2222
configSchema: {
23+
domain: { label: '部署域', type: ConfigFieldType.Select, required: true, default: 'feishu',
24+
description: '飞书(国内 open.feishu.cn)或 Lark(海外 open.larksuite.com)',
25+
options: [
26+
{ label: '飞书(国内)', value: 'feishu' },
27+
{ label: 'Lark(海外)', value: 'lark' },
28+
] },
2329
appId: { label: 'App ID', type: ConfigFieldType.String, required: true, description: 'Lark app ID' },
2430
appSecret: { label: 'App Secret', type: ConfigFieldType.Password, required: true, description: 'Lark app secret' },
2531
},
@@ -37,6 +43,7 @@ export const larkPlugin: ChannelPlugin = {
3743
const service = new LarkService({
3844
appId: config.appId,
3945
appSecret: config.appSecret,
46+
domain: (config.domain as LarkDomain) ?? 'feishu',
4047
logger,
4148
userIdType: LarkUserIdType.UnionId,
4249
filterEvent,

0 commit comments

Comments
 (0)