Files
conversation-layer/mcp/modules/whatsappIntegration.js
2025-06-07 10:52:21 -06:00

305 lines
8.5 KiB
JavaScript

import { ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { fetchJSON } from "../lib/api.js";
const log = (...args) => console.log("[MCP WhatsAppIntegration]", ...args);
export default function registerWhatsappIntegration(server) {
server.tool(
"whatsapp.send-text",
"Envía un mensaje de texto.",
{
to: z.string(),
content: z.string(),
},
async (params) => {
log("Tool invoked", "whatsapp.send-text", params);
const result = await fetchJSON("/whatsapp/sendText", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.send-image",
"Envía una imagen.",
{
to: z.string(),
file: z.string(),
filename: z.string().optional(),
caption: z.string().optional(),
quotedMsgId: z.string().optional(),
waitForId: z.boolean().optional(),
hideTags: z.boolean().optional(),
},
async (params) => {
log("Tool invoked", "whatsapp.send-image", params);
const result = await fetchJSON("/whatsapp/sendImage", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.send-file",
"Envía un archivo.",
{
to: z.string(),
file: z.string(),
filename: z.string().optional(),
caption: z.string().optional(),
ptt: z.boolean().optional(),
withoutPreview: z.boolean().optional(),
viewOnce: z.boolean().optional(),
hideTags: z.boolean().optional(),
},
async (params) => {
log("Tool invoked", "whatsapp.send-file", params);
const result = await fetchJSON("/whatsapp/sendFile", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.delete-chat",
"Elimina un chat.",
{
chatId: z.string(),
},
async (params) => {
log("Tool invoked", "whatsapp.delete-chat", params);
const result = await fetchJSON("/whatsapp/deleteChat", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.delete-message",
"Elimina un mensaje.",
{
chatId: z.string(),
messageId: z.string(),
onlyLocal: z.boolean().optional(),
},
async (params) => {
log("Tool invoked", "whatsapp.delete-message", params);
const result = await fetchJSON("/whatsapp/deleteMessage", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.edit-message",
"Edita un mensaje.",
{
messageId: z.string(),
text: z.string(),
},
async (params) => {
log("Tool invoked", "whatsapp.edit-message", params);
const result = await fetchJSON("/whatsapp/editMessage", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.forward-messages",
"Reenvía mensajes.",
{
to: z.string(),
messages: z.array(z.string()),
skipMyMessages: z.boolean().optional(),
},
async (params) => {
log("Tool invoked", "whatsapp.forward-messages", params);
const result = await fetchJSON("/whatsapp/forwardMessages", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.get-chat",
"Obtiene info de un chat",
{
contactId: z.string(),
},
async (params) => {
log("Tool invoked", "whatsapp.get-chat", params);
const result = await fetchJSON("/whatsapp/getChat", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.get-all-chats",
"Obtiene todos los chats.",
{
withNewMessageOnly: z.boolean().optional(),
},
async (params) => {
log("Tool invoked", "whatsapp.get-all-chats", params);
const result = await fetchJSON("/whatsapp/getAllChats", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.get-all-chat-ids",
"Obtiene todos los chat IDs.",
{},
async () => {
log("Tool invoked", "whatsapp.get-all-chat-ids");
const result = await fetchJSON("/whatsapp/getAllChatIds", {
method: "POST",
body: JSON.stringify({ args: {} }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.get-all-messages",
"Obtiene mensajes de un chat.",
{
chatId: z.string(),
includeMe: z.boolean().optional(),
includeNotifications: z.boolean().optional(),
},
async (params) => {
log("Tool invoked", "whatsapp.get-all-messages", params);
const result = await fetchJSON("/whatsapp/getAllMessagesInChat", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.get-all-contacts",
"Obtiene todos los contactos.",
{},
async () => {
log("Tool invoked", "whatsapp.get-all-contacts");
const result = await fetchJSON("/whatsapp/getAllContacts", {
method: "POST",
body: JSON.stringify({ args: {} }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.get-contact",
"Obtiene la información de un contacto.",
{
contactId: z.string(),
},
async (params) => {
log("Tool invoked", "whatsapp.get-contact", params);
const result = await fetchJSON("/whatsapp/getContact", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.load-earlier-messages",
"Carga mensajes antiguos hasta una fecha dada.",
{
contactId: z.string(),
timestamp: z.number(),
},
async (params) => {
log("Tool invoked", "whatsapp.load-earlier-messages", params);
const result = await fetchJSON("/whatsapp/loadEarlierMessagesTillDate", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.react",
"Reacciona a un mensaje.",
{
messageId: z.string(),
emoji: z.string(),
},
async (params) => {
log("Tool invoked", "whatsapp.react", params);
const result = await fetchJSON("/whatsapp/react", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.reply",
"Responde a un mensaje.",
{
to: z.string(),
content: z.string(),
quotedMsgId: z.string(),
sendSeen: z.boolean().optional(),
},
async (params) => {
log("Tool invoked", "whatsapp.reply", params);
const result = await fetchJSON("/whatsapp/reply", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
server.tool(
"whatsapp.send-text-with-mentions",
"Envía un texto mencionando contactos.",
{
to: z.string(),
content: z.string(),
hideTags: z.boolean().optional(),
mentions: z.array(z.string()).optional(),
},
async (params) => {
log("Tool invoked", "whatsapp.send-text-with-mentions", params);
const result = await fetchJSON("/whatsapp/sendTextWithMentions", {
method: "POST",
body: JSON.stringify({ args: params }),
});
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
}