{
  "name": "sms agent",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "ee0a43de-9ea0-4b53-80de-0d32552fbfab",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -180,
        0
      ],
      "id": "c5015aa2-537f-4ed7-881d-32269c1543ff",
      "name": "Webhook",
      "webhookId": "ee0a43de-9ea0-4b53-80de-0d32552fbfab"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $('Code').item.json.messages }}",
        "options": {
          "systemMessage": "Your job is to reply to one or multiple messages.\n\nYou will receive an array of one or multiple messages related to general enquires and also gmail functions\n\nTo send an email use the send_email tool. to get the email address of the recipient use the google sheets tool to return a list of names with email and match up to requested persons name with their email address.\n\nTo get info on emails use the get_emails tool which will return the last 50 emails and find what the user was looking for"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.8,
      "position": [
        360,
        220
      ],
      "id": "45b227d9-29ea-4104-94ab-443d6ed824a6",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4.1-mini-2025-04-14",
          "mode": "list",
          "cachedResultName": "gpt-4.1-mini-2025-04-14"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        160,
        360
      ],
      "id": "104ce4a7-7318-4725-b32e-22236b5fe014",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "id": "xX6Qn7dUJHcs40V9",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Code').item.json.messages }}"
      },
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        260,
        440
      ],
      "id": "34a3f504-905d-4f99-8968-ef8ec43f5e93",
      "name": "Simple Memory"
    },
    {
      "parameters": {
        "from": "+447458008548",
        "to": "={{ $('Code').item.json.from }}",
        "message": "={{ $('AI Agent').item.json.output }}",
        "options": {}
      },
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        760,
        220
      ],
      "id": "7a4808da-bc5e-4d57-93e1-2d7544b0cfe8",
      "name": "Twilio",
      "credentials": {
        "twilioApi": {
          "id": "ZDQp8uf0Tfd5K2V7",
          "name": "Twilio account"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1hsSP-EAipqgpR4_8IyrSiZfF1yhHkth51IHon6ljYt4",
          "mode": "list",
          "cachedResultName": "Sms messages",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1hsSP-EAipqgpR4_8IyrSiZfF1yhHkth51IHon6ljYt4/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1hsSP-EAipqgpR4_8IyrSiZfF1yhHkth51IHon6ljYt4/edit#gid=0"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Message": "={{ $json.body.Body }}"
          },
          "matchingColumns": [
            "Message"
          ],
          "schema": [
            {
              "id": "Message",
              "displayName": "Message",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        20,
        0
      ],
      "id": "89795855-cd88-4656-af34-d0489dc14ce6",
      "name": "Google Sheets",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "tkQ5ewTob0DdThOL",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "amount": 10
      },
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        200,
        0
      ],
      "id": "a3db895b-857b-4eea-8662-36c8980a978a",
      "name": "Wait",
      "webhookId": "a9b6c90c-e129-40c8-ad1b-6ba61fb55857"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "1hsSP-EAipqgpR4_8IyrSiZfF1yhHkth51IHon6ljYt4",
          "mode": "list",
          "cachedResultName": "Sms messages",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1hsSP-EAipqgpR4_8IyrSiZfF1yhHkth51IHon6ljYt4/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1hsSP-EAipqgpR4_8IyrSiZfF1yhHkth51IHon6ljYt4/edit#gid=0"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        380,
        0
      ],
      "id": "fbf31761-2b11-449c-9d47-cdf479618180",
      "name": "Google Sheets1",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "tkQ5ewTob0DdThOL",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "sendTo": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('To', ``, 'string') }}",
        "subject": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Subject', ``, 'string') }}",
        "emailType": "text",
        "message": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Message', ``, 'string') }}",
        "options": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [
        600,
        480
      ],
      "id": "1c9a5668-c0ac-4dd7-b8c2-90873eddc7ee",
      "name": "Gmail",
      "webhookId": "ecfdaa7b-6506-4091-bbe7-ad4a4f5378c1",
      "credentials": {
        "gmailOAuth2": {
          "id": "LnrPa2bag9FoINre",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "operation": "getAll",
        "limit": 20,
        "filters": {}
      },
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [
        500,
        480
      ],
      "id": "bcf11224-83c3-4d76-8dbf-795b15156866",
      "name": "Gmail1",
      "webhookId": "16656f03-c078-4466-b116-7ab532d9aafd",
      "credentials": {
        "gmailOAuth2": {
          "id": "LnrPa2bag9FoINre",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "1dC5PMEgo39Wunqbh5krF21vxgBFybstrvVYtAKhK6lM",
          "mode": "list",
          "cachedResultName": "Name with emails",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1dC5PMEgo39Wunqbh5krF21vxgBFybstrvVYtAKhK6lM/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1dC5PMEgo39Wunqbh5krF21vxgBFybstrvVYtAKhK6lM/edit#gid=0"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.5,
      "position": [
        380,
        460
      ],
      "id": "b260bba4-d693-4b8b-8446-1db760ccfaed",
      "name": "Google Sheets3",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "tkQ5ewTob0DdThOL",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "operation": "delete",
        "documentId": {
          "__rl": true,
          "value": "1hsSP-EAipqgpR4_8IyrSiZfF1yhHkth51IHon6ljYt4",
          "mode": "list",
          "cachedResultName": "Sms messages",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1hsSP-EAipqgpR4_8IyrSiZfF1yhHkth51IHon6ljYt4/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1hsSP-EAipqgpR4_8IyrSiZfF1yhHkth51IHon6ljYt4/edit#gid=0"
        },
        "startIndex": "=2",
        "numberToDelete": "={{ $json.totalMessages }}"
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        740,
        0
      ],
      "id": "ef36e4f9-0a84-42af-9394-45a0b88fdbc9",
      "name": "Google Sheets4",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "tkQ5ewTob0DdThOL",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\nconst from = $('Webhook').first().json.body.From;\n\nconst messages = items.map(item => \n  `Row ${item.json.row_number}: ${item.json.Message}`\n);\n\n// Join the messages into one string\nconst messageString = messages.join('\\n');\n\nreturn [\n  {\n    json: {\n      from,\n      messages: messageString, // now a single string, not an array\n      totalMessages: messages.length\n    }\n  }\n];\n\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        560,
        0
      ],
      "id": "6c041fec-6366-47b3-9522-e57e6d62ffcf",
      "name": "Code"
    }
  ],
  "pinData": {},
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Twilio",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "Google Sheets1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets1": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Gmail1": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets3": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets4": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "Google Sheets4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "f18d3673-f987-4580-9b91-a5fac4615ffc",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "8711ef6aba5ff6781b8ed3c0d9d8ad1fb3b9e4be41f1c9a056c02209236d4a91"
  },
  "id": "jzN0PAgaxnA6pWgD",
  "tags": []
}