Add WhatsApp reactions support
Summary: Test Plan:
This commit is contained in:
committed by
Peter Steinberger
parent
aa87d6cee8
commit
551a8d5683
47
src/agents/tools/whatsapp-actions.ts
Normal file
47
src/agents/tools/whatsapp-actions.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import type { AgentToolResult } from "@mariozechner/pi-agent-core";
|
||||
|
||||
import type {
|
||||
ClawdbotConfig,
|
||||
WhatsAppActionConfig,
|
||||
} from "../../config/config.js";
|
||||
import { isSelfChatMode } from "../../utils.js";
|
||||
import { sendReactionWhatsApp } from "../../web/outbound.js";
|
||||
import { readWebSelfId } from "../../web/session.js";
|
||||
import { jsonResult, readStringParam } from "./common.js";
|
||||
|
||||
type ActionGate = (
|
||||
key: keyof WhatsAppActionConfig,
|
||||
defaultValue?: boolean,
|
||||
) => boolean;
|
||||
|
||||
export async function handleWhatsAppAction(
|
||||
params: Record<string, unknown>,
|
||||
cfg: ClawdbotConfig,
|
||||
): Promise<AgentToolResult<unknown>> {
|
||||
const action = readStringParam(params, "action", { required: true });
|
||||
const isActionEnabled: ActionGate = (key, defaultValue = true) => {
|
||||
const value = cfg.whatsapp?.actions?.[key];
|
||||
if (value === undefined) return defaultValue;
|
||||
return value !== false;
|
||||
};
|
||||
|
||||
if (action === "react") {
|
||||
if (!isActionEnabled("reactions")) {
|
||||
throw new Error("WhatsApp reactions are disabled.");
|
||||
}
|
||||
const chatJid = readStringParam(params, "chatJid", { required: true });
|
||||
const messageId = readStringParam(params, "messageId", { required: true });
|
||||
const emoji = readStringParam(params, "emoji", { required: true });
|
||||
const participant = readStringParam(params, "participant");
|
||||
const selfE164 = readWebSelfId().e164;
|
||||
const fromMe = isSelfChatMode(selfE164, cfg.whatsapp?.allowFrom);
|
||||
await sendReactionWhatsApp(chatJid, messageId, emoji, {
|
||||
verbose: false,
|
||||
fromMe,
|
||||
participant: participant ?? undefined,
|
||||
});
|
||||
return jsonResult({ ok: true });
|
||||
}
|
||||
|
||||
throw new Error(`Unsupported WhatsApp action: ${action}`);
|
||||
}
|
||||
11
src/agents/tools/whatsapp-schema.ts
Normal file
11
src/agents/tools/whatsapp-schema.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
|
||||
export const WhatsAppToolSchema = Type.Union([
|
||||
Type.Object({
|
||||
action: Type.Literal("react"),
|
||||
chatJid: Type.String(),
|
||||
messageId: Type.String(),
|
||||
emoji: Type.String(),
|
||||
participant: Type.Optional(Type.String()),
|
||||
}),
|
||||
]);
|
||||
18
src/agents/tools/whatsapp-tool.ts
Normal file
18
src/agents/tools/whatsapp-tool.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { loadConfig } from "../../config/config.js";
|
||||
import type { AnyAgentTool } from "./common.js";
|
||||
import { handleWhatsAppAction } from "./whatsapp-actions.js";
|
||||
import { WhatsAppToolSchema } from "./whatsapp-schema.js";
|
||||
|
||||
export function createWhatsAppTool(): AnyAgentTool {
|
||||
return {
|
||||
label: "WhatsApp",
|
||||
name: "whatsapp",
|
||||
description: "Manage WhatsApp reactions.",
|
||||
parameters: WhatsAppToolSchema,
|
||||
execute: async (_toolCallId, args) => {
|
||||
const params = args as Record<string, unknown>;
|
||||
const cfg = loadConfig();
|
||||
return await handleWhatsAppAction(params, cfg);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user