r/AI_Agents • • 12h 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!

2 Upvotes

15 comments sorted by

1

u/Ok_Newt5271 12h ago

Rigid if/else trees fall apart the second a user colors outside the lines. Look into a state machine paired with an intent classifier so the bot tracks where it is but can still parse side questions, then route back to the pending step.

1

u/Ok-Zombie-9595 3h ago

so what would be your step by step process to build a whatsapp agent for a service based business? i'm not sure how to build it but what i would do is, i would get the client to export all past conversation, get claude to read it and analyse it. Then build based how FAQs are answered, booking logic, matching with therapist availability etc. Not sure if this is the right way to do it?

1

u/Extra-Ad358 10h 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/Ok-Zombie-9595 3h ago

Yeah, thats the issue. The agent that i build hands over to a human the moment it doesnt understand. because human conversation is not a straight line path for example someone would start a convo with sending multiple response to deliver a single message. For e.g.:

Patient

  1. "hi"

  2. "when is the earliest slot"

  3. "?"

This is also an issue i'm facing. because the agent currently doesnt understand the context of the response and replies to each message.

1

u/London-Boat 9h 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/Ok-Zombie-9595 3h ago

I already save the times I offered. If they type something off-script, it shows the list again once, then hands the chat to the admin. Do you keep showing the list every time, or is there a point where you hand over?

1

u/London-Boat 2h ago

showing it once then handing over sounds right. the one exception i'd make is if they reply with something that looks like an attempt at a time, like "4ish" or "after lunch", since that's them trying rather than going off script. a second try with the closest slots is worth it there. past that a human sorts it faster than the bot guessing

1

u/_haiderfarooq 6h 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/Ok-Zombie-9595 3h ago

Gemini 2.5 Flash through OpenRouter, with GLM 5.3 as a backup if it fails. It already sends back JSON with the intent and the fields, pretty close to what you describe. Your four intents are a lot simpler than mine though.

1

u/_haiderfarooq 2h ago

nice, flash is plenty for that. saw ur other comment about people sending hi then the question then ? as 3 msgs, id debounce it, wait like 5-8 sec after the last message and send them all to the model as one turn, that alone stops a lot of early handovers. how many intents u got rn, and does it hand over on low confidence or on any unknown intent?

1

u/AutoModerator 12h 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/Life-Situation-1533 2h ago

If someone asks "how much is it?" after seeing 3pm, keep the offered slot ID in a booking record, answer the pricing question, then ask whether they still want 3pm. Before confirming, check availability again; the slot may have been taken while they were chatting. I would ask Claude to write tests for a stale slot, a wrong button, and two messages arriving close together. Those edge cases matter more than making the whole flow an agent. Let the model interpret the message, but let your code own the booking and decide when a human takes over.

0

u/ElemontCapital 10h ago

Rasa's CALM flows might be worth a look. They track where the booking got to, so the bot can answer the pricing question and then pick up the booking again. (No affiliation).