n8n workflow: Gmail AI auto-responder: create draft replies to incoming emails

Nodes:
name: Gmail Trigger
type: n8n-nodes-base.gmailTrigger
parameters: simple: no
filters: q: -from:me
pollTimes: item: everyMinute
name: If Needs Reply
type: n8n-nodes-base.if
parameters: conditions: options: caseSensitive: yes
typeValidation: strict
combinator: and
conditions: operator: type: boolean
operation: true
singleValue: yes
leftValue: ={{ $json.needsReply }}
rightValue: true
name: JSON Parser
type: @n8n/n8n-nodes-langchain.outputParserStructured
parameters: jsonSchema: {
  "type": "object",
  "properties": {
    "needsReply": {
      "type": "boolean"
      }
  },
  "required": ["needsReply"]
}

name: OpenAI Chat
type: @n8n/n8n-nodes-langchain.lmChatOpenAi
parameters: model: gpt-4o
options: temperature: 0
responseFormat: json_object
name: Sticky Note
type: n8n-nodes-base.stickyNote
parameters: width: 304.10628068244364
height: 394.42512272977456
content: ## When I receive an Email

name: Sticky Note1
type: n8n-nodes-base.stickyNote
parameters: width: 556
height: 397
content: ## ... that Needs a Reply

name: Sticky Note2
type: n8n-nodes-base.stickyNote
parameters: width: 333.19082443588354
height: 400.08454375343996
content: ## Generate a Reply
name: Sticky Note3
type: n8n-nodes-base.stickyNote
parameters: width: 326
height: 395
content: ## ...as a Draft in the conversation
name: OpenAI Chat Model
type: @n8n/n8n-nodes-langchain.lmChatOpenAi
parameters: model: gpt-4-turbo
name: Gmail - Create Draft
type: n8n-nodes-base.gmail
parameters: message: ={{ $json.text.replace(/\n/g, "<br />\n") }}
options: sendTo: ={{ $('Gmail Trigger').item.json.headers.from }}
threadId: ={{ $('Gmail Trigger').item.json.threadId }}
subject: =Re: {{ $('Gmail Trigger').item.json.headers.subject }}
resource: draft
emailType: html
name: Assess if message needs a reply
type: @n8n/n8n-nodes-langchain.chainLlm
parameters: prompt: =Subject: {{ $json.subject }}
Message:
{{ $json.textAsHtml }} 
messages: messageValues: Your task is to assess if the message requires a response. Return in JSON format true if it does, false otherwise.
Marketing emails don't require a response.
name: Generate email reply
type: @n8n/n8n-nodes-langchain.chainLlm
parameters: text: =Subject: {{ $('Gmail Trigger').item.json.subject }}
Message: {{ $('Gmail Trigger').item.json.textAsHtml }}
messages: messageValues: You're a helpful personal assistant and your task is to draft replies on my behalf to my incoming emails. Whenever I provide some text from an email, return an appropriate draft reply for it and nothing else.
Ensure that the reply is suitable for a professional email setting and addresses the topic in a clear, structured, and detailed manner.
Do not make things up.

Detailed instructions:
- Be concise and maintain a business casual tone.
- Start with "Hello,", and end with "Best,"
- When replying to yes-no questions, draft 2 responses: one affirmative and one negative separated by " - - - - - - - OR - - - - - - - "
- If you don't know an answer, you can leave placeholders like "[YOUR_ANSWER_HERE]".
- Don't use any special formatting, only plain text.
- Reply in the same language as the inbound email.
promptType: define

Connections:
JSON Parser: ai_outputParser: node: Assess if message needs a reply
type: ai_outputParser
OpenAI Chat: ai_languageModel: node: Assess if message needs a reply
type: ai_languageModel
Gmail Trigger: main: node: Assess if message needs a reply
type: main
If Needs Reply: main: node: Generate email reply
type: main
OpenAI Chat Model: ai_languageModel: node: Generate email reply
type: ai_languageModel
Generate email reply: main: node: Gmail - Create Draft
type: main
Assess if message needs a reply: main: node: If Needs Reply
type: main