# NMC GHL Chat Bot - Architecture

## Flow
```
Customer sends SMS/webchat/FB msg
        ↓
GHL receives message
        ↓
GHL Workflow triggers on "Customer Reply"
        ↓
Workflow → Custom Webhook (POST to our endpoint)
        ↓
Our endpoint:
  1. Receives message + contact info
  2. Loads conversation history from GHL
  3. Sends to AI (with NMC system prompt + pricing tool)
  4. If pricing requested → calls Launch27 API
  5. Returns AI response
        ↓
GHL Workflow receives response
        ↓
Workflow → Send SMS/Chat reply with AI response
```

## Components Needed

### 1. AI Chat Endpoint (Twilio Serverless or similar)
- Receives: message text, contact ID, contact name, message type (SMS/webchat/FB)
- Processes with AI model (need OpenAI or Anthropic API key)
- Can call Launch27 for pricing
- Returns: response text

### 2. GHL Workflow (configured in GHL UI)
- Trigger: Customer Reply
- Action 1: Custom Webhook → our endpoint
- Action 2: Send reply with webhook response

### 3. Conversation Context
- GHL v1 API: GET /conversations/{conversationId}/messages for history
- Or pass last N messages in the webhook payload via GHL custom variables

## API Keys Needed
- OpenAI or Anthropic API key (for the AI model) ← MISSING
- GHL API key (for reading conversation history + sending replies) ← HAVE
- Launch27 API key (for pricing lookups) ← HAVE

## GHL Send Message API (v1)
POST https://rest.gohighlevel.com/v1/conversations/messages
{
  "type": "SMS" | "Live_Chat" | "FB" | "IG",
  "contactId": "xxx",
  "message": "response text"
}
