{
  "name": "Flowforge · AI Voice Receptionist Booking",
  "nodes": [
    {
      "parameters": {
        "content": "## AI Voice Receptionist Booking\nTool endpoint for a voice agent (Vapi / Retell). The agent calls it with name, service and preferred time; n8n checks business hours and Google Calendar, books the slot and texts a confirmation, or reads ALL busy intervals (free/busy API) and returns the two nearest free slots that fit the service length and opening hours.\n\nSet Workflow settings → Error workflow to \"Flowforge · Error Reporter\".",
        "height": 240,
        "width": 460,
        "color": 5
      },
      "id": "0000005a-f10f-4f0e-9a1b-000000001090",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        -40
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "voice/book-appointment",
        "responseMode": "responseNode",
        "authentication": "headerAuth",
        "options": {}
      },
      "id": "0000004f-f10f-4f0e-9a1b-000000001079",
      "name": "Webhook: Voice Tool Call",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        400
      ],
      "webhookId": "b7e2d9aa-0ce1-4b00-8a22-000000000005",
      "credentials": {
        "httpHeaderAuth": {
          "id": "cr11",
          "name": "Voice Agent Shared Secret"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "00000046-f10f-4f0e-9a1b-000000001070",
              "name": "callId",
              "value": "={{ $json.body.call.id }}",
              "type": "string"
            },
            {
              "id": "00000047-f10f-4f0e-9a1b-000000001071",
              "name": "patientName",
              "value": "={{ $json.body.args.name }}",
              "type": "string"
            },
            {
              "id": "00000048-f10f-4f0e-9a1b-000000001072",
              "name": "phone",
              "value": "={{ $json.body.call.customer.number }}",
              "type": "string"
            },
            {
              "id": "00000049-f10f-4f0e-9a1b-000000001073",
              "name": "service",
              "value": "={{ $json.body.args.service }}",
              "type": "string"
            },
            {
              "id": "0000004a-f10f-4f0e-9a1b-000000001074",
              "name": "durationMin",
              "value": "={{ $json.body.args.service === \"cleaning\" ? 60 : 30 }}",
              "type": "number"
            },
            {
              "id": "0000004b-f10f-4f0e-9a1b-000000001075",
              "name": "start",
              "value": "={{ DateTime.fromISO($json.body.args.preferred_start, { zone: \"America/Chicago\" }).toISO() }}",
              "type": "string"
            },
            {
              "id": "0000004c-f10f-4f0e-9a1b-000000001076",
              "name": "end",
              "value": "={{ DateTime.fromISO($json.body.args.preferred_start, { zone: \"America/Chicago\" }).plus({ minutes: $json.body.args.service === \"cleaning\" ? 60 : 30 }).toISO() }}",
              "type": "string"
            },
            {
              "id": "0000004d-f10f-4f0e-9a1b-000000001077",
              "name": "withinHours",
              "value": "={{ (() => { const dur = $json.body.args.service === \"cleaning\" ? 60 : 30; const s = DateTime.fromISO($json.body.args.preferred_start, { zone: \"America/Chicago\" }); const e = s.plus({ minutes: dur }); const h = s.weekday <= 5 ? [8, 17] : s.weekday === 6 ? [9, 13] : null; if (!h) return false; const day = s.startOf(\"day\"); return s >= day.plus({ hours: h[0] }) && e <= day.plus({ hours: h[1] }); })() }}",
              "type": "boolean"
            }
          ]
        },
        "options": {}
      },
      "id": "00000050-f10f-4f0e-9a1b-000000001080",
      "name": "Map Booking",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        480,
        400
      ]
    },
    {
      "parameters": {
        "resource": "calendar",
        "calendar": {
          "__rl": true,
          "value": "frontdesk@example-clinic.example",
          "mode": "id"
        },
        "timeMin": "={{ $json.start }}",
        "timeMax": "={{ $json.end }}",
        "options": {
          "outputFormat": "availability",
          "timezone": "America/Chicago"
        }
      },
      "id": "00000051-f10f-4f0e-9a1b-000000001081",
      "name": "Calendar: Availability",
      "type": "n8n-nodes-base.googleCalendar",
      "typeVersion": 1.3,
      "position": [
        720,
        400
      ],
      "credentials": {
        "googleCalendarOAuth2Api": {
          "id": "cr07",
          "name": "Google Calendar – Front Desk"
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "0000004e-f10f-4f0e-9a1b-000000001078",
              "leftValue": "={{ $json.available && $('Map Booking').item.json.withinHours }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "00000052-f10f-4f0e-9a1b-000000001082",
      "name": "IF: Slot Free",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        960,
        400
      ]
    },
    {
      "parameters": {
        "calendar": {
          "__rl": true,
          "value": "frontdesk@example-clinic.example",
          "mode": "id"
        },
        "start": "={{ $('Map Booking').item.json.start }}",
        "end": "={{ $('Map Booking').item.json.end }}",
        "additionalFields": {
          "summary": "={{ $('Map Booking').item.json.service }} – {{ $('Map Booking').item.json.patientName }}",
          "description": "=Booked by AI receptionist. Phone: {{ $('Map Booking').item.json.phone }}. Call ID: {{ $('Map Booking').item.json.callId }}",
          "color": "9"
        }
      },
      "id": "00000053-f10f-4f0e-9a1b-000000001083",
      "name": "Calendar: Book",
      "type": "n8n-nodes-base.googleCalendar",
      "typeVersion": 1.3,
      "position": [
        1200,
        280
      ],
      "credentials": {
        "googleCalendarOAuth2Api": {
          "id": "cr07",
          "name": "Google Calendar – Front Desk"
        }
      }
    },
    {
      "parameters": {
        "from": "+15125550148",
        "to": "={{ $('Map Booking').item.json.phone }}",
        "message": "=Hi {{ $('Map Booking').item.json.patientName.split(' ')[0] }}, you're booked at Example Dental Clinic on {{ DateTime.fromISO($('Map Booking').item.json.start).toFormat('ccc d LLL, h:mm a') }}. Reply C to cancel.",
        "options": {}
      },
      "id": "00000054-f10f-4f0e-9a1b-000000001084",
      "name": "Twilio: SMS Confirm",
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        1440,
        280
      ],
      "credentials": {
        "twilioApi": {
          "id": "cr08",
          "name": "Twilio – SMS"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "REPLACE_WITH_CALL_LOG_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Bookings",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Call ID": "={{ $('Map Booking').item.json.callId }}",
            "Patient": "={{ $('Map Booking').item.json.patientName }}",
            "Phone": "={{ $('Map Booking').item.json.phone }}",
            "Service": "={{ $('Map Booking').item.json.service }}",
            "Start": "={{ $('Map Booking').item.json.start }}",
            "Outcome": "Booked"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "Call ID",
              "displayName": "Call ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Patient",
              "displayName": "Patient",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Phone",
              "displayName": "Phone",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Service",
              "displayName": "Service",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Start",
              "displayName": "Start",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Outcome",
              "displayName": "Outcome",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "cellFormat": "RAW"
        }
      },
      "id": "00000055-f10f-4f0e-9a1b-000000001085",
      "name": "Sheets: Call Log",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1680,
        280
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "cr02",
          "name": "Google Sheets – Ops"
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"result\": \"booked\", \"say\": \"You're all set for {{ DateTime.fromISO($('Map Booking').item.json.start).toFormat('cccc, h:mm a') }}. I've texted you a confirmation.\" }",
        "options": {
          "responseCode": 200
        }
      },
      "id": "00000056-f10f-4f0e-9a1b-000000001086",
      "name": "Respond: Booked",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1920,
        280
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://www.googleapis.com/calendar/v3/freeBusy",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "googleCalendarOAuth2Api",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ timeMin: $('Map Booking').item.json.start, timeMax: DateTime.fromISO($('Map Booking').item.json.start).plus({ days: 7 }).toISO(), timeZone: 'America/Chicago', items: [{ id: 'frontdesk@example-clinic.example' }] }) }}",
        "options": {
          "timeout": 15000
        }
      },
      "id": "00000057-f10f-4f0e-9a1b-000000001087",
      "name": "Calendar: Free/Busy",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1200,
        520
      ],
      "credentials": {
        "googleCalendarOAuth2Api": {
          "id": "cr07",
          "name": "Google Calendar – Front Desk"
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Finds the two nearest free slots from ALL busy intervals, inside business hours and long enough for the service.\nconst zone = 'America/Chicago';\nconst b = $('Map Booking').item.json;\nconst cal = Object.values($input.item.json.calendars || {})[0] || {};\nif (cal.errors && cal.errors.length) throw new Error('Calendar free/busy failed: ' + JSON.stringify(cal.errors));\nconst busy = (cal.busy || []).map(x => [DateTime.fromISO(x.start).toMillis(), DateTime.fromISO(x.end).toMillis()]);\nconst dur = Number(b.durationMin) || 30;\nconst hours = d => (d.weekday <= 5 ? [8, 17] : d.weekday === 6 ? [9, 13] : null);\nconst slots = [];\nlet lastEnd = 0;\nlet t = DateTime.fromISO(b.start, { zone });\nconst rem = t.minute % 30;\nif (rem || t.second) t = t.plus({ minutes: 30 - rem }).startOf('minute');\nfor (let i = 0; i < 7 * 48 && slots.length < 2; i++, t = t.plus({ minutes: 30 })) {\n  const h = hours(t);\n  if (!h) continue;\n  const day = t.startOf('day');\n  const end = t.plus({ minutes: dur });\n  if (t < day.plus({ hours: h[0] }) || end > day.plus({ hours: h[1] })) continue;\n  const s = t.toMillis();\n  const e = end.toMillis();\n  if (s < lastEnd || busy.some(([bs, be]) => s < be && e > bs)) continue;\n  slots.push(t.toISO());\n  lastEnd = e;\n}\nconst fmt = s => DateTime.fromISO(s, { zone }).toFormat(\"cccc 'at' h:mm a\");\nlet say = 'I am sorry, I could not find a free slot in the next seven days. Can I take a message?';\nif (slots.length === 2) say = 'That time is taken. I can offer ' + fmt(slots[0]) + ' or ' + fmt(slots[1]) + '. Which works better?';\nelse if (slots.length === 1) say = 'That time is taken. The next free slot is ' + fmt(slots[0]) + '. Does that work?';\nreturn { json: { slots, say } };"
      },
      "id": "00000058-f10f-4f0e-9a1b-000000001088",
      "name": "Find Free Slots",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1440,
        520
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"result\": \"unavailable\", \"slots\": {{ JSON.stringify($json.slots) }}, \"say\": {{ JSON.stringify($json.say) }} }",
        "options": {
          "responseCode": 200
        }
      },
      "id": "00000059-f10f-4f0e-9a1b-000000001089",
      "name": "Respond: Alternatives",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1680,
        520
      ]
    }
  ],
  "connections": {
    "Webhook: Voice Tool Call": {
      "main": [
        [
          {
            "node": "Map Booking",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Map Booking": {
      "main": [
        [
          {
            "node": "Calendar: Availability",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calendar: Availability": {
      "main": [
        [
          {
            "node": "IF: Slot Free",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF: Slot Free": {
      "main": [
        [
          {
            "node": "Calendar: Book",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Calendar: Free/Busy",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calendar: Book": {
      "main": [
        [
          {
            "node": "Twilio: SMS Confirm",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Twilio: SMS Confirm": {
      "main": [
        [
          {
            "node": "Sheets: Call Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sheets: Call Log": {
      "main": [
        [
          {
            "node": "Respond: Booked",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calendar: Free/Busy": {
      "main": [
        [
          {
            "node": "Find Free Slots",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find Free Slots": {
      "main": [
        [
          {
            "node": "Respond: Alternatives",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true,
    "errorWorkflow": "REPLACE_WITH_ERROR_WORKFLOW_ID"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false,
    "instanceId": "flowforge-concept-demo"
  },
  "tags": [
    {
      "id": "ff-tag-voice",
      "name": "voice"
    },
    {
      "id": "ff-tag-booking",
      "name": "booking"
    },
    {
      "id": "ff-tag-calendar",
      "name": "calendar"
    }
  ]
}