{
  "name": "Patient Appointment Booking WhatsApp Agent",
  "nodes": [
    {
      "parameters": {
        "resource": "calendar",
        "calendar": {
          "__rl": true,
          "value": "primary",
          "mode": "list",
          "cachedResultName": "Primary"
        },
        "timeMin": "={{ $fromAI('Start_Time', ``, 'string') }}",
        "timeMax": "={{ $fromAI('End_Time', ``, 'string') }}",
        "options": {}
      },
      "id": "d67be493-6c43-4474-8180-5ebd83527a29",
      "name": "Check Availability",
      "type": "n8n-nodes-base.googleCalendarTool",
      "position": [
        -128,
        48
      ],
      "typeVersion": 1.3,
      "credentials": {
        "googleCalendarOAuth2Api": {
          "id": "xMYib7VUIpkJCTQG",
          "name": "Google Calendar account"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Twilio Trigger').item.json.From }}"
      },
      "id": "028159e3-594c-439e-a3c6-005bc2b20227",
      "name": "Window Buffer Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        -256,
        48
      ],
      "typeVersion": 1.3
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1zOVxpDieE7-28ohFYhyGN1ZDD95JWsPSZcRqgAotyJc",
          "mode": "list",
          "cachedResultName": "Patient Scheduling(Healthcare)"
        },
        "sheetName": {
          "__rl": true,
          "value": 1539472103,
          "mode": "list",
          "cachedResultName": "Patient Scheduling"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Appointment ID": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Appointment_ID', ``, 'string') }}",
            "Name": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Name', ``, 'string') }}",
            "Email": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Email', ``, 'string') }}",
            "Phone Number": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Phone_Number', ``, 'string') }}",
            "Date": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Date', ``, 'string') }}",
            "Time": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Time', ``, 'string') }}",
            "Event ID": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Event_ID', `collect from Create event tool`, 'string') }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "Appointment ID",
              "displayName": "Appointment ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Event ID",
              "displayName": "Event ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Name",
              "displayName": "Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Email",
              "displayName": "Email",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Phone Number",
              "displayName": "Phone Number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Date",
              "displayName": "Date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Time",
              "displayName": "Time",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [
        0,
        48
      ],
      "id": "d0c18710-1ee2-44ee-bd5e-baa645205310",
      "name": "Append Appointment Details",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "GojUvCMDXbhY8HrB",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "1zOVxpDieE7-28ohFYhyGN1ZDD95JWsPSZcRqgAotyJc",
          "mode": "list",
          "cachedResultName": "Patient Scheduling(Healthcare)"
        },
        "sheetName": {
          "__rl": true,
          "value": 1539472103,
          "mode": "list",
          "cachedResultName": "Patient Scheduling"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Date": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Date', ``, 'string') }}",
            "Time": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Time', ``, 'string') }}",
            "Appointment ID": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Appointment_ID__using_to_match_', ``, 'string') }}"
          },
          "matchingColumns": [
            "Appointment ID"
          ],
          "schema": [
            {
              "id": "Appointment ID",
              "displayName": "Appointment ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Event ID",
              "displayName": "Event ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Name",
              "displayName": "Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Email",
              "displayName": "Email",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Phone Number",
              "displayName": "Phone Number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Date",
              "displayName": "Date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Time",
              "displayName": "Time",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "row_number",
              "displayName": "row_number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true,
              "readOnly": true,
              "removed": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [
        128,
        48
      ],
      "id": "d79ec967-c0dd-41fc-886a-359d6c4df53b",
      "name": "Re-schedule Appointment",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "GojUvCMDXbhY8HrB",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "1zOVxpDieE7-28ohFYhyGN1ZDD95JWsPSZcRqgAotyJc",
          "mode": "list",
          "cachedResultName": "Patient Scheduling(Healthcare)"
        },
        "sheetName": {
          "__rl": true,
          "value": 1539472103,
          "mode": "list",
          "cachedResultName": "Patient Scheduling"
        },
        "filtersUI": {
          "values": [
            {
              "lookupColumn": "Appointment ID",
              "lookupValue": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('values0_Value', ``, 'string') }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.6,
      "position": [
        256,
        48
      ],
      "id": "8bdd9928-c94c-453b-b2d6-21fd8908adf8",
      "name": "Get booking details",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "GojUvCMDXbhY8HrB",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.chatInput }}",
        "options": {
          "systemMessage": "=You are a virtual assistant specializing in appointment management for General clinic Hospital. \nYour role is to manage consultations accurately, ensuring real availability while providing a smooth experience for patients.\n\nFirst greet users with the only Welcome to Forgent Clinic name on first message only.\n\nHospital Hours:\n- Monday – Friday: 9:00 AM – 8:00 PM\n- Saturday: 9:00 AM – 1:00 PM\n- Sunday: Closed\n- Consultation Duration: 1 hour\n- Break Between Patients: 15 minutes\n\n- User provide a outside of our operating time\n    Response only: \"{{ 'time' }} slot would be outside of our operating time. Could you let me know a preferred time between 9:00AM To 8:00PM today (or another day that works for you)?\"\n\n- check a day and date is Sunday \n      Response only: \"The date you provided (2025‑09‑21) falls on a Sunday, and our clinic is closed on Sundays.Could you please let me know a preferred date (Monday – Saturday) 9:00AM To 8:00PM\"\n\nBooking Process:\n1. Collect Patient Information:\n   - Full Name\n   - Email Address\n   - Desired Date and Time\n   - If any information is missing, politely ask the patient before proceeding.\n   - Always check the appointment date and time against the current time: {{ $now.setZone(\"America/New_York\").toFormat(\"yyyy-MM-dd h:mma\") }}\n\n2. Availability Check:\n   - Always use the \"Check Availability\" tool to verify free slots in Google Calendar.\n   - If requested time overlaps with an existing appointment, suggest the next available slot.\n   - Do not create or confirm an appointment until the user explicitly approves the slot.\n\n3. Booking Confirmation:\n   - create the appointment using the \"Create Event\" tool after explicit user confirmation.\n   - Do not say \"appointment booked\" unless the tool is actually used.\n   - Appointment ID format: APT-YYYYMMDD-XXXXXX (Always set 6 random alphanumeric characters and don't miss it).\n   - Event Summary: Patient Name - Phone Number\n   - Event Description: Include full name, phone, email, appointment ID.\n\nConfirmation Message Format:\nWhen booking is complete, return only this message:\n✅ Appointment Confirmed:\n- Full Name: {full_name}\n- Phone: {phone}\n- Email: {email}\n- Date: {date}\n- Time: {time}\n- Appointment ID: {appointment_id}\n\nConfirm appointment after call \"Append Appointment Details\" tool to store all details.\n\nDo NOT include:\n- Extra text after the appointment ID\n- Any confirmation without Create Event tool usage\n\nRescheduling a Booking:\nIf the user wants to 'Reschedule', follow this process: \n  - Asking a only first of for Appointment id message and provide id \n  - After call \"Get booking details\" tool and available existing details \n  - After asking new Preferred date and time for reschedule.\n  - Collect new date and time \n  - After first check a availability using \"Check Availability\" tool and after update new data and call \"Get booking details\" tool for Event id.\n  - Update Reschedule new date and time using the \"Reschedule booking\" and \"Update event\" tool.\n  - Don,t call a \"Append Appointment Details\" tool for reschedule appointment process.\nConfirm with a message like:\n\"Your appointment booking has been successfully rescheduled to [new date/time]!\"\n\nCancellations:\nIf asked to cancel, Send Response:\"I am Sorry,Please connect with help desk care to cancel your appointment. We’ll guide you through the next steps!\".\n\nVerification:\nIf asked to check an appointment by ID, reply: \"Check your email we sent when you booked.\"\n\nTools Available:\n- Check Availability → Verify free slots in Google Calendar\n- Create Event → Book confirmed appointment in Google Calendar\n\nCommunication Rules:\n- Always be polite, clear, and friendly.\n- Never assume missing details—ask for them.\n- Never create or cancel without explicit confirmation.\n- Stick strictly to availability check and booking after confirmation.\n- Always check the appointment date and time against the current time: {{ $now.setZone(\"America/New_York\").toFormat(\"yyyy-MM-dd h:mma\") }}\n- If the patient provides a past date, reply:\n“The date you entered is in the past. Please provide a future date for your appointment.”\n- If the patient provides today’s date but a past time, reply:\n“The time you entered has already passed today. Please choose a valid future time.”\n- Only confirm the appointment once you have a full name, email, and a valid future date & time.\n\nHandle Today, Tomorrow, weekdays using:\n{{ $now.setZone(\"America/New_York\").toFormat(\"yyyy-MM-dd h:mma\") }} {{ [\"Sunday\",\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\"][ $now.setZone(\"America/New_York\").weekday % 7 ] }}\n\nPhone number : {{ $json.From.replace('whatsapp:+1', '') }}"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 2.2,
      "position": [
        -8,
        -176
      ],
      "id": "52bc2dba-1861-403e-abf7-eea39dc986f9",
      "name": "Patient Appointment Scheduling"
    },
    {
      "parameters": {
        "operation": "update",
        "calendar": {
          "__rl": true,
          "value": "primary",
          "mode": "list",
          "cachedResultName": "Primary"
        },
        "eventId": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Event_ID', `collect from 'Get booking details' tools`, 'string') }}",
        "useDefaultReminders": false,
        "updateFields": {
          "end": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('End', ``, 'string') }}",
          "start": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Start', ``, 'string') }}"
        }
      },
      "type": "n8n-nodes-base.googleCalendarTool",
      "typeVersion": 1.3,
      "position": [
        384,
        48
      ],
      "id": "9f107232-5364-4a45-ab85-0bb31a281a5b",
      "name": "Update event",
      "credentials": {
        "googleCalendarOAuth2Api": {
          "id": "xMYib7VUIpkJCTQG",
          "name": "Google Calendar account"
        }
      }
    },
    {
      "parameters": {
        "updates": [
          "com.twilio.messaging.inbound-message.received"
        ]
      },
      "type": "n8n-nodes-base.twilioTrigger",
      "typeVersion": 1,
      "position": [
        -336,
        -176
      ],
      "id": "019908bb-1a7d-4d1b-88fd-82036ddde842",
      "name": "Twilio Trigger",
      "webhookId": "2a0af9be-bbcd-4ead-9ba0-e34dce29bbb0",
      "credentials": {
        "twilioApi": {
          "id": "xmQ7NblKKLEiOZwC",
          "name": "Twilio account"
        }
      }
    },
    {
      "parameters": {
        "from": "={{ $('Twilio Trigger').item.json.To }}",
        "to": "={{ $('Twilio Trigger').item.json.From }}",
        "message": "={{ $json.output }}",
        "options": {}
      },
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        416,
        -176
      ],
      "id": "c69e3855-8dea-42b5-a450-7c43684248d1",
      "name": "Send an SMS/MMS/WhatsApp message",
      "credentials": {
        "twilioApi": {
          "id": "xmQ7NblKKLEiOZwC",
          "name": "Twilio account"
        }
      }
    },
    {
      "parameters": {
        "calendar": {
          "__rl": true,
          "value": "primary",
          "mode": "list",
          "cachedResultName": "Primary"
        },
        "start": "={{ $fromAI('Start', ``, 'string') }}",
        "end": "={{ $fromAI('End', ``, 'string') }}",
        "useDefaultReminders": false,
        "additionalFields": {
          "attendees": [],
          "description": "={{ $fromAI('Description', ``, 'string') }}",
          "summary": "={{ $fromAI('Summary', ``, 'string') }}"
        }
      },
      "id": "e486b381-7f39-478b-8f90-0fca5b1e934c",
      "name": "Create event",
      "type": "n8n-nodes-base.googleCalendarTool",
      "position": [
        512,
        48
      ],
      "typeVersion": 1.3,
      "credentials": {
        "googleCalendarOAuth2Api": {
          "id": "xMYib7VUIpkJCTQG",
          "name": "Google Calendar account"
        }
      }
    },
    {
      "parameters": {
        "content": "## Data Setup\nCreate your own spreadsheet with these tabs/columns, then select it in the Google Sheets nodes:\n- **Patient Scheduling(Healthcare) → Patient Scheduling**: `Appointment ID`, `Event ID`, `Name`, `Email`, `Phone Number`, `Date`, `Time`",
        "height": 96,
        "width": 480
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -336,
        -304
      ],
      "typeVersion": 1,
      "id": "0475bde1-80c3-45f5-842e-9e41069b9726",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "model": "openai/gpt-oss-120b",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "typeVersion": 1,
      "position": [
        -384,
        48
      ],
      "id": "59f84696-373c-4aa5-8366-af8a9ef184bd",
      "name": "OpenRouter Chat Model",
      "credentials": {
        "openRouterApi": {
          "id": "XDH1ME5qL4duPuQc",
          "name": "OpenRouter account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Check Availability": {
      "ai_tool": [
        [
          {
            "node": "Patient Appointment Scheduling",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Window Buffer Memory": {
      "ai_memory": [
        [
          {
            "node": "Patient Appointment Scheduling",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Append Appointment Details": {
      "ai_tool": [
        [
          {
            "node": "Patient Appointment Scheduling",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Re-schedule Appointment": {
      "ai_tool": [
        [
          {
            "node": "Patient Appointment Scheduling",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Get booking details": {
      "ai_tool": [
        [
          {
            "node": "Patient Appointment Scheduling",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Update event": {
      "ai_tool": [
        [
          {
            "node": "Patient Appointment Scheduling",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Twilio Trigger": {
      "main": [
        [
          {
            "node": "Patient Appointment Scheduling",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Patient Appointment Scheduling": {
      "main": [
        [
          {
            "node": "Send an SMS/MMS/WhatsApp message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create event": {
      "ai_tool": [
        [
          {
            "node": "Patient Appointment Scheduling",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Patient Appointment Scheduling",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "3d4fa4dd-2b7d-4c10-9b13-21f948e6d2b7",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "f1c26778ec36097a9722a22c47c5e9c01f00b8862dadd0121ec118026a370fa6"
  },
  "id": "VarI1vjK95bMjofa",
  "tags": []
}