Send a ChatGPT email reply and save responses to Google Sheets n8n Workflow Template

n8n workflow template · 32 nodes · OpenAI

Send a ChatGPT email reply and save responses to Google Sheets is a free n8n workflow template that starts from the Gmail Trigger node and works with OpenAI, Gmail and Google Sheets. It has 32 nodes, 1 of them AI nodes (OpenAI). The notes inside the workflow say: “This workflow sends a OpenAI GPT reply when an email is received from specific email recipients. It then saves the initial email and the GPT response to an automatically generated Google spreadsheet.” To run it, you add your own credentials for Gmail OAuth2, Google Sheets OAuth2 and OpenAI API.

At a glance

Trigger
Gmail Trigger (On email received)
Nodes
32
Apps
OpenAI, Gmail and Google Sheets
AI nodes
1
Credentials
3 types
Category
Email automation

Apps and integrations

  • OpenAI
  • Gmail
  • Google Sheets

Node types used

  • OpenAI
  • Gmail Trigger
  • If
  • Edit Fields (Set)
  • Gmail
  • Crypto
  • HTML
  • Code
  • Google Sheets
  • Webhook
  • Respond to Webhook
  • Switch

How it works

The text below comes from the sticky notes inside the workflow. We removed links, code samples and anything that looks like a key or token.

Send a ChatGPT email reply when email received and save responses to Google Sheets

This workflow sends a OpenAI GPT reply when an email is received from specific email recipients. It then saves the initial email and the GPT response to an automatically generated Google spreadsheet. Subsequent GPT responses will be added to the same spreadsheet. Additionally, when feedback is given for any of the GPT responses, it will be recorded to the spreasheet, which can then be used later to fine-tune the GPT model.

How it works

This workflow is essentially a two-in-one workflow. It triggers off from two different nodes and have very different functionality from each trigger.

On email received

  1. Triggers off on the On email received node.
  2. Extract the email body from the email.
  3. Generate a response from the email body using the OpenAI node.
  4. Reply to the email sender using the Send reply to recipient node. A feedback link is also included in the email body which will trigger the On feedback given node. This is used to fine-tune the GPT model.
  5. Save the email body and OpenAI response to a Google Sheet. If a sheet does not exist, it will be created.

On feedback given

  1. Triggers off when a feedback link is clicked in the emailed GPT response.
  2. The feedback, either positive or negative, for that specific GPT response is then recorded to the Google Sheet.

This workflow should never reach this node. It is only here for extending the functionality of this workflow if needed.

The email that was received is too large to process, as it exceeds token limit. See more on token limits.

Get spreadsheet ID

The 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.

If your workflow reaches this stage, you will need to consider increasing the tokens in Generate reply node.

Get spreadsheet ID

The 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.

Create spreadsheet and populate with headers and deal information

A spreadsheet is created if the spreadsheet does not exist. The spreadsheet ID is stored in the $getWorkflowStaticData('global') variable. Using Extract current deal node, the deal information is formatted for the sending to the new spreadsheet.

Tip: Deleting old spreadsheets

If you ever want to start over, delete the old spreadsheet, making sure that it is also deleted from Google Drive's trash.

Setup notes

The text below comes from the sticky notes inside the workflow. We removed links, code samples and anything that looks like a key or token.

Configuration

If 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 empty spreadsheetid and worksheetId variables (see the Configure node). Then map the output from Store spreadsheet ID to this node.

It is recommended that you specify the spreadsheetId and worksheetId, since relying solely on a workflow's static data is considered bad practice.

spreadsheetId: The ID of the spreadsheet where Pipedrive deals will be stored. worksheetId: The ID of the worksheet where Pipedrive deals will be stored. spreadsheetName(required): The human readable name of the spreadsheet where Pipedrive deals will be stored. worksheetName(required): The human readable name of the worksheet in the spreadsheet where Pipedrive deals will be stored. recipients(required): Comma-separated list of email recipients to send ChatGPT emails to. Use to send ChatGPT response to every email address. maxTokenSize(required): The maximum token size for the model you choose. See possible models from OpenAI here. replyTokenSize(required): The reply's maximum token size. Default is 300. This determines how much text the AI will reply with.

Nodes in this workflow

The 32 nodes in file order (sticky notes excluded). Branches and connections determine execution order in n8n:

  1. Generate reply (OpenAI)
  2. On email received (Gmail Trigger)
  3. Only continue for specific emails (If)
  4. Configure (Edit Fields (Set))
  5. Send reply to recipient (Gmail)
  6. Generate UUID (Crypto)
  7. Thanks for your response! (HTML)
  8. Extract message content (advanced) (Code)
  9. If spreadsheet doesn't exist (If)
  10. Create spreadsheet (Google Sheets)
  11. Store spreadsheet ID (Code)
  12. Paste data (Google Sheets)
  13. If no sheet IDs (If)
  14. Create or update rows (Google Sheets)
  15. Get data from Format data (Code)
  16. Get data from Format data node (Code)
  17. Format data (Edit Fields (Set))
  18. On feedback given (Webhook)
  19. Send feedback for fine-tuned data (Google Sheets)
  20. Show HTML page (Respond to Webhook)
  21. Get sheet IDs #1 (Code)
  22. If no spreadsheet in configuration #2 (If)
  23. Store specific sheet IDs #2 (Code)
  24. Get sheet IDs #2 (Code)
  25. If no spreadsheet in configuration #1 (If)
  26. Store specific sheet IDs #1 (Code)
  27. Email template (HTML)
  28. Identify trigger #2 (Edit Fields (Set))
  29. Identify trigger #1 (Edit Fields (Set))
  30. If reply is complete (If)
  31. Determine which trigger ran (Switch)
  32. Is text within token limit? (If)

Credentials you need

  • Gmail OAuth2 – used by On email received and Send reply to recipient
  • Google Sheets OAuth2 – used by Create or update rows, Create spreadsheet, Paste data and Send feedback for fine-tuned data
  • OpenAI API – used by Generate reply

Configure your own credentials after importing. Create each credential in n8n, then select it in the node.

How to import into n8n

  1. Download the JSON file with the button above.
  2. In n8n, open a new workflow, select the ... menu and choose Import from File. You can also paste the JSON straight into the editor canvas.
  3. Open each node that shows a warning and select or create its credential.
  4. Read the notes on the canvas, test the workflow with sample data, then activate it.

Do you need somewhere to run it? Read our guide to self-hosted n8n.

Need this customised?

We can adapt this workflow to your apps, data and process, then deploy and support it.

Contact Us