Pular para o conteúdo principal

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

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

Enviar mensagens

ctx.jelou.send(options)

Retorna { messageId: string }.
await ctx.jelou.send({
  type: "text",
  to: "+5511987654321",
  text: "Seu pedido #1234 está a caminho",
});
Outros tipos suportados: video, audio, file, sticker, contacts, quick_reply, cta_url, location, flow.

Templates HSM

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

Tratamento de erros

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

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