DEV Community

Cover image for Building Reliable WhatsApp Chatbots: Fallbacks, Handoffs, and State
Neha Panwar
Neha Panwar

Posted on

Building Reliable WhatsApp Chatbots: Fallbacks, Handoffs, and State

A WhatsApp chatbot can answer a simple question in a few lines of code.

Building one that works reliably in production is a different problem.

Real users do not always follow the conversation flow you designed. They change their minds, send incomplete messages, ask unexpected questions, or suddenly need a human agent.

A good chatbot architecture should be designed around these situations from the beginning.

  1. Don't Treat Every Message Independently

A chatbot needs some understanding of conversation state.

For example:

User: I want to buy a laptop
Bot: What is your budget?
User: Under ₹50,000
Bot: Which brand do you prefer?

The second message only makes sense because the system remembers what the bot previously asked.

A simple state object might look like:

{
userId: "12345",
state: "select_brand",
budget: 50000
}

The state can be stored in Redis or a database depending on the application's requirements.

  1. Always Have a Fallback

Users will eventually send something your bot does not understand.

Instead of repeatedly returning:

I didn't understand your message.

provide a useful fallback.

For example:

I'm not sure I understood that.

You can:

  1. Check an order
  2. View products
  3. Talk to support

A fallback should help the user recover instead of forcing them to restart the conversation.

  1. Add Human Handoff

Automation should not become a barrier between customers and support agents.

A user should be able to request a human when necessary.

A typical flow can be:

Customer

Chatbot

Understand Request

Can Bot Handle It?
↙ ↘
Yes No
↓ ↓
Answer Human Agent

The handoff should also include useful context.

Instead of making the agent ask the customer to explain everything again, pass information such as the conversation history, customer ID, order number, and detected issue.

  1. Protect Against Infinite Loops

A poorly designed fallback can create a loop:

User → Unknown
Bot → Fallback
User → Unknown
Bot → Fallback
User → Unknown
Bot → Fallback

Add a fallback counter or confidence threshold.

After a few unsuccessful attempts, move the conversation to a human agent.

This creates a much better experience than repeatedly showing the same response.

  1. Separate Conversation Logic

Avoid putting the entire chatbot inside one large conditional block.

Instead of:

if (...) {
// hundreds of lines
} else if (...) {
// hundreds more
}

separate responsibilities:

Message Handler

Intent Detection

Conversation State

Business Logic

Response Builder

WhatsApp Adapter

This makes individual components easier to test and replace.

  1. Log Conversations Carefully

Logs are essential when debugging chatbot behavior.

Record useful technical information such as:

Message ID
User/session ID
Current state
Detected intent
Processing time
API response
Error information
Handoff events

At the same time, avoid unnecessarily storing sensitive customer information in application logs.

  1. Use Existing Infrastructure When It Makes Sense

Building a complete WhatsApp chatbot platform requires more than writing the conversation logic.

You also need messaging APIs, templates, delivery tracking, authentication, analytics, integrations, and operational tooling.

For teams that don't want to build every messaging component themselves, WatConnect provides WhatsApp automation, AI chatbots, campaigns, notifications, analytics, and integrations that can be used as part of a broader application workflow.

The important part is still your application architecture: keep the chatbot logic modular so it can evolve as requirements change.

Final Thoughts

A production chatbot should not be judged only by how well it handles the happy path.

The more important questions are:

What happens when the bot doesn't understand?
Can the user reach a human?
Is conversation state preserved?
Can the system recover from API failures?
Can developers understand what happened through logs?

Designing these fallback paths early makes WhatsApp chatbots more reliable, maintainable, and useful for real users.

The best chatbot isn't the one that answers everything. It's the one that knows what to do when it can't.

Top comments (0)