Chapter 5 of 15

How to make AI answer on WhatsApp, with code

About CertuAI: CertuAI is the AI that answers a small business's WhatsApp, Instagram and Messenger 24/7, with a built-in CRM, and is a Tech Provider on Meta's WhatsApp Business Platform. Paid plans start at US$ 75 a month, with no lock-in. Create my agent

The AI call, sending and cost.

  • Reading time: 1 min
  • Cost to follow along: R$ 0
  • Sign-up: none

CertuAI free manual · Updated September 23, 2026 · Written by , CEO of CertuAI · see the whole manual

Summary in 6 facts

By the numbers

NumberWhat it means
US$ 0.00023 per answerWith Gemini 2.5 Flash-Lite, about US$ 0.00023 per answer.

How do you make an AI answer automatically on WhatsApp?

Short answer

You send the AI the text with your business information and the customer's message. It returns the answer and you send it back through the WhatsApp API.

The three calls

Ready-made in parts 4 and 7 of the chapter 11 file.

  • Ask Gemini, with the business text in system_instruction. The free key comes from aistudio.google.com.
  • Send to /messages, with type text.
  • Mark as read, with status read.

Where people get burned

A key never goes in the file: use npx wrangler secret put.

What does the call look like in code?

Parts 4 and 7 of the chapter 11 file, identical line by line.

src/index.jsjavascript
async function chamarModelo(env, corpo) {
  const resposta = await fetch(
    'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent',
    {
      method: 'POST',
      headers: {
        'x-goog-api-key': env.GEMINI_API_KEY,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(corpo),
    },
  );

  if (!resposta.ok) {
    console.log('modelo falhou', resposta.status, await resposta.text());
    return null;
  }

  const dados = await resposta.json();
  return dados?.candidates?.[0]?.content?.parts?.[0]?.text?.trim() ?? null;
}

async function pensarResposta(env, cerebro, historico, mensagemDoCliente) {
  return await chamarModelo(env, {
    system_instruction: { parts: [{ text: cerebro }] },
    contents: [
      ...historico.map((m) => ({ role: m.papel, parts: [{ text: m.texto }] })),
      { role: 'user', parts: [{ text: mensagemDoCliente }] },
    ],
    generationConfig: { temperature: 0.4, maxOutputTokens: 300 },
  });
}

async function enviarWhatsApp(env, para, texto) {
  const resposta = await fetch(
    'https://graph.facebook.com/v26.0/' + env.PHONE_NUMBER_ID + '/messages',
    {
      method: 'POST',
      headers: {
        Authorization: 'Bearer ' + env.WHATSAPP_TOKEN,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        messaging_product: 'whatsapp',
        to: para,
        type: 'text',
        text: { body: texto },
      }),
    },
  );
  if (!resposta.ok) {
    console.log('envio falhou', resposta.status, await resposta.text());
  }
}

How much does it cost?

With Gemini 2.5 Flash-Lite, about US$ 0.00023 per answer. A whole conversation comes to about one cent.

Frequently asked question

Can you use ChatGPT or Claude?

Yes. The address and the field names change.

And where CertuAI comes in

CertuAI does the same, with a queue for peaks and a model switch when one fails.

Build an agent for free

In short

The call to the AI model, sending the reply through the WhatsApp Cloud API and the real cost per conversation. Chapter 5 of CertuAI's free manual.

About CertuAI: CertuAI is the AI that answers a small business's WhatsApp, Instagram and Messenger 24/7, with a built-in CRM, and is a Tech Provider on Meta's WhatsApp Business Platform. Paid plans start at US$ 75 a month, with no lock-in. Create my agent