refactor: share feishu reaction client setup

This commit is contained in:
Peter Steinberger 2026-03-13 16:49:56 +00:00
parent 49f3fbf726
commit a14a32695d
1 changed files with 20 additions and 27 deletions

View File

@ -9,6 +9,20 @@ export type FeishuReaction = {
operatorId: string; operatorId: string;
}; };
function resolveConfiguredFeishuClient(params: { cfg: ClawdbotConfig; accountId?: string }) {
const account = resolveFeishuAccount(params);
if (!account.configured) {
throw new Error(`Feishu account "${account.accountId}" not configured`);
}
return createFeishuClient(account);
}
function assertFeishuReactionApiSuccess(response: { code?: number; msg?: string }, action: string) {
if (response.code !== 0) {
throw new Error(`Feishu ${action} failed: ${response.msg || `code ${response.code}`}`);
}
}
/** /**
* Add a reaction (emoji) to a message. * Add a reaction (emoji) to a message.
* @param emojiType - Feishu emoji type, e.g., "SMILE", "THUMBSUP", "HEART" * @param emojiType - Feishu emoji type, e.g., "SMILE", "THUMBSUP", "HEART"
@ -21,12 +35,7 @@ export async function addReactionFeishu(params: {
accountId?: string; accountId?: string;
}): Promise<{ reactionId: string }> { }): Promise<{ reactionId: string }> {
const { cfg, messageId, emojiType, accountId } = params; const { cfg, messageId, emojiType, accountId } = params;
const account = resolveFeishuAccount({ cfg, accountId }); const client = resolveConfiguredFeishuClient({ cfg, accountId });
if (!account.configured) {
throw new Error(`Feishu account "${account.accountId}" not configured`);
}
const client = createFeishuClient(account);
const response = (await client.im.messageReaction.create({ const response = (await client.im.messageReaction.create({
path: { message_id: messageId }, path: { message_id: messageId },
@ -41,9 +50,7 @@ export async function addReactionFeishu(params: {
data?: { reaction_id?: string }; data?: { reaction_id?: string };
}; };
if (response.code !== 0) { assertFeishuReactionApiSuccess(response, "add reaction");
throw new Error(`Feishu add reaction failed: ${response.msg || `code ${response.code}`}`);
}
const reactionId = response.data?.reaction_id; const reactionId = response.data?.reaction_id;
if (!reactionId) { if (!reactionId) {
@ -63,12 +70,7 @@ export async function removeReactionFeishu(params: {
accountId?: string; accountId?: string;
}): Promise<void> { }): Promise<void> {
const { cfg, messageId, reactionId, accountId } = params; const { cfg, messageId, reactionId, accountId } = params;
const account = resolveFeishuAccount({ cfg, accountId }); const client = resolveConfiguredFeishuClient({ cfg, accountId });
if (!account.configured) {
throw new Error(`Feishu account "${account.accountId}" not configured`);
}
const client = createFeishuClient(account);
const response = (await client.im.messageReaction.delete({ const response = (await client.im.messageReaction.delete({
path: { path: {
@ -77,9 +79,7 @@ export async function removeReactionFeishu(params: {
}, },
})) as { code?: number; msg?: string }; })) as { code?: number; msg?: string };
if (response.code !== 0) { assertFeishuReactionApiSuccess(response, "remove reaction");
throw new Error(`Feishu remove reaction failed: ${response.msg || `code ${response.code}`}`);
}
} }
/** /**
@ -92,12 +92,7 @@ export async function listReactionsFeishu(params: {
accountId?: string; accountId?: string;
}): Promise<FeishuReaction[]> { }): Promise<FeishuReaction[]> {
const { cfg, messageId, emojiType, accountId } = params; const { cfg, messageId, emojiType, accountId } = params;
const account = resolveFeishuAccount({ cfg, accountId }); const client = resolveConfiguredFeishuClient({ cfg, accountId });
if (!account.configured) {
throw new Error(`Feishu account "${account.accountId}" not configured`);
}
const client = createFeishuClient(account);
const response = (await client.im.messageReaction.list({ const response = (await client.im.messageReaction.list({
path: { message_id: messageId }, path: { message_id: messageId },
@ -115,9 +110,7 @@ export async function listReactionsFeishu(params: {
}; };
}; };
if (response.code !== 0) { assertFeishuReactionApiSuccess(response, "list reactions");
throw new Error(`Feishu list reactions failed: ${response.msg || `code ${response.code}`}`);
}
const items = response.data?.items ?? []; const items = response.data?.items ?? [];
return items.map((item) => ({ return items.map((item) => ({