{
  "name": "Retell functions · receptionist (ep01 EN, Blackcircuit Labs)",
  "nodes": [
    {
      "parameters": {
        "content": "## Retell functions (ep01, English)\n1. Assign Google Calendar and Gmail credentials.\n2. Change `your-calendar@gmail.com` (both calendar nodes) and `marta@example.com`.\n3. Activate, then paste each webhook's production URL in the matching Retell custom function.\nSample data is fictional. Blackcircuit Labs.",
        "height": 240,
        "width": 380
      },
      "name": "Read me",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -480,
        -320
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "retell-check-availability",
        "responseMode": "responseNode",
        "options": {}
      },
      "name": "Check availability",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "webhookId": "retell-check-availability"
    },
    {
      "parameters": {
        "operation": "getAll",
        "calendar": {
          "__rl": true,
          "mode": "id",
          "value": "your-calendar@gmail.com"
        },
        "returnAll": true,
        "timeMin": "={{ DateTime.fromISO(($json.body.args || $json.body).date, { zone: 'America/Chicago' }).startOf('day').toISO() }}",
        "timeMax": "={{ DateTime.fromISO(($json.body.args || $json.body).date, { zone: 'America/Chicago' }).endOf('day').toISO() }}",
        "options": {
          "orderBy": "startTime",
          "recurringEventHandling": "expand"
        }
      },
      "name": "Appointments that day",
      "type": "n8n-nodes-base.googleCalendar",
      "typeVersion": 1.3,
      "position": [
        220,
        0
      ],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "jsCode": "// Open slots for one day, using the business hours of template 2 (change them here if yours differ).\nconst HOURS = { 1: [9, 19], 2: [9, 19], 3: [9, 19], 4: [9, 19], 5: [9, 19], 6: [9, 14] }; // 0 = Sunday, closed\nconst req = $('Check availability').first().json;\nconst args = req.body.args || req.body;\nconst tz = 'America/Chicago';\nconst day = DateTime.fromISO(args.date, { zone: tz });\nconst dur = Number(args.duration_minutes || 45);\nconst h = HOURS[day.weekday % 7];\nif (!day.isValid) return [{ json: { result: 'I could not understand that date.' } }];\nif (!h) return [{ json: { result: `We're closed on ${day.toFormat('cccc')}.` } }];\nconst busy = $input.all().map(i => i.json).filter(e => e.start?.dateTime)\n  .map(e => [DateTime.fromISO(e.start.dateTime), DateTime.fromISO(e.end.dateTime)]);\nconst now = DateTime.now().setZone(tz);\nconst free = [];\nfor (let t = day.set({ hour: h[0], minute: 0 }); t.plus({ minutes: dur }) <= day.set({ hour: h[1] - 1, minute: 0 }).plus({ minutes: 60 }); t = t.plus({ minutes: 30 })) {\n  const end = t.plus({ minutes: dur });\n  if (t < now.plus({ minutes: 30 })) continue;\n  if (end > day.set({ hour: h[1], minute: 0 })) break;\n  if (busy.some(([s, e]) => t < e && end > s)) continue;\n  free.push(t);\n}\nconst say = (t) => t.toFormat('h:mm a').replace(':00', '').toLowerCase().replace('am', 'a.m.').replace('pm', 'p.m.');\nreturn [{ json: { result: free.length\n  ? `Open times on ${day.toFormat('cccc, LLLL d')}: ${free.slice(0, 6).map(say).join(', ')}`\n  : `No open times on ${day.toFormat('cccc, LLLL d')}.`, slots: free.slice(0, 6).map(t => t.toISO()) } }];"
      },
      "name": "Open times",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { result: $json.result } }}",
        "options": {}
      },
      "name": "Answer: open times",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        660,
        0
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "retell-book-appointment",
        "responseMode": "responseNode",
        "options": {}
      },
      "name": "Book appointment",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        240
      ],
      "webhookId": "retell-book-appointment"
    },
    {
      "parameters": {
        "calendar": {
          "__rl": true,
          "mode": "id",
          "value": "your-calendar@gmail.com"
        },
        "start": "={{ DateTime.fromISO(($json.body.args || $json.body).start, { zone: 'America/Chicago' }).toISO() }}",
        "end": "={{ DateTime.fromISO(($json.body.args || $json.body).start, { zone: 'America/Chicago' }).plus({ minutes: Number(($json.body.args || $json.body).duration_minutes || 45) }).toISO() }}",
        "additionalFields": {
          "summary": "={{ ($json.body.args || $json.body).service }} - {{ ($json.body.args || $json.body).name }}",
          "description": "=Phone: {{ ($json.body.args || $json.body).phone }}\nName: {{ ($json.body.args || $json.body).name }}\nService: {{ ($json.body.args || $json.body).service }}\nBooked by: Lia (Retell)"
        }
      },
      "name": "Create appointment",
      "type": "n8n-nodes-base.googleCalendar",
      "typeVersion": 1.3,
      "position": [
        220,
        240
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { result: 'Booked: ' + ($('Book appointment').first().json.body.args || $('Book appointment').first().json.body).service + ' for ' + ($('Book appointment').first().json.body.args || $('Book appointment').first().json.body).name + ' on ' + DateTime.fromISO($json.start.dateTime).setZone('America/Chicago').toFormat(\"cccc, LLLL d 'at' h:mm a\") } }}",
        "options": {}
      },
      "name": "Answer: booked",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        440,
        240
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "retell-request-callback",
        "responseMode": "responseNode",
        "options": {}
      },
      "name": "Request callback",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        480
      ],
      "webhookId": "retell-request-callback"
    },
    {
      "parameters": {
        "sendTo": "marta@example.com",
        "subject": "=Lia: please call back {{ ($json.body.args || $json.body).name }}",
        "emailType": "text",
        "message": "=Name: {{ ($json.body.args || $json.body).name }}\nPhone: {{ ($json.body.args || $json.body).phone }}\nReason: {{ ($json.body.args || $json.body).reason }}\n\nLia told them you'll call back today."
      },
      "name": "Email Marta",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [
        220,
        480
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { result: 'Callback request sent to Marta.' } }}",
        "options": {}
      },
      "name": "Answer: callback noted",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        440,
        480
      ]
    }
  ],
  "connections": {
    "Check availability": {
      "main": [
        [
          {
            "node": "Appointments that day",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Appointments that day": {
      "main": [
        [
          {
            "node": "Open times",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Open times": {
      "main": [
        [
          {
            "node": "Answer: open times",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Book appointment": {
      "main": [
        [
          {
            "node": "Create appointment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create appointment": {
      "main": [
        [
          {
            "node": "Answer: booked",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Request callback": {
      "main": [
        [
          {
            "node": "Email Marta",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Email Marta": {
      "main": [
        [
          {
            "node": "Answer: callback noted",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "timezone": "America/Chicago"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false,
    "generatedBy": "tools/material_flujo_retell.py"
  }
}
