{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "1356b3c8-8cad-4ff4-8262-a691840401ad",
      "name": "Setup Instructions",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -720,
        -560
      ],
      "parameters": {
        "width": 504,
        "height": 1148,
        "content": "# Email chatbot workflow using AI Agent and SendGrid\n\n## Watch this tutorial\n@[youtube](aZU_vAyBuSQ)\n\n## How it works\nThis workflow automatically replies to incoming emails using AI-powered responses:\n- Monitor your inbox for new emails via IMAP connection\n- Extract and clean email content (sender, subject, body)\n- Generate contextual customer service replies using Google Gemini AI\n- Send automated responses through SendGrid with conversation memory\n- Maintain conversation context for follow-up emails\n\n## Setup\n1. **Connect your email:** Add IMAP credentials in the \"Read incoming emails\" node\n2. **Add Google Gemini API:** Get your API key from [Google AI Studio](https://aistudio.google.com/) and connect it to credentials\n3. **Configure SendGrid:** Add your SendGrid API key and set your \"from\" email address in the \"Send reply email\" node\n4. **Customize AI prompt:** Update the prompt in \"Generate reply email\" node to match your brand voice\n5. **Test workflow:** Send a test email to your inbox and verify the auto-reply works correctly\n\n## Need help or want to Customize? Contact me\nEmail: gcgilbs@gmail.com\nLinkedIn: [Gilbert Onyebuchi](https://www.linkedin.com/in/gilbert-onyebuchi/)\n"
      },
      "typeVersion": 1
    },
    {
      "id": "1b31d1d5-5524-4eb0-a595-9e015ed8ad5e",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        64,
        224
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "e80e12a5-79b8-484b-bd2c-912891c542d2",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        224,
        240
      ],
      "parameters": {
        "sessionKey": "session_key",
        "sessionIdType": "customKey"
      },
      "typeVersion": 1.3
    },
    {
      "id": "206c2148-923a-4a12-859b-3cf0eb3bd113",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -208,
        -384
      ],
      "parameters": {
        "color": 7,
        "width": 800,
        "height": 288,
        "content": "## 1. Read incoming email"
      },
      "typeVersion": 1
    },
    {
      "id": "2aa834ca-d260-4801-9128-27f2ba668f10",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -208,
        -80
      ],
      "parameters": {
        "color": 7,
        "width": 800,
        "height": 480,
        "content": "## 2. Generate email reply and send"
      },
      "typeVersion": 1
    },
    {
      "id": "2ac3002e-4966-4bed-91cf-dd3e04ab4c1f",
      "name": "Pause for 20 seconds",
      "type": "n8n-nodes-base.wait",
      "position": [
        96,
        -272
      ],
      "parameters": {
        "amount": 20
      },
      "typeVersion": 1.1
    },
    {
      "id": "f18aa35c-562a-436f-9b31-cca486028648",
      "name": "Set your details",
      "type": "n8n-nodes-base.set",
      "position": [
        320,
        -272
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "5f43db65-a2ab-4f0f-831d-803e355b49f0",
              "name": "from",
              "type": "string",
              "value": "={{ $json.metadata[\"return-path\"] }}"
            },
            {
              "id": "13bfcb4f-88c5-4136-84f2-ab90980e3c61",
              "name": "subject",
              "type": "string",
              "value": "={{ $json.subject }}"
            },
            {
              "id": "21211eda-8f99-4be8-a634-a5e851d993a6",
              "name": "body",
              "type": "string",
              "value": "={{\n  $json.textPlain\n    .split(\"\\r\\n\\r\\n\")\n    .slice(1)\n    .join(\" \")\n    .replace(/\\r\\n/g, \" \")\n    .replace(/\\s+/g, \" \")\n    .replace(/\u00e2\u0080\u0099/g, \"\u2019\")\n    .replace(/\u00e2\u0080\u0093/g, \"\u2013\")\n    .replace(/\u00e2\u0080\u0094/g, \"\u2014\")\n    .replace(/\u00e2\u0080\u009c/g, \"\u201c\")\n    .replace(/\u00e2\u0080\u009d/g, \"\u201d\")\n    .trim()\n}}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "fd1c7b07-eddd-448b-b01f-91e423811384",
      "name": "Check if email exists",
      "type": "n8n-nodes-base.if",
      "notes": "Ensures the email has content before processing",
      "position": [
        -128,
        32
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.from }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "8b0cbe4c-556e-46bd-8123-52f551d1b04b",
      "name": "Send reply email",
      "type": "n8n-nodes-base.sendGrid",
      "position": [
        400,
        16
      ],
      "parameters": {
        "subject": "=Re {{ $('Set your details').item.json.subject }}",
        "toEmail": "={{ $('Set your details').item.json.from }}",
        "fromName": "Your_name",
        "resource": "mail",
        "fromEmail": "Your_email_address",
        "contentValue": "={{ $json.output }}\nGilbert Onyebuchi",
        "additionalFields": {}
      },
      "credentials": {
        "sendGridApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "f1af183f-2008-4512-9b5c-4293a5757b71",
      "name": "Generate reply email",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        96,
        16
      ],
      "parameters": {
        "text": "=You are my helpful customer service chatbot. Give me a reply to this email: \"{{ $json.body }}\"\n\n\n\nRemember. Output only the reply, nothing else",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 3
    },
    {
      "id": "560fe41f-b83f-4f21-b675-38676a2ec8d2",
      "name": "Read incoming emails",
      "type": "n8n-nodes-base.emailReadImap",
      "notes": "CONFIGURE THIS:\n- Host: imap.gmail.com (for Gmail) or your email provider's IMAP server\n- Port: 993 (SSL) or 143 (standard)\n- User: your-email@company.com\n- Password: your email password or app password\n- Check 'Secure': Yes for SSL",
      "position": [
        -128,
        -272
      ],
      "parameters": {
        "options": {},
        "postProcessAction": "nothing"
      },
      "credentials": {
        "imap": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    }
  ],
  "connections": {
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "Generate reply email",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Set your details": {
      "main": [
        [
          {
            "node": "Check if email exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate reply email": {
      "main": [
        [
          {
            "node": "Send reply email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pause for 20 seconds": {
      "main": [
        [
          {
            "node": "Set your details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read incoming emails": {
      "main": [
        [
          {
            "node": "Pause for 20 seconds",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if email exists": {
      "main": [
        [
          {
            "node": "Generate reply email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Generate reply email",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  }
}