{
 "name": "Missed Call Text Back (n8n)",
 "nodes": [
  {
   "parameters": {
    "httpMethod": "POST",
    "path": "quo-call-missed",
    "responseMode": "onReceived",
    "options": {}
   },
   "name": "Quo call.missed",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2,
   "position": [
    0,
    0
   ],
   "id": "wh-missed",
   "webhookId": "quo-call-missed"
  },
  {
   "parameters": {
    "conditions": {
     "options": {
      "caseSensitive": true,
      "leftValue": "",
      "typeValidation": "loose"
     },
     "conditions": [
      {
       "id": "t1",
       "leftValue": "={{ $json.body.type }}",
       "rightValue": "call.missed",
       "operator": {
        "type": "string",
        "operation": "equals"
       }
      }
     ],
     "combinator": "and"
    },
    "options": {}
   },
   "name": "Is call.missed",
   "type": "n8n-nodes-base.if",
   "typeVersion": 2.2,
   "position": [
    220,
    0
   ],
   "id": "is-missed"
  },
  {
   "parameters": {
    "method": "POST",
    "url": "https://api.quo.com/v1/messages",
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth",
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify({ content: \"Sorry we missed you! This is Brightside Plumbing. Is this an emergency? Reply 1 for yes, or tell us what you need. If you smell gas, leave the building and call 911.\", from: 'YOUR_QUO_PHONE_NUMBER_ID', to: [$json.body.data.context.participants.external[0]] }) }}",
    "options": {}
   },
   "name": "Quo Send Text Back",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    440,
    -80
   ],
   "id": "quo-send-text-back",
   "credentials": {
    "httpHeaderAuth": {
     "id": "PLACEHOLDER",
     "name": "Quo API key (HTTP Header Auth — set in n8n)"
    }
   }
  },
  {
   "parameters": {
    "httpMethod": "POST",
    "path": "quo-message-received",
    "responseMode": "onReceived",
    "options": {}
   },
   "name": "Quo message.received",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2,
   "position": [
    0,
    300
   ],
   "id": "wh-msg",
   "webhookId": "quo-message-received"
  },
  {
   "parameters": {
    "promptType": "define",
    "text": "=From: {{ $json.body.data.context.senderIdentifier }}\nText: {{ $json.body.data.resource.text }}",
    "messages": {
     "messageValues": [
      {
       "message": "You are the office assistant for Brightside Plumbing & Drain. A customer texted back after a missed call. Return ONLY valid JSON (no markdown) with keys: category (one of: emergency, quote, spam), summary (one sentence for the owner). Gas smell, flooding, or active leaks are emergency. Replying '1' means emergency. Wrong numbers and sales pitches are spam. Any language."
      }
     ]
    }
   },
   "name": "Basic LLM Chain",
   "type": "@n8n/n8n-nodes-langchain.chainLlm",
   "typeVersion": 1.5,
   "position": [
    260,
    300
   ],
   "id": "basic-llm-chain"
  },
  {
   "parameters": {
    "model": {
     "__rl": true,
     "value": "gpt-4o-mini",
     "mode": "list"
    },
    "options": {}
   },
   "name": "OpenAI Chat Model",
   "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
   "typeVersion": 1.2,
   "position": [
    260,
    520
   ],
   "id": "openai-chat-model",
   "credentials": {
    "openAiApi": {
     "id": "PLACEHOLDER",
     "name": "OpenAI account (set in n8n)"
    }
   }
  },
  {
   "parameters": {
    "jsCode": "const raw = $input.first().json.text.replace(/```json|```/g,'').trim();\nconst d = JSON.parse(raw);\nconst src = $('Quo message.received').first().json.body;\nreturn [{json:{category:String(d.category).toLowerCase(),summary:d.summary,from:src.data.context.senderIdentifier,text:src.data.resource.text,receivedAt:src.data.resource.createdAt,messageId:src.data.resource.id,loggedAt:new Date().toISOString()}}];"
   },
   "name": "Parse AI JSON",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    540,
    300
   ],
   "id": "parse-ai-json"
  },
  {
   "parameters": {
    "operation": "append",
    "documentId": {
     "__rl": true,
     "value": "YOUR_GOOGLE_SHEET_ID",
     "mode": "id"
    },
    "sheetName": {
     "__rl": true,
     "value": "gid=0",
     "mode": "id"
    },
    "columns": {
     "mappingMode": "defineBelow",
     "value": {
      "Received At": "={{ $json.receivedAt }}",
      "Logged At": "={{ $json.loggedAt }}",
      "From": "={{ $json.from }}",
      "Text": "={{ $json.text }}",
      "Category": "={{ $json.category }}",
      "Summary": "={{ $json.summary }}",
      "Message ID": "={{ $json.messageId }}",
      "Tool": "n8n"
     },
     "matchingColumns": [],
     "schema": []
    },
    "options": {}
   },
   "name": "Log to Sheet",
   "type": "n8n-nodes-base.googleSheets",
   "typeVersion": 4.5,
   "position": [
    800,
    300
   ],
   "id": "log-sheet",
   "credentials": {
    "googleSheetsOAuth2Api": {
     "id": "PLACEHOLDER",
     "name": "Google Sheets account (set in n8n)"
    }
   }
  },
  {
   "parameters": {
    "conditions": {
     "options": {
      "caseSensitive": false,
      "leftValue": "",
      "typeValidation": "loose"
     },
     "conditions": [
      {
       "id": "c1",
       "leftValue": "={{ $('Parse AI JSON').item.json.category }}",
       "rightValue": "emergency",
       "operator": {
        "type": "string",
        "operation": "equals"
       }
      }
     ],
     "combinator": "and"
    },
    "options": {}
   },
   "name": "If Emergency",
   "type": "n8n-nodes-base.if",
   "typeVersion": 2.2,
   "position": [
    1060,
    300
   ],
   "id": "if-emergency"
  },
  {
   "parameters": {
    "sendTo": "YOUR_EMAIL@example.com",
    "subject": "=EMERGENCY text: {{ $('Parse AI JSON').item.json.from }}",
    "message": "={{ $('Parse AI JSON').item.json.summary }}\n\nThey said: {{ $('Parse AI JSON').item.json.text }}\nReceived: {{ $('Parse AI JSON').item.json.receivedAt }}",
    "options": {
     "appendAttribution": false
    }
   },
   "name": "Alert Owner",
   "type": "n8n-nodes-base.gmail",
   "typeVersion": 2.1,
   "position": [
    1320,
    220
   ],
   "id": "alert-owner",
   "credentials": {
    "gmailOAuth2": {
     "id": "PLACEHOLDER",
     "name": "Gmail account (set in n8n)"
    }
   }
  }
 ],
 "connections": {
  "Quo call.missed": {
   "main": [
    [
     {
      "node": "Is call.missed",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Is call.missed": {
   "main": [
    [
     {
      "node": "Quo Send Text Back",
      "type": "main",
      "index": 0
     }
    ],
    []
   ]
  },
  "Quo message.received": {
   "main": [
    [
     {
      "node": "Basic LLM Chain",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "OpenAI Chat Model": {
   "ai_languageModel": [
    [
     {
      "node": "Basic LLM Chain",
      "type": "ai_languageModel",
      "index": 0
     }
    ]
   ]
  },
  "Basic LLM Chain": {
   "main": [
    [
     {
      "node": "Parse AI JSON",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Parse AI JSON": {
   "main": [
    [
     {
      "node": "Log to Sheet",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Log to Sheet": {
   "main": [
    [
     {
      "node": "If Emergency",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "If Emergency": {
   "main": [
    [
     {
      "node": "Alert Owner",
      "type": "main",
      "index": 0
     }
    ],
    []
   ]
  }
 },
 "settings": {
  "executionOrder": "v1"
 },
 "active": false
}
