How to Create a Bot with a State Machine: A Step-by-Step Guide

How to Create a Bot with a State Machine: A Step-by-Step Guide
Photo by Alex Knight / Unsplash

Building a bot that behaves intelligently and performs tasks reliably requires a well-structured system, especially as the bot’s complexity grows. One of the most effective ways to manage this complexity is by using a state machine. A state machine helps simplify complicated logic by breaking down a bot’s behavior into distinct "states." The bot then transitions between these states based on user inputs or specific triggers, making it easier to manage, expand, and maintain.

In this blog post, we will explore how to create a bot using a state machine, from understanding the basics of state machines to manually implementing one without the help of external libraries. Whether you're developing a chatbot, automation bot, or any system that needs well-structured logic, this guide will provide the insights you need to build a maintainable bot.

What is a State Machine?

A state machine is a computational model used to design systems where an entity transitions from one state to another based on predefined events or actions. Each state represents a specific phase of operation, and transitions between states occur when certain conditions are met.

For example, in a chatbot designed for booking rides, the states could be:

  1. INIT – The bot introduces itself.
  2. ASK_PICKUP – The bot asks the user for their pickup location.
  3. ASK_DESTINATION – The bot asks the user for their destination.
  4. CONFIRM – The bot asks the user to confirm the ride.
  5. COMPLETE – The bot confirms the ride and completes the conversation.

The transitions between these states happen when the user responds to the bot’s prompts. By managing these transitions through a state machine, you can control the bot’s flow and ensure that it follows the correct sequence of actions.

Why Use a State Machine for Bots?

State machines provide several benefits for bot development:

  1. Simplified Logic: The control flow is clear and structured, avoiding the pitfalls of complex if-else statements or deeply nested loops.
  2. Improved Maintainability: Since each state is self-contained, it’s easier to modify one part of the bot’s behavior without affecting others.
  3. Scalability: As your bot grows in functionality, you can easily add new states and transitions without rewriting the existing logic.
  4. Predictability: State machines ensure that the bot behaves predictably, making it easier to debug and maintain.

How to Build a Bot with a State Machine: Step-by-Step Guide

Let’s go through the process of building a bot with a state machine. In this example, we’ll create a simple Python-based bot for booking a ride. The bot will ask the user for the pickup location, destination, confirm the ride, and complete the booking.

1. Set Up Your Python Environment

To get started, create a project folder and set up a Python environment. You can use virtual environments to keep your dependencies isolated:

mkdir ride_hailing_bot
cd ride_hailing_bot
python3 -m venv env
source env/bin/activate

For simplicity, we won’t rely on any external libraries for the state machine logic. We’ll build everything manually.

2. Design the Bot’s States and Transitions

We will define five states for the bot:

  1. INIT: The bot introduces itself and asks if the user wants to book a ride.
  2. ASK_PICKUP: The bot asks for the user’s pickup location.
  3. ASK_DESTINATION: The bot asks for the user’s destination.
  4. CONFIRM: The bot confirms the ride details.
  5. COMPLETE: The bot finalizes the booking.

The bot will move between these states based on the user’s input.

3. Implement the State Machine Without Libraries

Now let’s manually implement a state machine in Python. Here’s the core code:

class RideHailingBot:
    def __init__(self):
        # Set the initial state to 'INIT'
        self.state = 'INIT'

    # Method to handle state transitions
    def handle_state(self):
        if self.state == 'INIT':
            return self.greet()
        elif self.state == 'ASK_PICKUP':
            return self.ask_pickup()
        elif self.state == 'ASK_DESTINATION':
            return self.ask_destination()
        elif self.state == 'CONFIRM':
            return self.confirm_ride()
        elif self.state == 'COMPLETE':
            return self.complete()

    # Bot actions based on the current state
    def greet(self):
        print("Hello! I'm your ride-hailing bot. Would you like to book a ride?")
        response = input("Type 'yes' to start: ").lower()
        if response == 'yes':
            self.state = 'ASK_PICKUP'
    
    def ask_pickup(self):
        print("Where should I pick you up from?")
        pickup_location = input("Enter your pickup location: ")
        if pickup_location:
            self.state = 'ASK_DESTINATION'
    
    def ask_destination(self):
        print("Where would you like to go?")
        destination = input("Enter your destination: ")
        if destination:
            self.state = 'CONFIRM'
    
    def confirm_ride(self):
        print("Let me confirm your booking...")
        confirmation = input("Type 'confirm' to confirm the ride: ").lower()
        if confirmation == 'confirm':
            self.state = 'COMPLETE'
    
    def complete(self):
        print("Your ride has been confirmed! Thank you.")

4. Manage User Input and State Transitions

The bot will process user input and transition between states based on the input provided. Let’s walk through a full conversation with the bot.

# Instantiate and run the bot
bot = RideHailingBot()

# Run the bot until it reaches the 'COMPLETE' state
while bot.state != 'COMPLETE':
    bot.handle_state()

Expected Output:

