WhatsApp Chatbots & Automation

How to Build a WhatsApp Chatbot

Build a custom WhatsApp chatbot using webhooks, conversation state, backend logic, automation, and business workflows.

Practical Guide7 min read

Step 1

How a WhatsApp Chatbot Works

A custom WhatsApp chatbot is an event driven backend. A customer message arrives through a webhook, your application identifies the contact and conversation state, decides the next action, and sends a reply through the Cloud API. The state and business data normally live in a database or CRM rather than in the webhook request itself.

Keep transport, conversation logic, and business actions separate. The webhook layer receives events, the conversation layer decides what should happen, and service modules perform actions such as sending messages, creating leads, checking orders, or handing a conversation to a human.

Implementation Checklist

  • Receive customer messages through a verified webhook.
  • Load the customer and current conversation state before choosing a reply.
  • Run deterministic business rules before external side effects.
  • Send the reply through one server side WhatsApp messaging service and store the message ID.

Step 2

Set Up Cloud API

This workflow assumes the official WhatsApp Cloud API is already working. At minimum, you need the Meta app, WhatsApp Business Account, a test or registered business phone number, the Phone Number ID, a server side access token, and a webhook endpoint before the rest of the automation can be reliable.

Implementation Checklist

  • Prove one outbound message with the correct Phone Number ID and token.
  • Verify that your webhook can receive a known event.
  • Keep credentials and IDs in protected server side configuration.

This guide only covers the part needed for the current workflow. For the complete setup, examples, and troubleshooting, continue with How to Set Up WhatsApp Cloud API.

Step 3

Receive Incoming Messages

Incoming customer messages arrive inside webhook payloads. Parse the message object, identify the sender, message type, message ID, and relevant content, then store or queue the event before running chatbot logic. This gives you a recoverable record if downstream processing fails.

Do not assume every webhook contains a text message. Customers can send media, interactive replies, reactions, or other supported message types, and webhook payloads can also contain status events. Route each event by type before it reaches the conversation engine.

Implementation Checklist

  • Identify the sender and WhatsApp message ID.
  • Route text, interactive, media, and status events separately.
  • Deduplicate repeated message events before generating a second reply.
  • Acknowledge the webhook quickly and move slow work to background processing when needed.

Step 4

Design Conversation State

Structured conversation flows work best when each step is an explicit state with known valid inputs and next routes. Store the state outside the webhook process so the flow survives restarts, retries, and messages that arrive later.

Implementation Checklist

  • Define explicit states and transitions.
  • Validate input before advancing the state.
  • Include restart, cancel, fallback, and help paths.

This guide only covers the part needed for the current workflow. For the complete setup, examples, and troubleshooting, continue with How to Build WhatsApp Conversation Flows.

Step 5

Create Routing Logic

Structured conversation flows work best when each step is an explicit state with known valid inputs and next routes. Store the state outside the webhook process so the flow survives restarts, retries, and messages that arrive later.

Implementation Checklist

  • Define explicit states and transitions.
  • Validate input before advancing the state.
  • Include restart, cancel, fallback, and help paths.

This guide only covers the part needed for the current workflow. For the complete setup, examples, and troubleshooting, continue with How to Build WhatsApp Conversation Flows.

Step 6

Send Automated Replies

Automated replies should come from the conversation decision you already made, not from raw keyword matching scattered across the webhook handler. Build a reply object, send it through the WhatsApp messaging service, and store both the outbound message ID and the conversation state transition.

Before sending, check whether a human currently owns the conversation and whether the reply type is valid for the current messaging context. This prevents the bot from replying over an agent or using the wrong outbound message format.

Implementation Checklist

  • Generate the reply from the current conversation state and validated input.
  • Check handoff status before the bot sends anything.
  • Use one messaging service for API calls and error handling.
  • Store the outbound message ID and next conversation state together.

Step 7

Store Customer Context

Context is the information your automation needs beyond the current message. It can include conversation state, previous answers, customer identity, CRM stage, recent orders, language preference, or whether a human agent currently owns the conversation.

Store only the context you actually need and give each field a clear source of truth. Temporary conversational values can live with the conversation, while durable customer data belongs in the CRM or customer database. This prevents old chatbot state from overwriting newer business information.

Implementation Checklist

  • Separate temporary conversation state from durable customer records.
  • Record when context was updated and which workflow produced it.
  • Avoid placing large raw webhook payloads into every customer record.
  • Clear or archive stale conversation state when a flow ends or restarts.

Step 8

Add Business Actions

A workflow should translate one verified event into a small number of predictable actions. Define the trigger, required data, side effects, and completion state before connecting multiple systems together.

Keep each side effect independently observable. If a CRM update succeeds but a notification fails, your application should know which part needs retrying instead of running the entire workflow again and creating duplicate records.

Implementation Checklist

  • Name the exact event that starts the workflow.
  • Validate required business data before side effects begin.
  • Store the workflow run and result of each important action.
  • Make retries idempotent so partial failures do not duplicate successful work.

Step 9

Add Human Handoff

Human handoff needs a real ownership state. When escalation starts, pause normal bot replies, preserve customer context, assign the conversation to a monitored queue or agent, and define how automation resumes after the agent finishes.

Implementation Checklist

  • Store handoff status and assignment.
  • Pause the bot while a human owns the conversation.
  • Resume automation only through a defined close or resume action.

This guide only covers the part needed for the current workflow. For the complete setup, examples, and troubleshooting, continue with How to Add Human Handoff to a WhatsApp Chatbot.

Step 10

Add AI Carefully

AI should handle flexible language and knowledge tasks while your application keeps control of sensitive business actions. Give the model bounded context and narrow tools, validate tool calls server side, and fall back to deterministic logic or a human when the request is uncertain.

Implementation Checklist

  • Keep business rules outside the model.
  • Expose only narrow, authorized tools.
  • Add fallback and human handoff for uncertainty.

This guide only covers the part needed for the current workflow. For the complete setup, examples, and troubleshooting, continue with How to Add AI to a WhatsApp Chatbot.

Step 11

Test Conversation Paths

Debug this layer independently before changing several parts of the integration at once. Reproduce one known request or event, capture the input and output, and compare the result with what the application expected.

Use structured logs with timestamps, message IDs, event IDs, phone number IDs, and internal workflow IDs. Redact tokens and sensitive customer content. Good logs let you follow one message from API request to webhook event to CRM update without guessing which layer failed.

Implementation Checklist

  • Create one repeatable test case with known input.
  • Log the boundary between this layer and the next system.
  • Change one variable at a time while diagnosing the failure.
  • Keep a successful baseline request so future changes can be compared quickly.

Step 12

Production Checklist

Debug this layer independently before changing several parts of the integration at once. Reproduce one known request or event, capture the input and output, and compare the result with what the application expected.

Use structured logs with timestamps, message IDs, event IDs, phone number IDs, and internal workflow IDs. Redact tokens and sensitive customer content. Good logs let you follow one message from API request to webhook event to CRM update without guessing which layer failed.

Implementation Checklist

  • Create one repeatable test case with known input.
  • Log the boundary between this layer and the next system.
  • Change one variable at a time while diagnosing the failure.
  • Keep a successful baseline request so future changes can be compared quickly.

Need Implementation Help?

Need Help With Your WhatsApp or Automation Project?

If you need help building, integrating, troubleshooting, or improving a production system, you can discuss the project with me directly.