← Back to all files

00 — Overview

Project: WhatsApp shopping chatbot for mart.tanisha-bd.com Purpose: Learning project. Clarity over cleverness. Stack: Laravel 13, PHP 8.3, MySQL 8, Redis (queue), Claude API


What you are building

A customer messages your business on WhatsApp. An AI reads the message, looks up real products in your MySQL database, builds a cart, collects their address, and places a real order — all through conversation.

Customer: hi, do you have sunsilk shampoo?
Bot:      Yes! Sunsilk Shampoo 400ml is 450 BDT and we have 12 in stock.
          How many would you like?
Customer: 2 please
Bot:      Added 2 x Sunsilk Shampoo 400ml. Subtotal: 900 BDT.
          What name should I put on the delivery?
...
Bot:      Here's your order:
          2 x Sunsilk Shampoo 400ml — 900 BDT
          Delivery (Dhaka Metro) — 60 BDT
          Total: 960 BDT
          Cash on delivery to House 12, Road 5, Dhanmondi.
          Shall I place it?
Customer: yes
Bot:      Done! Your order is TM-2026-004821. We'll deliver in 2-3 days.

The core idea: tool calling

The AI model knows nothing about your products. It cannot query your database. What it can do is say "I want to call check_stock with query: sunsilk shampoo".

Your PHP code receives that request, runs a normal Eloquent query, and hands the result back to the model. The model then writes a sentence for the customer.

┌──────────┐   "do you have sunsilk?"   ┌───────────┐
│ Customer │ ─────────────────────────► │ Your PHP  │
└──────────┘                            └─────┬─────┘
                                              │ message + list of available tools
                                              ▼
                                        ┌───────────┐
                                        │  Claude   │
                                        └─────┬─────┘
                                              │ "call check_stock(query: sunsilk)"
                                              ▼
                                        ┌───────────┐
                                        │ Your PHP  │ ──► MySQL ──► {price: 450, stock: 12}
                                        └─────┬─────┘
                                              │ tool result
                                              ▼
                                        ┌───────────┐
                                        │  Claude   │
                                        └─────┬─────┘
                                              │ "Yes! It's 450 BDT, 12 in stock."
                                              ▼
                                        ┌──────────┐
                                        │ Customer │
                                        └──────────┘

This loop — model asks for a tool, you run it, you feed the result back — is the whole architecture. Everything else is plumbing around it.

The model never writes SQL and never sees your database. It only picks which tool to call and what arguments to pass. Your Action classes decide whether that's allowed.


Message flow

sequenceDiagram
    participant C as Customer
    participant M as Meta WhatsApp
    participant W as WebhookController
    participant Q as Queue
    participant O as ChatOrchestrator
    participant AI as Claude API
    participant DB as MySQL

    C->>M: sends message
    M->>W: POST /webhooks/whatsapp
    W->>W: verify signature
    W->>DB: save message (skip if duplicate)
    W->>Q: dispatch job
    W-->>M: 200 OK

    Q->>O: run()
    O->>DB: load session + history + cart
    O->>AI: message + tools

    loop up to 5 times
        AI-->>O: "call check_stock"
        O->>DB: Eloquent query
        O->>AI: tool result
    end

    AI-->>O: final text
    O->>M: send reply
    M->>C: delivers message

Why the queue? Meta expects a response within about 20 seconds or it retries the webhook. An AI call plus tool calls can take 10–20 seconds. So the controller saves the message, pushes a job, and returns 200 immediately. The slow work happens in the background.


Files you will create

Area Files Spec
Config config/services.php additions 03
Migrations 4 new tables 02
Models WhatsAppSession, WhatsAppMessage, Cart, CartItem 02
Webhook Controller, signature middleware, route 03
WhatsApp sending WhatsAppService 03
Tools 5 Action classes + ToolRegistry 04
Enums CartAction, PaymentMethod 04
AI client ClaudeClient + interface 05
Orchestrator ChatOrchestrator + HandleIncomingMessage job 05
Widget Blade component 03

Roughly 25 files. Build them in spec order — each one only depends on the ones before it.


Suggested build path

Do not start with WhatsApp. Getting a Meta Business account approved takes days, and you do not want to be blocked on paperwork while learning the interesting part.

Phase 1 — tools and AI loop (no WhatsApp). Build the database, the Action classes, and the orchestrator. Test it from php artisan tinker by passing text in and reading text out. This is where the actual learning is.

// Works with zero WhatsApp setup
$session = WhatsAppSession::firstOrCreate(['phone_number' => '8801700000000']);
$reply = app(ChatOrchestrator::class)->run($session, 'do you have shampoo?');
echo $reply;

Phase 2 — a web chat page. A simple Blade page with a text box that posts to a controller and calls the same orchestrator. Now you can click through real conversations.

Phase 3 — WhatsApp. Register a Meta test number, point the webhook at ngrok, and connect the same orchestrator to the real channel. Nothing you built in phases 1 and 2 changes.

This ordering means every phase produces something working.


Prerequisites

Thing Why Cost
Existing Laravel project You have it —
Anthropic API key The AI Pay per use, a few dollars covers heavy testing
Redis Queue + locks Free, local
ngrok or similar Expose localhost to Meta Free tier works
Meta Developer account WhatsApp Cloud API Free
WhatsApp test number Meta provides one free Free, limited to 5 recipients

Phase 1 needs only the API key.


Design decisions

These were chosen for clarity. The reasoning is noted so you understand what you would change for a real production build.

# Decision Why for study Production would
D-1 No Repository layer. Actions use Eloquent directly. One less hop to follow when reading code Add repositories if the app is large
D-2 carts / cart_items are new tables They were not in the existing schema Same
D-3 Payment methods: cash on delivery, bKash, Nagad, card Realistic for Bangladesh Same
D-4 No conversation summarization in v1 Adds a whole background job for little benefit under 20 turns Add rolling summary — see 06 §6
D-5 LIKE search first, FULLTEXT noted as upgrade LIKE is readable and fine under ~5k products FULLTEXT or Meilisearch
D-6 Claude Sonnet 4.6 (claude-sonnet-4-6) Reliable tool calling, reasonable cost Same, plus a cheaper model for simple turns
D-7 Stock is not reserved when added to cart Avoids reservation-expiry machinery Depends on contention
D-8 Customer table assumed to be customers — Check your actual schema

Three rules that matter

Everything else is detail. These three are what separate a safe chatbot from a dangerous one, and they are the real lessons in this project.

1. Identity comes from the webhook, never from the AI. No tool accepts a phone number. If the AI could pass one, a customer could say "my phone number is 8801712345678" and shop as someone else. The phone number comes from Meta's signed payload and is injected by your code.

2. Prices and stock come from the database, never from the AI. No tool accepts a price. If place_order took a total argument, a customer could talk the AI into a total of 1 taka. Your code reads the price, does the arithmetic, and ignores whatever the AI thinks the number is.

3. Orders need a server-issued confirmation token. Telling the AI "only place an order after confirmation" is a suggestion, not a guarantee. Instead, your server generates a token when it shows the order summary, and place_order rejects any call without a matching one. See 04 §7.

If you understand why each of these is structural rather than prompt-based, you have understood the security model of AI agents.


Reading order

  1. 01-requirements.md — what it does
  2. 02-database.md — tables and models
  3. 03-whatsapp.md — webhook in, messages out
  4. 04-tools.md — the five tools
  5. 05-orchestrator.md — the AI loop and prompt
  6. 06-testing.md — running it, testing it, improving it