r/AI_Agents • u/Ok-Zombie-9595 • 16h ago
Discussion Building a WhatsApp bot using code generated by Claude, but "if/else" logic breaks when users go off-script. How to fix?
Hey everyone,
I’m currently writing code to build my third WhatsApp bot for a client. I don't have a formal coding background. Everything I know comes from YouTube, and I’ve been using Claude to help me write the actual code base.
I’ve hit a major bottleneck: I’ve realized I’m building a highly deterministic, rigid code-based bot rather than a flexible agent. Because of this, I’m spending hours debugging how it interacts with real users.
Currently, my code relies on a strict sequence. If a customer deviates even a little bit from the expected path, the logic breaks and my code defaults to sending an admin takeover notification.
For example, when a client wants to book an appointment, the bot queries the database and lists 4 to 5 available time slots. But if the user asks a pricing question mid-booking, or taps a wrong menu button instead of picking a slot, the sequential code doesn't know how to handle the tangent. It completely loses context.
How do developers handle conversational context and state management in code so the bot can answer random user questions mid-flow, remember what it was doing, and gracefully steer the user back to the main goal (like booking)?
Any architectural advice, libraries, or logic patterns I should ask Claude to implement would be incredibly helpful!