{
  "id": "4w6vXQK63QpwF3JW",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Whatsapp/Telegram WooCommerce Order support",
  "tags": [],
  "nodes": [
    {
      "id": "2835d72f-10eb-4b94-8e56-90413c74e89a",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        1072,
        1312
      ],
      "parameters": {},
      "typeVersion": 3.2
    },
    {
      "id": "653d7db3-92d9-49e0-8f9e-a914bfb0754c",
      "name": "Get an order in WooCommerce",
      "type": "n8n-nodes-base.wooCommerceTool",
      "position": [
        2224,
        1616
      ],
      "parameters": {
        "orderId": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Order_ID', ``, 'string') }}",
        "resource": "order",
        "operation": "get"
      },
      "credentials": {
        "wooCommerceApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "e5582697-96c2-41e1-b535-c1792bcfe362",
      "name": "Fetch user query",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        672,
        1024
      ],
      "parameters": {
        "updates": [
          "message",
          "*"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "b43894b4-bbc2-42f5-8be2-31283c2623c1",
      "name": "Fetch support mail",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        672,
        1472
      ],
      "parameters": {
        "simple": false,
        "filters": {
          "readStatus": "unread"
        },
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "601f74d3-b435-4612-9ce5-eb7249f10d1f",
      "name": "Primary Model [Gemini 2.5 Flash]",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        1504,
        1600
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "ae55ba67-1a8e-4415-996d-1431529ffb25",
      "name": "Fallback Model [GPT 4.1 mini]",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1648,
        1600
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "a1ad0dde-d2e9-48c6-bc27-a9cfa046f0b7",
      "name": "Conversation Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        1936,
        1600
      ],
      "parameters": {
        "sessionKey": "={{ $('Set Manual Fields').item.json.conversation_id }}",
        "sessionIdType": "customKey"
      },
      "typeVersion": 1.3
    },
    {
      "id": "52e1e028-60aa-4f26-9e01-fed0c3637266",
      "name": "Set Manual Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        1456,
        1312
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "5595792e-a575-4e1c-9bbd-5b398f79613d",
              "name": "platform",
              "type": "string",
              "value": "={{ $json.message ? 'telegram' : 'gmail' }}"
            },
            {
              "id": "acab6a28-4cc4-430d-8f1d-32bb46e6ddae",
              "name": "sender_id",
              "type": "string",
              "value": "={{ $json.message ? $json.message.chat.id : $json.from.value[0].address }}"
            },
            {
              "id": "a2215038-320e-4130-a2e7-6e220c2ba614",
              "name": "query_text",
              "type": "string",
              "value": "={{ $json.message ? $json.message.text : ($json.text || $json.snippet) }}"
            },
            {
              "id": "155368c4-3720-45c3-88a8-32afe2d7cedd",
              "name": "reply_to_id",
              "type": "string",
              "value": "={{ $json.id }}"
            },
            {
              "id": "7fbf6c01-9f2b-4326-8307-db17688f4cf2",
              "name": "conversation_id",
              "type": "string",
              "value": "={{ $json.message ? $json.message.chat.id : $json.threadId }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "873e594c-9a80-43fd-bb5b-697945fca750",
      "name": "Reply to Specific Platform",
      "type": "n8n-nodes-base.switch",
      "position": [
        2320,
        1312
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Telegram",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "5dac6afb-45bd-47a8-9a4b-d1bcbbe25f02",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $('Set Manual Fields').item.json.platform }}",
                    "rightValue": "=telegram"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Mail",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "2d5cbefc-3d7b-4015-89e4-1c612b5c1791",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $('Set Manual Fields').item.json.platform }}",
                    "rightValue": "gmail"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "21780180-84e5-4b4c-8ed0-2fa5056ba4c9",
      "name": "Send Telegram Response",
      "type": "n8n-nodes-base.telegram",
      "position": [
        2672,
        1120
      ],
      "parameters": {
        "text": "={{ $json.output }}",
        "chatId": "={{ $('Set Manual Fields').item.json.sender_id }}",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "33a5bc09-4397-4960-8888-c955739052ed",
      "name": "Send Response via Mail",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2672,
        1424
      ],
      "parameters": {
        "message": "={{ $json.output }}",
        "options": {},
        "emailType": "text",
        "messageId": "={{ $('Set Manual Fields').item.json.reply_to_id }}",
        "operation": "reply"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "a050a96b-7439-49ad-b98d-237cb952faf8",
      "name": "Mark received mail as read",
      "type": "n8n-nodes-base.gmail",
      "position": [
        3040,
        1424
      ],
      "parameters": {
        "messageId": "={{ $('Set Manual Fields').item.json.reply_to_id }}",
        "operation": "markAsRead"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "8c082d22-44b4-44e0-8847-b588985cd8b8",
      "name": "WooCommerce Customer support Agent1",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1840,
        1312
      ],
      "parameters": {
        "text": "=You are a specialized customer support assistant for a WooCommerce store communicating via {{ $json.platform }}. Your one and only capability is to retrieve information about a customer's order.\n\nYou have access to a single tool: `Get an order in WooCommerce`. This tool requires an Order ID to work.\n\nFollow these rules strictly:\n\n1.  If the user asks about their order and provides a number that looks like an Order ID, use the `Get an order in WooCommerce` tool with that ID to find their order details.\n2.  If the user asks about their order status (e.g., \"Where is my package?\") but does NOT provide an Order ID, you MUST ask them for it. Do not attempt to answer without the ID. Simply respond with: \"I can help with that. Could you please provide your Order ID?\"\n3.  If the user asks any other question that is not about finding a specific order (e.g., \"What products do you have?\", \"What is your return policy?\", or general small talk), you must politely decline and state your purpose. Respond with: \"I can only assist with inquiries about existing orders. If you have an order you'd like to check on, please provide the Order ID.\"\n\n\n**Users Query:** {{ $json.query_text }}",
        "options": {},
        "promptType": "define",
        "needsFallback": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "3e44e6e8-dc34-4b26-9ee5-1c3cc82974c3",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        544,
        832
      ],
      "parameters": {
        "width": 368,
        "height": 400,
        "content": "## \u2708\ufe0f Fetch user query\n\n* Acts as the real-time entry point for Telegram.\n* Uses a webhook for instant triggering.\n* Captures user message and chat ID.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "2817c251-4b9d-4a30-9279-d6a9395ec4e1",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        544,
        1248
      ],
      "parameters": {
        "color": 7,
        "width": 368,
        "height": 400,
        "content": "## \ud83d\udce5  Fetch support mail\n\n* Polls the Gmail inbox every minute.\n* Filters for `UNREAD` emails only.\n* \"Get Message\" option is ON to fetch the full body and headers.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "be7c848b-7187-453c-b3f3-1d3594c06b14",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        928,
        1024
      ],
      "parameters": {
        "color": 2,
        "width": 368,
        "height": 464,
        "content": "## \ud83d\udd00 Merge\n\n* Unifies inputs from all triggers (Telegram & Gmail).\n* Creates a single data stream for processing.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "cb002988-3e3d-4168-b21c-c81d7d87937b",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1312,
        1024
      ],
      "parameters": {
        "color": 6,
        "width": 384,
        "height": 464,
        "content": "## \ud83c\udff7\ufe0f Set Manual Fields\n\n* Normalizes data from all platforms into a standard format.\n* Creates universal fields like `platform`, `sender_id`, and `query_text`.\n* **CRUCIAL:** Makes the rest of the workflow channel-agnostic."
      },
      "typeVersion": 1
    },
    {
      "id": "f7526f40-942d-4697-89d1-c66873a36173",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1712,
        1024
      ],
      "parameters": {
        "color": 3,
        "width": 480,
        "height": 464,
        "content": "## \ud83e\udde0 WooCommerce Customer support Agent\n\n- **Function:** Acts as the AI \"brain\" to understand user intent, use tools, and formulate replies.\n- **Input:** The normalized `query_text`, conversation history from Memory, and available Tools.\n- **Output:** A JSON object containing the final `output` text to be sent to the user.\n- **Note:** This node orchestrates the entire conversation and logic flow."
      },
      "typeVersion": 1
    },
    {
      "id": "e712bf53-d3d3-4dca-8a84-3b35b66ffb28",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1424,
        1504
      ],
      "parameters": {
        "color": 5,
        "width": 400,
        "height": 400,
        "content": "## \ud83d\ude80 AI Language Engines\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n* Provide the language processing power for the AI Agent.\n* Uses Primary (Gemini) first, then Fallback (GPT) on error.\n* Ensures high reliability for the bot.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "d1e67a2c-07db-409f-88da-31f2c8fa29e6",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1840,
        1504
      ],
      "parameters": {
        "color": 4,
        "width": 272,
        "height": 400,
        "content": "## \ud83d\udcdc Conversation Memory\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n* Provides short-term memory to the AI Agent.\n* Uses `conversation_id` to track individual conversations.\n* Essential for handling follow-up questions."
      },
      "typeVersion": 1
    },
    {
      "id": "889b0dbc-f2eb-4a01-96f6-1ace353ed96a",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2128,
        1504
      ],
      "parameters": {
        "width": 272,
        "height": 400,
        "content": "## \ud83d\uded2 Get an order from WooCommerce\n\n\n\n\n\n\n\n\n\n\n\n\n\n* A specialized \"Tool\" available to the AI Agent.\n* Connects to WooCommerce to fetch order details.\n* Only runs when called by the agent with a valid Order ID.\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "cda68193-1a66-4fb6-81a8-5aaa00e65056",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2208,
        1024
      ],
      "parameters": {
        "color": 7,
        "width": 320,
        "height": 464,
        "content": "## \ud83d\udcee Reply to Specific Platform\n\n* Routes the AI's final response to the correct channel.\n* Checks the `platform` field ('telegram' or 'gmail').\n* Ensures replies are sent back to the source platform.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "a2c815cc-71bf-4253-ac53-1d0e219fbb07",
      "name": "Sticky Note9",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2544,
        944
      ],
      "parameters": {
        "color": 2,
        "width": 352,
        "height": 352,
        "content": "## \ud83d\udde3\ufe0f Send Telegram Response\n\n* Final action for the Telegram path.\n* Sends the AI's response to the user's chat ID.\n\n\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "0c96b670-31c1-44bc-8b9b-b3aa9ce51f8e",
      "name": "Sticky Note10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2544,
        1312
      ],
      "parameters": {
        "color": 6,
        "width": 352,
        "height": 400,
        "content": "## \ud83d\udce8 Send Response via Mail\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n* Final action for the Gmail path.\n* Sends the AI's response as a threaded email reply.\n* Uses the `reply_to_id` to maintain the conversation thread."
      },
      "typeVersion": 1
    },
    {
      "id": "dd7376e7-724b-40af-8fe4-8abec4c711c7",
      "name": "Sticky Note11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2912,
        1312
      ],
      "parameters": {
        "color": 4,
        "width": 336,
        "height": 400,
        "content": "## \ud83d\udeab Mark received mail as read\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n* Vital control node for the Gmail trigger.\n* Marks the original incoming email as 'Read' after processing.\n* CRITICAL: Prevents infinite reply loops."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "938f9a4c-e86d-4219-aa44-8118b0bdb6ec",
  "connections": {
    "Merge": {
      "main": [
        [
          {
            "node": "Set Manual Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch user query": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Manual Fields": {
      "main": [
        [
          {
            "node": "WooCommerce Customer support Agent1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch support mail": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Conversation Memory": {
      "ai_memory": [
        [
          {
            "node": "WooCommerce Customer support Agent1",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Send Response via Mail": {
      "main": [
        [
          {
            "node": "Mark received mail as read",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reply to Specific Platform": {
      "main": [
        [
          {
            "node": "Send Telegram Response",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Response via Mail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get an order in WooCommerce": {
      "ai_tool": [
        [
          {
            "node": "WooCommerce Customer support Agent1",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Fallback Model [GPT 4.1 mini]": {
      "ai_languageModel": [
        [
          {
            "node": "WooCommerce Customer support Agent1",
            "type": "ai_languageModel",
            "index": 1
          }
        ]
      ]
    },
    "Primary Model [Gemini 2.5 Flash]": {
      "ai_languageModel": [
        [
          {
            "node": "WooCommerce Customer support Agent1",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "WooCommerce Customer support Agent1": {
      "main": [
        [
          {
            "node": "Reply to Specific Platform",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}