
Cómo Enviar Notificaciones de WhatsApp desde Node.js
Envía alertas, recordatorios y actualizaciones a WhatsApp desde cualquier app Node.js. whatsmeow-node se mantiene conectado en segundo plano — solo llamas a sendMessage() cuando necesites notificar a alguien.
Requisitos Previos
- Una sesión vinculada de whatsmeow-node (Cómo Vincular)
- El número de teléfono del destinatario (como JID:
5512345678@s.whatsapp.net)
Paso 1: Configurar una Conexión Persistente
import { createClient } from "@whatsmeow-node/whatsmeow-node";
const client = createClient({ store: "session.db" });
async function start() {
const { jid } = await client.init();
if (!jid) {
console.error("Not paired! See: How to Pair WhatsApp");
process.exit(1);
}
await client.connect();
await client.sendPresence("available");
console.log("Notification service is ready");
}
start().catch(console.error);
El cliente se mantiene conectado y listo para enviar mensajes en cualquier momento.
Paso 2: Enviar una Notificación
async function notify(phone: string, message: string) {
const jid = `${phone}@s.whatsapp.net`;
await client.sendMessage(jid, { conversation: message });
}
// Example: order update
await notify("5512345678", "Your order #1234 has shipped! Track it at https://example.com/track/1234");
Paso 3: Disparar desde un Endpoint HTTP
Envuelve la notificación en una ruta de Express para que otros servicios puedan activarla:
import express from "express";
const app = express();
app.use(express.json());
app.post("/notify", async (req, res) => {
const { phone, message } = req.body;
if (!phone || !message) {
return res.status(400).json({ error: "phone and message are required" });
}
try {
const jid = `${phone}@s.whatsapp.net`;
const resp = await client.sendMessage(jid, { conversation: message });
res.json({ sent: true, id: resp.id });
} catch (err) {
res.status(500).json({ error: "Failed to send" });
}
});
app.listen(3000, () => console.log("Notification API on :3000"));
Ahora cualquier servicio puede enviar una notificación:
curl -X POST http://localhost:3000/notify \
-H "Content-Type: application/json" \
-d '{"phone": "5512345678", "message": "Your appointment is in 1 hour"}'
Paso 4: Enviar a Múltiples Destinatarios
async function broadcast(phones: string[], message: string) {
for (const phone of phones) {
const jid = `${phone}@s.whatsapp.net`;
await client.sendMessage(jid, { conversation: message });
// Wait between sends to avoid rate limiting
await new Promise((r) => setTimeout(r, 2000));
}
}
await broadcast(
["5512345678", "5598765432", "5511223344"],
"Reminder: team meeting at 3 PM today",
);
WhatsApp limita la tasa de envío. Espacia los mensajes de 1 a 3 segundos para envíos masivos. Enviar demasiado rápido puede hacer que tu cuenta sea restringida temporalmente. Consulta Límites de Tasa.
Patrones de Notificación
Recordatorio de cita
await notify(phone, `Reminder: your appointment with Dr. Smith is tomorrow at 10:00 AM.
Reply CONFIRM to confirm or CANCEL to cancel.`);
Actualización de pedido
await notify(phone, `Order #${orderId} update: ${status}
${status === "shipped" ? `Track: ${trackingUrl}` : ""}`);
Alerta del sistema
await notify(adminPhone, `⚠ Server alert: CPU usage at ${cpuPercent}% on ${hostname}`);
Notificación a grupo
// Send to a group instead of an individual
const groupJid = "120363XXXXX@g.us";
await client.sendMessage(groupJid, {
conversation: "Deploy complete: v2.1.0 is live",
});
Ejemplo Completo
Un servicio de notificaciones con API HTTP:
import { createClient } from "@whatsmeow-node/whatsmeow-node";
import express from "express";
const client = createClient({ store: "session.db" });
const app = express();
app.use(express.json());
app.post("/notify", async (req, res) => {
const { phone, message } = req.body;
if (!phone || !message) {
return res.status(400).json({ error: "phone and message are required" });
}
try {
const jid = `${phone}@s.whatsapp.net`;
const resp = await client.sendMessage(jid, { conversation: message });
res.json({ sent: true, id: resp.id });
} catch (err) {
console.error("Send failed:", err);
res.status(500).json({ error: "Failed to send" });
}
});
app.post("/broadcast", async (req, res) => {
const { phones, message } = req.body;
if (!phones?.length || !message) {
return res.status(400).json({ error: "phones[] and message are required" });
}
// Send in background
(async () => {
for (const phone of phones) {
try {
const jid = `${phone}@s.whatsapp.net`;
await client.sendMessage(jid, { conversation: message });
} catch (err) {
console.error(`Failed to send to ${phone}:`, err);
}
await new Promise((r) => setTimeout(r, 2000));
}
console.log(`Broadcast complete: ${phones.length} recipients`);
})();
res.json({ queued: true, count: phones.length });
});
async function main() {
const { jid } = await client.init();
if (!jid) {
console.error("Not paired!");
process.exit(1);
}
await client.connect();
await client.sendPresence("available");
app.listen(3000, () => console.log("Notification API on :3000"));
process.on("SIGINT", async () => {
await client.sendPresence("unavailable");
await client.disconnect();
client.close();
process.exit(0);
});
}
main().catch(console.error);
Errores Comunes
Enviar mensajes masivos no solicitados viola los Términos de Servicio de WhatsApp y hará que tu cuenta sea baneada. Solo envía notificaciones a usuarios que hayan dado su consentimiento.
Espacia los envíos con un delay de 1-3 segundos. Consulta Límites de Tasa para más detalles.
El cliente debe permanecer conectado para enviar mensajes. Si el proceso termina, tendrás que reconectar. Para producción, usa un gestor de procesos como PM2 o ejecútalo como servicio de systemd.





