Skip to content

Resources · Reference architecture

WhatsApp business bot: a reference architecture

The design we start from for an assistant on the WhatsApp Business Platform that answers order and booking questions, sends the updates people opted into, and passes a chat to your team without losing it. It is a reference design, not a client’s system; the rules Meta sets for webhooks and messaging windows are cited on this page with the date we checked them.

Layer: all four, centred on Reach

One WhatsApp conversation, webhook to CRMSample data
  1. TOOLIngressWebhook from Meta: X-Hub-Signature-256 matches the app secret
  2. OKIngressNew message id: event stored, 200 returned before any model call
  3. NOTESend gateCustomer wrote just now: service window open, free-form reply allowed
  4. PLANRouterIntent order status · language Hindi · contact matched by phone number
  5. TOOLLookupRead order 4471 from the order system (read-only scope)
  6. OKAssistantReply in Hindi with the date and a “Talk to a person” button
  7. OKSenderStatus webhooks: delivered, then read
  8. APPROVALSend gateNext day, window closed: update sent only as the approved utility template
  9. DONECRMSummary, consent state and template sent written to the contact

The problem

What this architecture solves

A WhatsApp assistant is a webhook service with rules attached. Meta delivers every message and status update to your endpoint, may deliver the same event more than once, and decides what you may send by how recently the customer wrote to you. Duplicate replies, updates that never send and messages to people who opted out all come from ignoring one of those facts, not from the model.

  • Every inbound event is verified, stored and acknowledged before any slow work starts.
  • Inside the customer service window replies are free-form; later messages go out as approved templates, or not at all.
  • Consent is a record with a history, checked before every message your business starts.

Components

Components and the options for each

  • Webhook ingress

    Checks the X-Hub-Signature-256 header against your app secret, stores the raw event and returns 200 before anything slow runs.

    • Cloudflare Workers
    • Next.js route on Vercel
    • Python service in Docker
  • Event store and dedupe

    Keeps every event by message id, so a redelivery is dropped and any run can be replayed.

    • Cloudflare D1
    • Supabase Postgres
  • Conversation lane

    One ordered lane per customer number: a burst of messages is handled in sequence, never in parallel.

    • Cloudflare Queues
    • Postgres job table
    • n8n in queue mode
  • Router and assistant

    Detects intent and language and drafts a reply from approved content or a lookup.

    • OpenAI
    • Anthropic
    • Google Gemini
  • Read-only lookups

    Order status, booking slots and store hours through your systems’ APIs, with scopes that cannot change data.

    • Your order system via its API
    • Your booking calendar via its API
    • Stripe payment status
  • Send gate

    Checks the service window and consent before every send: inside the window the draft, outside only an approved template.

    • Window state per contact
    • Template registry with approval status
  • Outbound sender

    Paces sends and tracks delivered or failed status for each message.

    • Queue with backoff
    • Status tracking per message
  • Consent store

    Where each opt-in came from, its wording and time, opt-out keywords and the full history.

    • Supabase
    • Fields on your CRM contact
  • Team inbox and CRM

    A teammate takes over in the inbox; the summary, consent state and templates sent are written to the contact.

    • Your CRM via its API
    • Shared team inbox

Data flow

How data moves, step by step

  1. 01Meta posts the message; ingress checks the signature, stores the event and answers 200.
  2. 02The message id is checked against the event store; a redelivery stops here.
  3. 03The event joins the customer’s lane, so replies never cross.
  4. 04The router reads intent and language; a lookup fetches the order if needed.
  5. 05The assistant drafts; the send gate confirms the window is open and the contact has not opted out.
  6. 06The sender posts the reply; a failed send is retried or parked.
  7. 07Later updates outside the window go out only as approved templates the contact opted into.
  8. 08A request for a person moves the chat to the inbox, and the CRM receives the summary.

How it works

Design decisions

  1. Mechanism 01

    Acknowledge first, think later

    Meta retries webhooks it considers failed, and a model call can take seconds. So ingress does only cheap work: signature, store, 200. Everything else runs from a queue, and a retried webhook finds its message id already stored.

    What's the alternative? Answer inside the webhook request

    Simpler code, until a slow reply triggers a retry and the customer receives the same answer twice.

  2. Mechanism 02

    One lane per conversation

    Customers write in bursts: a question, a photo, then the order number. Handling one number’s events in order, with a short pause for the burst to finish, lets the assistant answer the whole turn instead of each fragment.

    What's the alternative? Process every event in parallel

    Faster on paper; in practice replies arrive out of order and contradict each other.

  3. Mechanism 03

    The send gate owns the rules

    Window and consent checks live in one place every send passes through, not in prompts. The model may draft any text; only the gate decides whether it goes out as written, becomes an approved template, or is not sent.

    The customer wrote recently, so the reply goes out as drafted.

    • Free-form text, media or buttons
    • Delivery status tracked
    What's the alternative? Put the rules in the prompt

    Models drift from instructions; a rule about what may legally be sent belongs in code.

