> ## Documentation Index
> Fetch the complete documentation index at: https://docs.plazbot.com/llms.txt
> Use this file to discover all available pages before exploring further.

# WhatsApp

> Envia mensajes, plantillas y gestiona webhooks de WhatsApp

### Introduccion

Para trabajar con **WhatsApp**, es necesario configurar tu numero en Plazbot. La configuracion se realiza directamente con Meta.

<Card title="Configurar WhatsApp" icon="whatsapp" href="https://docs.plazbot.com/guides/primeros-pasos/conectar-whatsapp">
  Guia para conectar tu numero de WhatsApp en Plazbot.
</Card>

### Inicializacion

<CodeGroup>
  ```ts Plazbot (recomendado) theme={null}
  import { Plazbot } from 'plazbot';

  const plazbot = new Plazbot({
    workspaceId: "YOUR_WORKSPACE_ID",
    apiKey: "YOUR_API_KEY",
    zone: "LA"
  });

  // Usar: plazbot.message.onWhatsappMessage(...)
  ```

  ```ts Message individual theme={null}
  import { Message } from 'plazbot';

  const message = new Message({
    workspaceId: "YOUR_WORKSPACE_ID",
    apiKey: "YOUR_API_KEY",
    zone: "LA"
  });
  ```
</CodeGroup>

***

### Enviar Mensaje Simple

Envia un mensaje de texto por WhatsApp. Solo funciona si la conversacion con el cliente esta activa (dentro de la ventana de 24 horas de Meta).

```ts theme={null}
const result = await plazbot.message.onWhatsappMessage({
  message: "Gracias por contactarnos!",
  to: "51912345678"
});
```

<Info>
  Consulta la documentacion de Meta sobre ventanas de conversacion y tarifas: [Meta Pricing](https://developers.facebook.com/docs/whatsapp/pricing)
</Info>

***

### Enviar Plantilla (Template)

Las plantillas permiten iniciar conversaciones en cualquier momento. Deben estar creadas y aprobadas previamente en **Marketing** > **Plantillas** dentro de Plazbot.

**Uso basico:**

```ts theme={null}
await plazbot.message.onConversation({
  to: "51912345678",
  template: "welcome_plazbot"
});
```

**Con variables:**

```ts theme={null}
await plazbot.message.onConversation({
  to: "51912345678",
  template: "order_confirmation",
  variablesBody: [
    { variable: "1", value: "Juan" },
    { variable: "2", value: "ORD-12345" }
  ],
  variablesHeader: [
    { variable: "1", value: "Pedido Confirmado" }
  ]
});
```

**Con archivo adjunto:**

```ts theme={null}
await plazbot.message.onConversation({
  to: "51912345678",
  template: "invoice_template",
  file: {
    fileUrl: "https://tu-servidor.com/facturas/INV-001.pdf",
    fileName: "factura.pdf"
  }
});
```

| Campo             | Tipo     | Requerido | Descripcion                              |
| ----------------- | -------- | --------- | ---------------------------------------- |
| `to`              | `string` | Si        | Numero de telefono con codigo de pais    |
| `template`        | `string` | Si        | Nombre de la plantilla aprobada          |
| `variablesBody`   | `array`  | No        | Variables para el cuerpo del mensaje     |
| `variablesHeader` | `array`  | No        | Variables para el header                 |
| `file`            | `object` | No        | Archivo adjunto (`fileUrl` y `fileName`) |

***

### Historial de Mensajes

Consulta mensajes enviados y recibidos en tu workspace.

```ts theme={null}
// Obtener mensajes recientes
const messages = await plazbot.message.getMessages({ limit: 20 });

// Historial de conversacion con un contacto
const history = await plazbot.message.getConversationHistory({
  contactId: "contact-id"
});

// Buscar mensajes por texto
const results = await plazbot.message.searchMessages({
  query: "precio"
});
```

***

### Webhooks

Registra un webhook para recibir mensajes entrantes de WhatsApp en tu endpoint.

**Registrar webhook:**

```ts theme={null}
await plazbot.message.registerWebhook({
  number: "51912345678",
  webhookUrl: "https://tu-servidor.com/webhooks/whatsapp"
});
```

**Eliminar webhook:**

```ts theme={null}
await plazbot.message.deleteWebhook({
  number: "51912345678"
});
```

<Tip>
  El webhook envia el mensaje y la informacion del contacto cada vez que un cliente escribe al numero configurado. Es ideal para integraciones con Make, Zapier o tu propio servidor.
</Tip>
