Simple Chatbot

The next most basic use case of unconscious is to build a simple application on top of it.

In this example, we will build a simple chatbot that can respond to a few basic commands.

Of course, this is just a starting point. You can build much more complex applications on top of unconscious.

Prerequisites

Before we start building the chatbot, make sure you have unconscious installed on your system. If not, you can follow the installation instructions.

Building the chatbot

To build the chatbot, we will create a new Python file chatbot.py and write the following code:

from unconscious.agent import npc, NpcManager
from unconscious.client import msg
from unconscious import Client

@msg # Define a message type
class SimpleMessage:
  senders_name: str
  message: str

@npc # Define a new NPC class
class Alice:
  def on_alive(self):
    print(f"[{self.name}] ({self.name}): I'm alive, sending a message")
    self.send_message({"simple_message": {"senders_name": self.name, "message": "Hello"}})

  def on_message(self, message, client_id):
    if message.is_type(SimpleMessage) and message.senders_name != self.name:
    print(f"[{self.name}] ({message.senders_name}): Received a simple   message: {message.message}")
    self.send_message({"simple_message": {"senders_name": self.name, "message": "Hi"}})


manager = NpcManager()

for npc_class in [Alice]:
    manager.register_npc(npc_class)

manager.start()
Back to Documentation