{
  "name": "Send a ChatGPT email reply and save responses to Google Sheets",
  "nodes": [
    {
      "id": "88c0f64c-a7cd-4f35-96dd-9eee4b1d6a1a",
      "name": "Generate reply",
      "type": "n8n-nodes-base.openAi",
      "position": [
        -480,
        2260
      ]
    },
    {
      "id": "7105b689-9f9c-4354-aad9-8f1abb6c0a06",
      "name": "On email received",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -2460,
        2680
      ]
    },
    {
      "id": "ea18ed9a-0158-45e1-ac1b-1993ace4ff2c",
      "name": "Only continue for specific emails",
      "type": "n8n-nodes-base.if",
      "position": [
        -1360,
        2460
      ]
    },
    {
      "id": "d1425dff-0fc1-4a4b-9202-418ce30d7cd9",
      "name": "Configure",
      "type": "n8n-nodes-base.set",
      "position": [
        -1940,
        2800
      ]
    },
    {
      "id": "594f77e6-9e7e-4e93-b6e0-95fad57e42f0",
      "name": "Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2060,
        2480
      ],
      "parameters": {
        "width": 330.0279884670691,
        "height": 929.4540475960038,
        "content": "### Configuration\nIf you decide to use your own spreadsheet, it is up to you to ensure all columns are present before running this workflow. A good way to do this is to run this workflow once with **e"
      }
    },
    {
      "id": "2dc3e403-f2a0-43c2-a1e4-187d901d692f",
      "name": "Send reply to recipient",
      "type": "n8n-nodes-base.gmail",
      "position": [
        360,
        1860
      ]
    },
    {
      "id": "f845aa4d-5542-4126-a42d-4e5afa1893d1",
      "name": "Generate UUID",
      "type": "n8n-nodes-base.crypto",
      "position": [
        -1140,
        2360
      ]
    },
    {
      "id": "3c468585-4546-439b-9e8a-efb7231277d8",
      "name": "Thanks for your response!",
      "type": "n8n-nodes-base.html",
      "position": [
        -1140,
        2980
      ]
    },
    {
      "id": "6b0bfa33-84ca-4b9c-98ec-c1bc08a1230d",
      "name": "Extract message content (advanced)",
      "type": "n8n-nodes-base.code",
      "position": [
        -920,
        2360
      ]
    },
    {
      "id": "4f6998f6-88a8-4b8b-acea-33c3f33d04dd",
      "name": "If spreadsheet doesn't exist",
      "type": "n8n-nodes-base.if",
      "position": [
        1420,
        2500
      ]
    },
    {
      "id": "f3564023-a1c5-42f5-923d-a8e98c95c284",
      "name": "Successfully created or updated row",
      "type": "n8n-nodes-base.noOp",
      "position": [
        1660,
        2640
      ]
    },
    {
      "id": "55869b16-3a98-4127-83ec-bcfdf21c2daf",
      "name": "Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        980,
        2140
      ],
      "parameters": {
        "width": 778.177339901478,
        "height": 289.16256157635416,
        "content": "### Create spreadsheet and populate with headers and deal information\nA spreadsheet is created if the spreadsheet does not exist. The spreadsheet ID is stored in the `$getWorkflowStaticData('global')`"
      }
    },
    {
      "id": "8994f1e7-dd0d-4247-89fd-befcc9c511b0",
      "name": "Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1220,
        2680
      ],
      "parameters": {
        "width": 301.18226600985224,
        "height": 114.67980295566498,
        "content": "### Tip: Deleting old spreadsheets\nIf you ever want to start over, delete the old spreadsheet, __making sure that it is also deleted from Google Drive's trash__."
      }
    },
    {
      "id": "cd8c9657-3380-4e25-907e-baa1c02c0793",
      "name": "Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        400,
        2140
      ],
      "parameters": {
        "width": 260.3940886699507,
        "height": 333.34975369458095,
        "content": "### `Get spreadsheet ID`\n\n\n\n\n\n\n\n\n\n\n\n\n\nThe spreadsheet ID is stored in this workflow's static data. If you want to refresh the static data you will need to copy this entire workflow into a new workflow"
      }
    },
    {
      "id": "ab0348c2-f688-42d3-815b-63290e95baad",
      "name": "Create spreadsheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1020,
        2260
      ]
    },
    {
      "id": "c56522b2-5eca-497d-afbb-d713abd8d810",
      "name": "Store spreadsheet ID",
      "type": "n8n-nodes-base.code",
      "position": [
        1220,
        2260
      ]
    },
    {
      "id": "ba62fd4d-912b-4b37-9fda-2f80cdeb65f8",
      "name": "Paste data",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1620,
        2260
      ]
    },
    {
      "id": "a8be831a-f2be-48c9-a661-bc8c5cde6444",
      "name": "If no sheet IDs",
      "type": "n8n-nodes-base.if",
      "position": [
        800,
        2380
      ]
    },
    {
      "id": "efdb343d-f5bf-4ba4-bc27-850b9e7935ac",
      "name": "Create or update rows",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1220,
        2500
      ]
    },
    {
      "id": "091ad4fa-21aa-42e0-abc5-17221cdf8fb7",
      "name": "Get data from `Format data`",
      "type": "n8n-nodes-base.code",
      "position": [
        1020,
        2500
      ]
    },
    {
      "id": "97071540-59b2-48dd-8f88-ab44446832fc",
      "name": "Get data from `Format data` node",
      "type": "n8n-nodes-base.code",
      "position": [
        1420,
        2260
      ]
    },
    {
      "id": "ecf03802-51c8-43b1-84d8-5ed5826fd444",
      "name": "Format data",
      "type": "n8n-nodes-base.set",
      "position": [
        -40,
        2380
      ]
    },
    {
      "id": "9eedd7b7-ec4e-4dbf-a257-33e73bdff9c1",
      "name": "Send email reply",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -40,
        1860
      ]
    },
    {
      "id": "8e2f4a3b-d224-4248-9682-184a646e022f",
      "name": "On feedback given",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -2460,
        2940
      ]
    },
    {
      "id": "87506e44-21aa-4f08-82f9-f47a24ddb9ce",
      "name": "Send feedback for fine-tuned data",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -100,
        2980
      ]
    },
    {
      "id": "d2a720d4-8487-4dfa-bdb8-6b59368e44bc",
      "name": "Show HTML page",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        -920,
        2980
      ]
    },
    {
      "id": "2da7a7b1-e96d-4759-b3cb-13558e2ad1d4",
      "name": "Get sheet IDs #1",
      "type": "n8n-nodes-base.code",
      "position": [
        480,
        2200
      ]
    },
    {
      "id": "08ddeed5-fefe-4acd-918a-00d1fd5a5392",
      "name": "Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -480,
        2780
      ],
      "parameters": {
        "width": 260.3940886699507,
        "height": 333.34975369458095,
        "content": "### `Get spreadsheet ID`\n\n\n\n\n\n\n\n\n\n\n\n\n\nThe spreadsheet ID is stored in this workflow's static data. If you want to refresh the static data you will need to copy this entire workflow into a new workflow"
      }
    },
    {
      "id": "49d77f89-3c1e-4e86-93e8-ae7a566802b7",
      "name": "If no spreadsheet in configuration #2",
      "type": "n8n-nodes-base.if",
      "position": [
        -700,
        2980
      ]
    },
    {
      "id": "e3b8f696-41eb-46e1-a4b1-6ba2d219aa45",
      "name": "Store specific sheet IDs #2",
      "type": "n8n-nodes-base.code",
      "position": [
        -400,
        3180
      ]
    },
    {
      "id": "44d37f76-af16-4507-b1a1-76fadf530806",
      "name": "Get sheet IDs #2",
      "type": "n8n-nodes-base.code",
      "position": [
        -400,
        2840
      ]
    },
    {
      "id": "fae8cbc5-7462-4eb0-9f60-85e8e7cfd10e",
      "name": "If no spreadsheet in configuration #1",
      "type": "n8n-nodes-base.if",
      "position": [
        180,
        2380
      ]
    },
    {
      "id": "67312347-74c0-4ce4-a78c-615da6937bcf",
      "name": "Store specific sheet IDs #1",
      "type": "n8n-nodes-base.code",
      "position": [
        480,
        2540
      ]
    },
    {
      "id": "400eae76-7b17-48de-a49f-8b0cbc9db1f8",
      "name": "Email template",
      "type": "n8n-nodes-base.html",
      "position": [
        160,
        1860
      ]
    },
    {
      "id": "38e0f992-a461-4bc1-9f5c-2ceb0e461708",
      "name": "Record feedback",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -1360,
        2980
      ]
    },
    {
      "id": "899a0c63-0333-4dc4-ba83-5615a38ae431",
      "name": "Fallback route",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -1360,
        3280
      ]
    },
    {
      "id": "2fd5b109-8a54-4684-a8a3-3f7b2d961ae3",
      "name": "Identify trigger #2",
      "type": "n8n-nodes-base.set",
      "position": [
        -2240,
        2940
      ]
    },
    {
      "id": "8c27f798-d947-432c-bfc9-d22727d0159e",
      "name": "Identify trigger #1",
      "type": "n8n-nodes-base.set",
      "position": [
        -2240,
        2680
      ]
    },
    {
      "id": "bd8cc1dd-3643-4d2f-9527-cfd740a4072a",
      "name": "Do not send unfinished email reply",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -40,
        2060
      ]
    },
    {
      "id": "c8b68fdb-c1c0-4f94-b712-e0570a3ad53c",
      "name": "If reply is complete",
      "type": "n8n-nodes-base.if",
      "position": [
        -260,
        1960
      ]
    },
    {
      "id": "f9d56d42-aa4e-4394-8c83-8d39164a784e",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -100,
        2020
      ],
      "parameters": {
        "width": 225.59802712700315,
        "height": 314.2786683107279,
        "content": "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nIf your workflow reaches this stage, you will need to consider increasing the tokens in `Generate reply` node."
      }
    },
    {
      "id": "039714b3-88ac-4ca8-86fc-ec1c109110c3",
      "name": "Do not send email to this recipient",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -1140,
        2560
      ]
    },
    {
      "id": "330c67dd-e538-414d-a144-e05dbf5effb3",
      "name": "Send reply to database",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -260,
        2380
      ]
    },
    {
      "id": "6e7586db-f437-4450-a1c7-e5ea7e8767b0",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3060,
        2520
      ],
      "parameters": {
        "width": 516.6954377311955,
        "height": 680.5491163173024,
        "content": "## Send a ChatGPT email reply when email received and save responses to Google Sheets\nThis workflow sends a OpenAI GPT reply when an email is received from specific email recipients. It then saves the"
      }
    },
    {
      "id": "9d5e780e-4282-4c7e-b083-3f769f7dc740",
      "name": "Determine which trigger ran",
      "type": "n8n-nodes-base.switch",
      "position": [
        -1660,
        2800
      ]
    },
    {
      "id": "2c6c604c-7f59-42cc-9ed2-6d55f342f0ae",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1420,
        3240
      ],
      "parameters": {
        "width": 225.59802712700315,
        "height": 289.61775585696694,
        "content": "\n\n\n\n\n\n\n\n\n\n\n\n\n\nThis workflow should never reach this node. It is only here for extending the functionality of this workflow if needed."
      }
    },
    {
      "id": "3defbf98-0caa-49b1-9bfd-f4640b43d64b",
      "name": "Is text within token limit?",
      "type": "n8n-nodes-base.if",
      "position": [
        -700,
        2360
      ]
    },
    {
      "id": "b268b8a3-6361-4515-a995-320cd0979688",
      "name": "Do nothing",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -480,
        2460
      ]
    },
    {
      "id": "413588d1-ede0-4a51-85fa-c9035ec2e605",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -540,
        2420
      ],
      "parameters": {
        "width": 225.59802712700315,
        "height": 288.2949081608216,
        "content": "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nThe email that was received is too large to process, as it exceeds token limit. See more on [token limits](https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-t"
      }
    }
  ],
  "connections": {
    "Configure": {
      "main": [
        [
          {
            "node": "Determine which trigger ran",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format data": {
      "main": [
        [
          {
            "node": "If no spreadsheet in configuration #1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate UUID": {
      "main": [
        [
          {
            "node": "Extract message content (advanced)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Email template": {
      "main": [
        [
          {
            "node": "Send reply to recipient",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate reply": {
      "main": [
        [
          {
            "node": "Send reply to database",
            "type": "main",
            "index": 0
          },
          {
            "node": "If reply is complete",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Show HTML page": {
      "main": [
        [
          {
            "node": "If no spreadsheet in configuration #2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If no sheet IDs": {
      "main": [
        [
          {
            "node": "Create spreadsheet",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get data from `Format data`",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Record feedback": {
      "main": [
        [
          {
            "node": "Thanks for your response!",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get sheet IDs #1": {
      "main": [
        [
          {
            "node": "If no sheet IDs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get sheet IDs #2": {
      "main": [
        [
          {
            "node": "Send feedback for fine-tuned data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send email reply": {
      "main": [
        [
          {
            "node": "Email template",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "On email received": {
      "main": [
        [
          {
            "node": "Identify trigger #1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "On feedback given": {
      "main": [
        [
          {
            "node": "Identify trigger #2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create spreadsheet": {
      "main": [
        [
          {
            "node": "Store spreadsheet ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Identify trigger #1": {
      "main": [
        [
          {
            "node": "Configure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Identify trigger #2": {
      "main": [
        [
          {
            "node": "Configure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If reply is complete": {
      "main": [
        [
          {
            "node": "Send email reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Do not send unfinished email reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store spreadsheet ID": {
      "main": [
        [
          {
            "node": "Get data from `Format data` node",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create or update rows": {
      "main": [
        [
          {
            "node": "If spreadsheet doesn't exist",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send reply to database": {
      "main": [
        [
          {
            "node": "Format data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Thanks for your response!": {
      "main": [
        [
          {
            "node": "Show HTML page",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Determine which trigger ran": {
      "main": [
        [
          {
            "node": "Only continue for specific emails",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Record feedback",
            "type": "main",
            "index": 0
          }
        ],
        null,
        [
          {
            "node": "Fallback route",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get data from `Format data`": {
      "main": [
        [
          {
            "node": "Create or update rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is text within token limit?": {
      "main": [
        [
          {
            "node": "Generate reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Do nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store specific sheet IDs #1": {
      "main": [
        [
          {
            "node": "If no sheet IDs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store specific sheet IDs #2": {
      "main": [
        [
          {
            "node": "Send feedback for fine-tuned data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If spreadsheet doesn't exist": {
      "main": [
        [
          {
            "node": "Create spreadsheet",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Successfully created or updated row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get data from `Format data` node": {
      "main": [
        [
          {
            "node": "Paste data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Only continue for specific emails": {
      "main": [
        [
          {
            "node": "Generate UUID",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Do not send email to this recipient",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract message content (advanced)": {
      "main": [
        [
          {
            "node": "Is text within token limit?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If no spreadsheet in configuration #1": {
      "main": [
        [
          {
            "node": "Get sheet IDs #1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Store specific sheet IDs #1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If no spreadsheet in configuration #2": {
      "main": [
        [
          {
            "node": "Get sheet IDs #2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Store specific sheet IDs #2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}