|
| 1 | +import type { AgentToolResult } from "@mariozechner/pi-agent-core"; |
| 2 | + |
| 3 | +import type { |
| 4 | + ClawdbotConfig, |
| 5 | + WhatsAppActionConfig, |
| 6 | +} from "../../config/config.js"; |
| 7 | +import { isSelfChatMode } from "../../utils.js"; |
| 8 | +import { sendReactionWhatsApp } from "../../web/outbound.js"; |
| 9 | +import { readWebSelfId } from "../../web/session.js"; |
| 10 | +import { jsonResult, readStringParam } from "./common.js"; |
| 11 | + |
| 12 | +type ActionGate = ( |
| 13 | + key: keyof WhatsAppActionConfig, |
| 14 | + defaultValue?: boolean, |
| 15 | +) => boolean; |
| 16 | + |
| 17 | +export async function handleWhatsAppAction( |
| 18 | + params: Record<string, unknown>, |
| 19 | + cfg: ClawdbotConfig, |
| 20 | +): Promise<AgentToolResult<unknown>> { |
| 21 | + const action = readStringParam(params, "action", { required: true }); |
| 22 | + const isActionEnabled: ActionGate = (key, defaultValue = true) => { |
| 23 | + const value = cfg.whatsapp?.actions?.[key]; |
| 24 | + if (value === undefined) return defaultValue; |
| 25 | + return value !== false; |
| 26 | + }; |
| 27 | + |
| 28 | + if (action === "react") { |
| 29 | + if (!isActionEnabled("reactions")) { |
| 30 | + throw new Error("WhatsApp reactions are disabled."); |
| 31 | + } |
| 32 | + const chatJid = readStringParam(params, "chatJid", { required: true }); |
| 33 | + const messageId = readStringParam(params, "messageId", { required: true }); |
| 34 | + const emoji = readStringParam(params, "emoji", { required: true }); |
| 35 | + const participant = readStringParam(params, "participant"); |
| 36 | + const selfE164 = readWebSelfId().e164; |
| 37 | + const fromMe = isSelfChatMode(selfE164, cfg.whatsapp?.allowFrom); |
| 38 | + await sendReactionWhatsApp(chatJid, messageId, emoji, { |
| 39 | + verbose: false, |
| 40 | + fromMe, |
| 41 | + participant: participant ?? undefined, |
| 42 | + }); |
| 43 | + return jsonResult({ ok: true }); |
| 44 | + } |
| 45 | + |
| 46 | + throw new Error(`Unsupported WhatsApp action: ${action}`); |
| 47 | +} |
0 commit comments