A IA da Certu respondendo clientes no WhatsApp, na tela de conversas
A tela de conversas da Certu, numa conta de demonstração.

Chapter 5 of 15

How to make AI answer on WhatsApp, with code

This chapter of the CertuAI manual shows how the AI replies and how much each conversation costs.

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

Manual gratuito da Certu · Updated November 26, 2026· Written by the · ver o manual inteiro

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.

How to make AI answer on WhatsApp, with code: summary in 5 facts

Você manda à IA o texto com as informações do seu negócio e a mensagem do cliente.

  • Você manda à IA o texto com as informações do seu negócio e a mensagem do cliente.
  • Ready-made in parts 4 and 7 of the chapter 11 file.
  • Ask Gemini, with the business text in system_instruction.
  • With Gemini 2.5 Flash-Lite, about US$ 0.00023 per answer.
  • A whole conversation comes to about one cent.

How to make AI answer on WhatsApp, with code, in numbers

With Gemini 2.5 Flash-Lite, about US$ 0.00023 per answer.

How to make AI answer on WhatsApp, with code, in numbers
NumberWhat it means
US$ 0.00023 per answerWith Gemini 2.5 Flash-Lite, about US$ 0.00023 per answer.

About CertuAI: CertuAI is an AI chatbot that answers a small business's WhatsApp 24/7: it replies to customers, books appointments and keeps every conversation in a built-in CRM. It is a Tech Provider on Meta's WhatsApp Business Platform. Paid plans start at US$ 99 a month, with no lock-in. In measurements from 30 Jul to 30 Sep 2026, the AI replied in 11.7 seconds at the median (38,908 replies). Create my agent

What are 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 each AI reply cost with this code?

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 math on cost per conversation. Chapter 5 of CertuAI's free manual.

About CertuAI: CertuAI is an AI chatbot that answers a small business's WhatsApp 24/7: it replies to customers, books appointments and keeps every conversation in a built-in CRM. It is a Tech Provider on Meta's WhatsApp Business Platform. Paid plans start at US$ 99 a month, with no lock-in. In measurements from 30 Jul to 30 Sep 2026, the AI replied in 11.7 seconds at the median (38,908 replies). Create my agent