Reference diagram

The whole design on one diagram

Drawn to the same legend as our other reference architectures, so you can compare them box for box.
WhatsApp business bot · reference diagramReference design

A WhatsApp message passes webhook ingress and a per-customer lane to the router and model, which use a read-only order lookup. Drafts pass a send gate that reads the consent store, then the sender delivers them; handoffs go to the team inbox, which writes a summary to your CRM.

Diagram with 13 parts: WhatsApp message (source), Opt-in form (source), Webhook ingress (processing), Consent store (system of record), Conversation lane (processing), Your order system (system of record), Router + model (model), Read-only lookup (processing), Send gate (control), Team inbox (human), Outbound sender (processing), Customer (human), Your CRM (system of record).

Connections:

  • WhatsApp message → Webhook ingress (signed webhook)
  • Opt-in form → Consent store (opt-in)
  • Webhook ingress → Conversation lane (new event)
  • Consent store → Send gate (consent state)
  • Conversation lane → Router + model (one turn)
  • Your order system → Read-only lookup (order data)
  • Read-only lookup → Router + model (facts)
  • Router + model → Send gate (draft reply)
  • Router + model → Team inbox (handoff)
  • Send gate → Outbound sender (allowed send)
  • Outbound sender → Customer (message)
  • Team inbox → Your CRM (summary)

How to read these diagrams

Source· in this diagram
Where a request or data enters: a chat widget, a WhatsApp message, a phone call, a webhook, a schedule or an approved document set.
Processing· in this diagram
Ordinary code we write and test: ingress endpoints, routing, retrieval, queues, workers and tool calls. It behaves the same way every time.
Model· in this diagram
A language, speech or embedding model call. Its output is treated as a draft and checked before it can reach a customer or a system.
Control· in this diagram
A gate that can stop, hold or reroute work: input checks, citation checks, send rules, transfer rules and dead letters.
Human· in this diagram
A customer or someone on your team. An arrow into a person is the answer, the escalation or the approval path.
System of record· in this diagram
Where state lives and survives a restart: your CRM, helpdesk or calendar through its API, a search index, a log.
  • Arrows run in the order data moves: left to right, or top to bottom on a phone.
  • A label on an arrow names what travels along it, such as a question, an event id or a ticket.
  • Named products are options from our build stack; “your … via its API” means the system you already run.
  • Each diagram is a reference design, not a drawing of any client’s system. Component choices are made per project.
Read the diagram as text
  1. WhatsApp message → Webhook ingress (signed webhook)
  2. Opt-in form → Consent store (opt-in)
  3. Webhook ingress → Conversation lane (new event)
  4. Consent store → Send gate (consent state)
  5. Conversation lane → Router + model (one turn)
  6. Your order system → Read-only lookup (order data)
  7. Read-only lookup → Router + model (facts)
  8. Router + model → Send gate (draft reply)
  9. Router + model → Team inbox (handoff)
  10. Send gate → Outbound sender (allowed send)
  11. Outbound sender → Customer (message)
  12. Team inbox → Your CRM (summary)

Platform

Platform capabilities this design uses

Build or buy

Build, buy or combine

Plenty of WhatsApp inbox tools ship with a bot builder. The choice is about who holds the rules and the data.
Build, buy or combine: when each option fits and what it costs you
OptionFits whenThe trade-off
An inbox tool with a bot builderConversations are mostly human, menus are simple, and your team already works in that tool.Logic sits in the vendor’s builder, and switching means rebuilding.
Build on this reference designAnswers need your order or booking data, rules differ by market, or WhatsApp is one channel of several.You run a webhook service and a queue, and template approvals become your team’s job.
CombineKeep the inbox tool for agents and run the assistant in front of it, passing chats over through its API.Two systems on one number: the rule for who replies must be explicit, or both will.

Rollout

How we roll it out

Typical sequence for a first build; the weeks are typical ranges, set per project at scoping.
  1. Week 1

    Account, number and templates

    Your Meta Business account and number, opt-in wording, and the first templates submitted.

  2. Weeks 1–3

    Ingress, lanes and send gate

    Signed webhooks, dedupe, ordering and the send gate, tested with redelivered events.

  3. Weeks 3–4

    Assistant and lookups

    Intent routing, read-only order lookups and an eval set built from real chats.

  4. Weeks 4–5

    Inbox, CRM and soft launch

    Takeover by a person and the CRM write-back, then launch on one click-to-chat link.

  5. After launch

    More updates, more markets

    New templates as customers opt in, and per-market rules if you sell in more than one country.

