Files
openclaw/extensions/whatsapp/src/channel-react-action.ts
2026-04-03 17:33:45 +01:00

70 lines
2.5 KiB
TypeScript

import {
resolveReactionMessageId,
handleWhatsAppAction,
normalizeWhatsAppTarget,
readStringParam,
type OpenClawConfig,
} from "./channel-react-action.runtime.js";
const WHATSAPP_CHANNEL = "whatsapp" as const;
export async function handleWhatsAppReactAction(params: {
action: string;
params: Record<string, unknown>;
cfg: OpenClawConfig;
accountId?: string | null;
toolContext?: {
currentChannelId?: string | null;
currentChannelProvider?: string | null;
currentMessageId?: string | number | null;
};
}) {
if (params.action !== "react") {
throw new Error(`Action ${params.action} is not supported for provider ${WHATSAPP_CHANNEL}.`);
}
const isWhatsAppSource = params.toolContext?.currentChannelProvider === WHATSAPP_CHANNEL;
const explicitTarget =
readStringParam(params.params, "chatJid") ?? readStringParam(params.params, "to");
const normalizedTarget = explicitTarget ? normalizeWhatsAppTarget(explicitTarget) : null;
const normalizedCurrent =
isWhatsAppSource && params.toolContext?.currentChannelId
? normalizeWhatsAppTarget(params.toolContext.currentChannelId)
: null;
const isCrossChat =
normalizedTarget != null &&
(normalizedCurrent == null || normalizedTarget !== normalizedCurrent);
const scopedContext =
!isWhatsAppSource || isCrossChat || !params.toolContext
? undefined
: {
currentChannelId: params.toolContext.currentChannelId ?? undefined,
currentChannelProvider: params.toolContext.currentChannelProvider ?? undefined,
currentMessageId: params.toolContext.currentMessageId ?? undefined,
};
const messageIdRaw = resolveReactionMessageId({
args: params.params,
toolContext: scopedContext,
});
if (messageIdRaw == null) {
readStringParam(params.params, "messageId", { required: true });
}
const messageId = String(messageIdRaw);
const emoji = readStringParam(params.params, "emoji", { allowEmpty: true });
const remove = typeof params.params.remove === "boolean" ? params.params.remove : undefined;
return await handleWhatsAppAction(
{
action: "react",
chatJid:
readStringParam(params.params, "chatJid") ??
readStringParam(params.params, "to", { required: true }),
messageId,
emoji,
remove,
participant: readStringParam(params.params, "participant"),
accountId: params.accountId ?? undefined,
fromMe: typeof params.params.fromMe === "boolean" ? params.params.fromMe : undefined,
},
params.cfg,
);
}