Start Here / Your First Bot

Your First Bot: Receive, Think, Respond Beginner

Every bot, no matter how fancy, does three things in a loop. Build that loop in about 30 lines and you understand the core of all of them.

Placeholder draft. This tutorial is a first version and will be expanded.

1. The three-step loop

  1. Receive a message (from a terminal, a web form, a chat app, an email...).
  2. Think: decide on a reply, using simple rules or a language model (LLM).
  3. Respond: send the reply back the same way it came.

Keep these three parts separate. Then you can swap the input channel or the "brain" without rewriting everything.

2. What you need

3. Version A: a rule-based brain

def receive():
    return input("You: ")

def think(message):
    text = message.lower()
    if "hello" in text:
        return "Hi there! How can I help?"
    if "time" in text:
        import datetime
        return "It is " + datetime.datetime.now().strftime("%H:%M")
    return "I'm still learning. Try saying hello."

def respond(reply):
    print("Bot:", reply)

while True:
    msg = receive()
    if msg.strip().lower() in ("quit", "exit"):
        break
    respond(think(msg))

Run it with python bot.py. Congratulations: that's a working bot.

4. Version B: swap in an LLM brain

Most LLM providers accept an HTTP request with a list of messages and return text. Only think() changes. The URL, model name and response shape below are generic placeholders; check your provider's docs.

import os, json, urllib.request

API_URL = os.environ["LLM_API_URL"]   # your provider's chat endpoint
API_KEY = os.environ["LLM_API_KEY"]   # never hard-code keys
MODEL   = os.environ.get("LLM_MODEL", "your-model-name")

def think(message):
    body = json.dumps({
        "model": MODEL,
        "messages": [
            {"role": "system", "content": "You are a helpful, brief assistant."},
            {"role": "user", "content": message},
        ],
    }).encode()
    req = urllib.request.Request(API_URL, data=body, headers={
        "Content-Type": "application/json",
        "Authorization": "Bearer " + API_KEY,
    })
    with urllib.request.urlopen(req, timeout=30) as r:
        data = json.load(r)
    # Adjust this line to match your provider's response format:
    return data["choices"][0]["message"]["content"]

Tip: wrap the call in try/except and fall back to the rule-based reply if the network fails.

5. Make it yours

Next steps

Pick a track: API Connections, Email Workflows or Online Stores.