Vendor facts on this page, with the date we checked

  1. Send messages (customer service window, delivery order) (opens in a new tab) · Meta for Developers · accessed
  2. Webhooks: getting started (signatures, retries, deduplication) (opens in a new tab) · Meta for Developers · accessed
  3. Pricing on the WhatsApp Business Platform (opens in a new tab) · Meta for Developers · accessed
  4. Messaging limits (opens in a new tab) · Meta for Developers · accessed

Failure modes

What fails, and how it is handled

The same webhook arrives twice
Its message id is already stored; the copy is acknowledged and dropped.
A reply is drafted after the window closed
The send gate swaps in an approved template or holds the message for a person; free-form text never goes out late.
A template is rejected or paused
The registry tracks approval status; with no approved template the message is parked and the owner alerted, never dropped silently.
Messages are delivered out of order
Where order matters, the sender waits for a delivered status before sending the next message.
Your CRM or order system is down
The assistant says it cannot check right now and offers a person; write-backs wait in an outbox and retry.
A customer opts out mid-conversation
Opt-out keywords update the consent store at once and block every further message your business starts.

Running cost

What drives the running cost

Drivers, not prices: vendor prices change, so we quote them per project with the date we checked.
Template messages
Meta bills per delivered template message, by category and the recipient’s country code; service replies inside an open window are free.
Model tokens
One call per turn plus a summary at handoff; treating a burst as one turn cuts calls in chatty conversations.
Hosting, queue and event store
Small; grows with message volume and with how long raw events are kept.
Teammate time in the inbox
Every handoff is a person’s time.
A provider between you and Meta
A Business Solution Provider adds its own fee.

Defaults

Defaults we ship

  • Signature checked on every webhook; 200 returned before slow work.
  • Dedupe by message id, and one ordered lane per customer number.
  • Every send passes the window and consent checks in code.
  • Read-only lookups by default; any write needs an approval step.
  • Handoff to a person on request, on repeated confusion or on a sensitive topic.
  • Your own Meta Business account and number, so you own the channel.

Limits

Limits and variants

  • Outside the customer service window only approved templates can be sent; a bot cannot open a free-form conversation.
  • Template approval and quality rating are Meta’s decisions, and messaging limits cap how many people you can reach outside a window each day.
  • Voice notes need speech-to-text first; images go to a model that accepts them, or to a person.
  • Variant, web chat plus WhatsApp: one router and one set of content, with a channel adapter each.

Configuration

What the configuration looks like

WhatsApp channel configuration (Sample data)
channel: whatsapp-cloud-api
webhook:
  verify_signature: x-hub-signature-256
  ack_before_processing: true
  dedupe_key: message_id
ordering: one-lane-per-contact
send_gate:
  inside_window: draft-allowed
  outside_window: approved-template-only
  require_opt_in: true
  opt_out_keywords: [STOP, UNSUBSCRIBE]
templates:
  order_dispatched: { category: utility, status: approved }
handoff:
  triggers: [asked-for-person, repeated-confusion, complaint]
  target: team-inbox
crm_writeback: [summary, consent_state, templates_sent]

FAQ

Frequently asked questions

Why does the webhook reply before the assistant has answered?

Meta treats a failed response as a delivery failure and sends the event again. Replying 200 once the event is verified and stored stops those retries; the assistant works from a queue, and a repeat is recognised by its message id.

What happens to a reply the assistant drafts after the window has closed?

The send gate does not send it as written. If the contact opted in and an approved template fits, the template goes out with the details filled in; otherwise the message is held and a person decides.

How does the bot cope when a customer sends five messages in a row?

Each customer number has one ordered lane, and the assistant waits briefly for a burst to finish, then answers the whole turn once instead of each fragment.

Where do WhatsApp conversations live after a person takes over?

In your team inbox while the chat is open, then as a summary on the CRM contact with the consent state and templates sent. Raw events stay in the event store for the retention period you set.

Do we need a Business Solution Provider, or can you connect to Meta directly?

We usually connect to the Cloud API on your own Meta Business account, so you own the number with no middle layer. A provider makes sense if your team already works in its inbox; the design stays the same.

Which parts of this design would we keep if we dropped the AI?

Almost all of it. Ingress, dedupe, ordering, the send gate, consent and the CRM write-back make order updates reliable on their own; the router and model sit in the middle and can be added later.

Next step

Have a workflow in mind? Let’s scope it together.

Book a free 30-minute strategy call. Bring one process you want to automate and we will talk through scope, integrations and a sensible first step.

Free 30-minute call · no commitment

Send an RFP or security questionnaire