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

# Mensagens

> Envie mensagens de WhatsApp da sua função com ctx.jelou: texto, imagens, botões, listas, carrosséis, templates HSM e mais.

## O que é `ctx.jelou`?

`ctx.jelou` é o cliente de mensagens integrado. Permite enviar mensagens de WhatsApp diretamente da sua função. Disponível automaticamente quando a empresa tem credenciais da API do Jelou configuradas.

## Verificar disponibilidade

```typescript theme={null}
if (!ctx.jelou.available) {
  return { error: "Mensagens não configuradas para esta empresa" };
}
```

## Enviar mensagens

### `ctx.jelou.send(options)`

Retorna `{ messageId: string }`.

<Tabs>
  <Tab title="Texto">
    ```typescript theme={null}
    await ctx.jelou.send({
      type: "text",
      to: "+5511987654321",
      text: "Seu pedido #1234 está a caminho",
    });
    ```
  </Tab>

  <Tab title="Imagem">
    ```typescript theme={null}
    await ctx.jelou.send({
      type: "image",
      to: "+5511987654321",
      mediaUrl: "https://cdn.example.com/produto.jpg",
      caption: "Produto: Laptop Dell XPS 15",
    });
    ```
  </Tab>

  <Tab title="Botões">
    ```typescript theme={null}
    await ctx.jelou.send({
      type: "buttons",
      to: "+5511987654321",
      text: "Como deseja receber seu pedido?",
      buttons: [
        { id: "delivery", title: "Entrega em casa" },
        { id: "pickup", title: "Retirada na loja" },
        { id: "express", title: "Entrega express" },
      ],
    });
    ```
  </Tab>

  <Tab title="Lista">
    ```typescript theme={null}
    await ctx.jelou.send({
      type: "list",
      to: "+5511987654321",
      text: "Selecione uma categoria:",
      buttonText: "Ver categorias",
      sections: [
        {
          title: "Eletrônicos",
          rows: [
            { id: "laptops", title: "Laptops" },
            { id: "phones", title: "Celulares" },
          ],
        },
      ],
    });
    ```
  </Tab>

  <Tab title="Carrossel">
    ```typescript theme={null}
    await ctx.jelou.send({
      type: "carousel",
      to: "+5511987654321",
      cards: [
        {
          mediaUrl: "https://cdn.example.com/laptop.jpg",
          body: "Dell XPS 15 — R$6.999",
          buttons: [{ id: "buy", title: "Comprar" }],
        },
      ],
    });
    ```
  </Tab>
</Tabs>

Outros tipos suportados: `video`, `audio`, `file`, `sticker`, `contacts`, `quick_reply`, `cta_url`, `location`, `flow`.

## Templates HSM

```typescript theme={null}
await ctx.jelou.sendTemplate({
  template: "confirmacao_pedido",
  to: "+5511987654321",
  language: "pt_BR",
  params: ["Maria", "PED-1234", "R$59,99"],
});
```

## Tratamento de erros

```typescript theme={null}
import { JelouApiError } from "@jelou/functions";

try {
  await ctx.jelou.send({ type: "text", to: "+5511987654321", text: "Olá" });
} catch (err) {
  if (err instanceof JelouApiError) {
    if (err.isRateLimit()) return { error: "rate_limit" };
    if (err.isAuth()) return { error: "auth_error" };
  }
  throw err;
}
```

## Testes

```typescript theme={null}
import { createMockContext, createMockJelouClient } from "@jelou/functions/testing";

const mockJelou = createMockJelouClient();
const ctx = createMockContext({ jelou: mockJelou });

await ctx.jelou.send({ type: "text", to: "+5511987654321", text: "Test" });
mockJelou.calls.length; // 1
```