Hello! I'm your ride-hailing bot. Would you like to book a ride?
Type 'yes' to start: yes
Where should I pick you up from?
Pickup location: 123 Main St
Where would you like to go?
Destination: 456 Elm St
Let me confirm your booking...
Type 'confirm' to confirm the ride: confirm
Your ride has been confirmed! Thank you.

5. How the State Machine Works

The state machine operates by transitioning between predefined states. In this example:

  1. The bot starts in the INIT state and transitions to ASK_PICKUP after the user confirms they want to book a ride.
  2. The bot asks for the pickup location, then moves to the ASK_DESTINATION state.
  3. After the destination is provided, the bot transitions to the CONFIRM state, where it asks the user to confirm the ride details.
  4. Once confirmed, the bot transitions to the COMPLETE state, finalizing the conversation.

Each state corresponds to a specific action in the conversation flow, making the bot’s behavior predictable and structured.

Expanding the Bot: Adding Error Handling

Now, let’s add a simple error-handling state to make the bot more robust. If the user fails to provide a valid pickup or destination, the bot will prompt them to try again.

class RideHailingBotWithErrorHandling(RideHailingBot):
    def __init__(self):
        super().__init__()
        self.error_state = False

    def handle_state(self):
        if self.error_state:
            print("Sorry, I didn't understand that.")
            self.error_state = False  # Reset error state

        super().handle_state()

    def ask_pickup(self):
        print("Where should I pick you up from?")
        pickup_location = input("Enter your pickup location: ")
        if pickup_location:
            self.state = 'ASK_DESTINATION'
        else:
            self.error_state = True
            print("Pickup location cannot be empty. Please try again.")

How It Works:

  1. The bot enters an error state if the user provides invalid input (e.g., an empty pickup location).
  2. The error message is displayed, and the bot loops back to ask the user for valid input.

Why State Machines Are Essential for Bot Development

State machines provide a structured, predictable way to manage complex logic, making them perfect for bot development. By manually implementing a state machine, you gain a deeper understanding of how it manages transitions and handles different states, making it easier to debug, maintain, and scale your bot.

Key Benefits of Using a State Machine for Bots:

  1. Clarity: The bot’s control flow is clear and easy to follow.
  2. Modularity: Each state is self-contained, allowing for easy updates or modifications.
  3. Scalability: You can easily add new states or transitions as the bot’s functionality grows.
  4. Error Handling: It’s easy to add error states to gracefully handle invalid user input or unexpected situations.

Conclusion: Building a Bot with a State Machine from Scratch

Creating a bot with a state machine is a powerful way to manage its logic and ensure that it behaves predictably. By defining specific states and transitions, you can control the bot’s flow in a clear, structured way. As we’ve shown, you don’t need any external libraries to implement a state machine — just a solid understanding of how state machines work under the hood. By manually managing the states and transitions, you gain full control over the bot's behavior and ensure that it follows a structured path.

Recap of What We've Covered

  • Introduction to State Machines: We discussed how state machines provide a structured way to manage transitions between states, making them ideal for bots.
  • Implementation: We implemented a simple Python bot for booking a ride, using a custom state machine without any libraries.
  • Error Handling: We expanded the bot to include error handling, making it more robust and user-friendly.
  • Benefits of Using State Machines: Finally, we discussed the key benefits, such as clarity, modularity, scalability, and error handling, that state machines bring to bot development.

Scaling the Bot Further

While the example bot we built is simple, it provides a strong foundation for more complex applications. You can easily scale this approach to handle more advanced scenarios such as:

  • Conversation History: Storing previous interactions and using that data to inform future conversations.
  • Advanced Error States: Handling complex user input errors with more detailed guidance.
  • Integration with External APIs: Instead of simulating the bot’s behavior, you can connect the bot to external services, like ride-hailing platforms or weather services, to perform real-time actions.
  • Multi-Step Processes: Expanding the state machine to handle more complex, multi-step workflows, such as multi-party transactions, payment handling, or document submissions.

Real-World Use Cases of State Machines in Bot Development

Here are some real-world use cases where a state machine can help simplify bot logic:

  1. Customer Support Bots: Managing a user’s progress through support flows — handling frequently asked questions, escalating issues, and collecting feedback.
  2. E-Commerce Chatbots: Assisting users with product searches, recommending items, collecting payment information, and processing orders.
  3. Game Bots: Managing player interactions, tracking progress through game levels, and responding to player choices dynamically.
  4. Automation Bots: Automating tasks like scheduling meetings, making API requests, and gathering data across different services.

Final Thoughts

State machines offer a scalable, maintainable way to control a bot’s behavior, making them essential for developers building complex bots or applications with multiple steps. By breaking down the bot’s logic into distinct states and managing transitions between those states, you can create a more organized and predictable system.

As you continue developing your bot, remember that state machines can help maintain clarity and reduce complexity, making it easier to expand the bot’s capabilities while keeping the codebase manageable. Whether you're building a simple chatbot or a more complex automation tool, leveraging state machines is a smart strategy for long-term success.

Subscribe to codingwithalex

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe