{
  "name": "WhatsApp Chatbot with Scheduler",
  "nodes": [
    {
      "id": "1",
      "name": "WhatsApp Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        250
      ],
      "parameters": {
        "path": "whatsapp-incoming",
        "httpMethod": "POST"
      }
    },
    {
      "id": "2",
      "name": "Parse Message",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        500,
        250
      ],
      "parameters": {
        "functionCode": "const msg = $json.body.message || \"\";\nlet intent = \"fallback\";\nif (msg.toLowerCase().includes(\"schedule\")) intent = \"schedule\";\nelse if (msg.toLowerCase().includes(\"help\")) intent = \"help\";\n\nreturn [{ intent, msg, from: $json.body.from }];"
      }
    },
    {
      "id": "3",
      "name": "Switch Intent",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        750,
        250
      ],
      "parameters": {
        "property": "={{$json[\"intent\"]}}",
        "rules": [
          {
            "operation": "equal",
            "value": "schedule"
          },
          {
            "operation": "equal",
            "value": "help"
          },
          {
            "operation": "equal",
            "value": "fallback"
          }
        ]
      }
    },
    {
      "id": "4",
      "name": "Send Help Message",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1000,
        150
      ],
      "parameters": {
        "url": "https://api.twilio.com/whatsapp/send",
        "method": "POST",
        "jsonParameters": true,
        "bodyParametersJson": "{ \"to\": \"={{$json[\"from\"]}}\", \"message\": \"Send 'schedule' to add a task. Format: schedule <task> at <time>.\" }"
      }
    },
    {
      "id": "5",
      "name": "Store Task",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 2,
      "position": [
        1000,
        250
      ],
      "parameters": {
        "operation": "append",
        "sheetId": "YOUR_SHEET_ID",
        "range": "Tasks!A:C",
        "options": {},
        "valueInputMode": "RAW"
      },
      "credentials": {
        "googleApi": "<your credential>"
      }
    },
    {
      "id": "6",
      "name": "Confirm Task Saved",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1250,
        250
      ],
      "parameters": {
        "url": "https://api.twilio.com/whatsapp/send",
        "method": "POST",
        "jsonParameters": true,
        "bodyParametersJson": "{ \"to\": \"={{$json[\"from\"]}}\", \"message\": \"\u2705 Your task has been scheduled!\" }"
      }
    },
    {
      "id": "7",
      "name": "Send Fallback Message",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1000,
        350
      ],
      "parameters": {
        "url": "https://api.twilio.com/whatsapp/send",
        "method": "POST",
        "jsonParameters": true,
        "bodyParametersJson": "{ \"to\": \"={{$json[\"from\"]}}\", \"message\": \"\ud83e\udd14 Sorry, I didn\u2019t understand. Send 'help' for options.\" }"
      }
    },
    {
      "id": "8",
      "name": "Cron Scheduler",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        250,
        500
      ],
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      }
    },
    {
      "id": "9",
      "name": "Check Due Tasks",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 2,
      "position": [
        500,
        500
      ],
      "parameters": {
        "operation": "read",
        "sheetId": "YOUR_SHEET_ID",
        "range": "Tasks!A:C"
      },
      "credentials": {
        "googleApi": "<your credential>"
      }
    },
    {
      "id": "10",
      "name": "Send Reminder",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        750,
        500
      ],
      "parameters": {
        "url": "https://api.twilio.com/whatsapp/send",
        "method": "POST",
        "jsonParameters": true,
        "bodyParametersJson": "{ \"to\": \"={{$json[\"UserNumber\"]}}\", \"message\": \"\ud83d\udd14 Reminder: {{$json[\"Task\"]}}\" }"
      }
    }
  ],
  "connections": {
    "WhatsApp Webhook": {
      "main": [
        [
          {
            "node": "Parse Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Message": {
      "main": [
        [
          {
            "node": "Switch Intent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch Intent": {
      "main": [
        [
          {
            "node": "Store Task",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Help Message",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Fallback Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store Task": {
      "main": [
        [
          {
            "node": "Confirm Task Saved",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cron Scheduler": {
      "main": [
        [
          {
            "node": "Check Due Tasks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Due Tasks": {
      "main": [
        [
          {
            "node": "Send Reminder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}