Skip to main content

What is ctx.jelou?

ctx.jelou is the built-in messaging client. It lets you send WhatsApp messages directly from your function. Available automatically when the company has Jelou API credentials configured.

Check availability

if (!ctx.jelou.available) {
  return { error: "Messaging not configured for this company" };
}

Send messages

ctx.jelou.send(options)

Returns { messageId: string }.
await ctx.jelou.send({
  type: "text",
  to: "+593987654321",
  text: "Your order #1234 is on its way",
});
Other supported types: video, audio, file, sticker, contacts, quick_reply, cta_url, flow.

Send HSM templates

ctx.jelou.sendTemplate(options)

Returns Array<{ id, destination }>.
await ctx.jelou.sendTemplate({
  template: "order_confirmation",
  to: "+593987654321",
  language: "es",
  params: ["Maria", "ORD-1234", "$59.99"],
});

// Multiple recipients
await ctx.jelou.sendTemplate({
  template: "monthly_promo",
  to: ["+593987654321", "+593912345678"],
  params: ["20%", "April 30"],
});

Bot override

All messages default to ctx.bot.id. Override with botId:
await ctx.jelou.send({
  type: "text",
  to: "+593987654321",
  text: "Message from another bot",
  botId: "bot-notifications-456",
});

Error handling

import { JelouApiError } from "@jelou/functions";

try {
  await ctx.jelou.send({ type: "text", to: "+593987654321", text: "Hello" });
} catch (err) {
  if (err instanceof JelouApiError) {
    if (err.isRateLimit()) return { error: "rate_limit" };
    if (err.isAuth()) return { error: "auth_error" };
    if (err.isValidation()) return { error: "validation_error" };
  }
  throw err;
}

Testing

import { createMockContext, createMockJelouClient } from "@jelou/functions/testing";

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

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