r/AI_Agents • u/Ok-Zombie-9595 • 24d 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!
1
u/Extra-Ad358 24d ago
Don't script the conversation, script the data you need. Let the LLM classify intent and extract fields (name, date, service) into JSON each turn, and let your code decide what's still missing and when to hand off to a human. Off-script messages stop breaking things because there's no script to fall off.
1
u/London-Boat 24d ago
one thing that helps a lot is keeping the booking state outside the chat, like a small record with the service, date, chosen slot and which step you're on. when a side question comes in, answer it from that record or a faq, then re-send the pending step with the slots again so the user can just tap one. also save the slot list you offered so a wrong button tap can be handled by re-showing it instead of defaulting to an admin handoff. is this for one client with a fixed set of services or does each client have their own?
1
u/_haiderfarooq 24d ago
agree with the others on keeping state outside the chat. the bit that made it click for me: ur code owns the state, the model only classifies. each turn send the message plus the current step and ask for a tiny json back, intent (continue, side_question, restart, human) plus any fields it pulled out. side_question = answer from a faq then resend the pending step with the same slots. store the slots u offered with ids so "the 3pm one" resolves. what model are u calling rn?
1
u/AutoModerator 24d ago
Thank you for your submission, for any questions regarding AI, please check out our wiki at https://www.reddit.com/r/ai_agents/wiki (this is currently in test and we are actively adding to the wiki)
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/stealthagents 21d ago
That's a classic issue with bot development. One way to tackle this is by implementing a state management system that keeps track of the user's context throughout the conversation. You might also consider using a more flexible framework or adding some NLP capabilities to help the bot understand off-script questions and respond accordingly instead of defaulting to admin alerts.
1
u/[deleted] 23d ago
[removed] — view removed comment
0
u/[deleted] 24d ago
[removed] — view removed comment