{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "parameters": {
        "fields": {
          "values": [
            {
              "name": "phone",
              "stringValue": "={{ $json.body.sender.identifier }}"
            },
            {
              "name": "message",
              "stringValue": "={{ $json.body.content }}"
            },
            {
              "name": "contact id",
              "stringValue": "={{ $json.body.conversation.contact_inbox.contact_id }}"
            },
            {
              "name": "source id",
              "stringValue": "={{ $json.body.conversation.contact_inbox.source_id }}"
            }
          ]
        },
        "include": "none",
        "options": {}
      },
      "id": "10ac54a0-4b81-46b7-9441-b8e4d0a26876",
      "name": "current_customer_info",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.2,
      "position": [
        -200,
        400
      ]
    },
    {
      "parameters": {
        "content": "## conditions\n### channels other than whatsapp / another phone\n### new customer guided reply\n### AI reply in special conditions\n### AI can reply from company data\n### Human can reply when needed\n",
        "height": 245.92121982210932,
        "width": 634.536213468869
      },
      "id": "a180da4b-172a-4a74-bc98-19de600d286d",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -1220,
        640
      ]
    },
    {
      "parameters": {
        "jsCode": "// Loop over input items\nfor (const item of $input.all()) {\n  // Check if the 'phone ' key exists (noting the trailing space)\n  if (item.json && item.json['phone ']) {\n    // Extract the 'phone ' field\n    const phoneField = item.json['phone '];\n\n    // Use a regular expression to extract the numeric phone number\n    const match = phoneField.match(/(\\d+)@s\\.whatsapp\\.net/);\n    if (match) {\n      // Store the extracted phone number back into the item's JSON under a new field 'extractedPhoneNumber'\n      item.json.extractedPhoneNumber = match[1];\n    } else {\n      // Handle cases where the pattern does not match\n      item.json.extractedPhoneNumber = \"No valid identifier found\";\n    }\n  } else {\n    // Handle cases where 'phone ' key is missing\n    item.json.extractedPhoneNumber = \"Required data is missing\";\n  }\n}\n\n// Return the modified input items with the added 'extractedPhoneNumber' field\nreturn $input.all();\n"
      },
      "id": "b8d544c7-547f-4d34-97e6-52a96fa11bc0",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        180,
        100
      ]
    },
    {
      "parameters": {
        "fields": {
          "values": [
            {
              "name": "phone ",
              "stringValue": "={{ $('Webhook').item.json.body.meta.sender.identifier }}"
            }
          ]
        },
        "include": "none",
        "options": {}
      },
      "id": "f42bfa29-6870-4154-82c9-660450497c29",
      "name": "new customer",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.2,
      "position": [
        -180,
        140
      ]
    },
    {
      "parameters": {
        "jsCode": "// Loop over input items\nfor (const item of $input.all()) {\n  // Check if the 'phone' key exists\n  if (item.json && item.json['phone']) {\n    // Extract the 'phone' field\n    const phoneField = item.json['phone'];\n\n    // Use a regular expression to extract the numeric phone number\n    const match = phoneField.match(/(\\d+)@s\\.whatsapp\\.net/);\n    if (match) {\n      // Store the extracted phone number back into the item's JSON under a new field 'extractedPhoneNumber'\n      item.json.extractedPhoneNumber = match[1];\n    } else {\n      // Handle cases where the pattern does not match\n      item.json.extractedPhoneNumber = \"No valid identifier found\";\n    }\n  } else {\n    // Handle cases where 'phone' key is missing\n    item.json.extractedPhoneNumber = \"Required data is missing\";\n  }\n}\n\n// Return the modified input items with the added 'extractedPhoneNumber' field\nreturn $input.all();\n"
      },
      "id": "7978a7ca-857b-40fb-a210-7467509f6a3a",
      "name": "Code1",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        40,
        400
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://wa.mu3lnen.com/message/sendText/UK",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "xfgavi7o7"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n   \"number\":\"{{ $json.extractedPhoneNumber }}\",\n   \"options\":{\n      \"delay\":1200,\n      \"presence\":\"composing\",\n      \"linkPreview\":false\n   },\n   \"textMessage\":{\n      \"text\":\"custom text here\ud83d\ude09\ud83e\udd23\ud83e\udd29\ud83e\udd1d\ud83d\udc4f\ud83d\udc4d\ud83d\ude4f\"\n   }\n}",
        "options": {
          "redirect": {
            "redirect": {}
          }
        }
      },
      "id": "15af9ed6-47ce-4a87-adad-45f00c9d8fea",
      "name": "send message for new customer",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        500,
        100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://wa.mu3lnen.com/message/sendText/UK",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "xfgak0d2vi7o7"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n   \"number\":\"{{ $('Code1').item.json.extractedPhoneNumber }}\",\n   \"options\":{\n      \"delay\":1200,\n      \"presence\":\"composing\",\n      \"linkPreview\":false\n   },\n   \"textMessage\":{\n      \"text\":\"{{ $json.output }}\"\n   }\n}",
        "options": {
          "redirect": {
            "redirect": {}
          }
        }
      },
      "id": "7bdebf6a-6e9f-4e16-a571-f85de89f9492",
      "name": "send message for current customer",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1240,
        460
      ]
    },
    {
      "parameters": {},
      "id": "e3f437a9-c625-4ff7-afd8-63a0bbea8def",
      "name": "No Operation, do nothing",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        -460,
        560
      ]
    },
    {
      "parameters": {},
      "id": "1a55d186-593b-47ab-b43e-d1ed2f980063",
      "name": "human reply",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        720,
        300
      ]
    },
    {
      "parameters": {
        "model": "gpt-4",
        "options": {}
      },
      "id": "57fda915-bcc7-4ee7-8259-5dc4a42faadf",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1,
      "position": [
        580,
        740
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "name": "amazon_retriver",
        "description": "Call this tool to get requested information about amazon products, like price, sales, revenue, best selling products ...etc",
        "workflowId": "k0RweIrJQsieB6Wl"
      },
      "id": "c130feb1-ef31-4578-b7f4-f716d6edf0ec",
      "name": "Custom n8n Workflow Tool",
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "typeVersion": 1,
      "position": [
        920,
        720
      ]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "4"
      },
      "id": "bc4fb39f-0cea-4c4f-8f01-ded8f9bca173",
      "name": "Window Buffer Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.2,
      "position": [
        780,
        680
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.body.event }}",
                    "rightValue": "=conversation_created",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "new customer"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "8edf54ef-cf7a-4e59-936a-ca3a1d6ac8d6",
                    "leftValue": "={{ $json.body.event }}",
                    "rightValue": "message_created",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "current customer"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "e0bf26f4-4696-489f-ad68-2b050c879d5c",
                    "leftValue": "={{ $json.body.conversation.channel }}",
                    "rightValue": "Channel::Api",
                    "operator": {
                      "type": "string",
                      "operation": "notEquals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "other channel"
            }
          ]
        },
        "options": {}
      },
      "id": "9bac261a-09c6-4e4b-9b82-22f7668d96ce",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        -620,
        340
      ]
    },
    {
      "parameters": {
        "mode": "expression",
        "numberOutputs": 2,
        "output": "={{ $json.message.toLowerCase().includes(\"ai\") }}"
      },
      "id": "9847ee56-5381-44bf-ae90-686ee974cef1",
      "name": "Human or AI reply",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        260,
        400
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "ad0678f2-a082-4575-b092-6d2eafa52106",
        "options": {}
      },
      "id": "0fe9a366-b2ff-44ba-b000-a7c1fe41c483",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1.1,
      "position": [
        -1000,
        340
      ]
    },
    {
      "parameters": {
        "text": "={{ $json.message }}",
        "options": {}
      },
      "id": "a3626a35-6ffd-4b63-94e5-0b7f4a11ee70",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.2,
      "position": [
        740,
        460
      ]
    }
  ],
  "connections": {
    "current_customer_info": {
      "main": [
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "send message for new customer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "new customer": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code1": {
      "main": [
        [
          {
            "node": "Human or AI reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Custom n8n Workflow Tool": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Window Buffer Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "new customer",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "current_customer_info",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Human or AI reply": {
      "main": [
        [
          {
            "node": "human reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "send message for current customer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}