{
  "name": "instagram-facebook-dm-autoresponder.json",
  "nodes": [
    {
      "parameters": {
        "promptType": "define",
        "text": "=respond to user's message \n\nuser name: {{ $json.sender.name }}\nuser message: {{ $json.content }}\n\n",
        "options": {
          "systemMessage": "You are a helpful assistant at X company\n\nYOUR_KNOWLEDGE_BASE_HERE \n\nuse the transfer_to_supervisor tool, if the user explicitly asks to speak to a human supervisor. ",
          "returnIntermediateSteps": true
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        2592,
        608
      ],
      "id": "dee22e51-1e38-49be-9961-434212060d39",
      "name": "AI Agent",
      "retryOnFail": true
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4.1",
          "mode": "list",
          "cachedResultName": "gpt-4.1"
        },
        "builtInTools": {},
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        2528,
        848
      ],
      "id": "4713d843-a17a-45d1-80c0-8f91bbf7f04f",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "e85c0635-d483-457a-8b61-af9720a9f373",
              "leftValue": "={{ $json.payload[0].labels.join(',') }}",
              "rightValue": "bot_paused",
              "operator": {
                "type": "string",
                "operation": "contains"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        1536,
        416
      ],
      "id": "56e22e2e-3e35-43de-86b7-a5a3966d032e",
      "name": "Check if Bot Paused"
    },
    {
      "parameters": {
        "content": "# Sticky Notes color code: \n\n## Yellow notes: documentation\n## Blue notes: Chatwoot related\n## Green notes: Zernio related\n\n*******************************\n\n# Pre-requesites\n## **1- Zernio API**\n## **2- Chatwoot API**",
        "height": 608,
        "width": 416
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -496,
        320
      ],
      "id": "a6256ab2-6b4f-49c3-ad7e-1d21b53c7835",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "description": "Call this tool IMMEDIATELY when the user asks for a human, supervisor, or customer service agent, manager, OR if they express intent to make a deal, collaborate, pay money, or ask about payment methods.",
        "jsCode": "return \"Transfer to supervisor initiated. Let the user know a supervisor will join shortly.\";"
      },
      "type": "@n8n/n8n-nodes-langchain.toolCode",
      "typeVersion": 1.3,
      "position": [
        2848,
        848
      ],
      "id": "589c79c6-19fd-49f5-a5a3-007b40d5cc84",
      "name": "transfer_to_supervisor"
    },
    {
      "parameters": {
        "content": "# **Setting up pre-requisites:**\n# **Zernio:**\n ### 1- Sign up for free at [Zernio](https://zernio.com/)\n### 2- Copy your API somewhere safe \n### 3- Use the API to set up the Bearer Auth for http request nodes hitting zernio API\n\n\n***********************\n# **Chatwoot:**\n### 1- Sign into your chatwoot account (cloud or selfhoted)\n### 2- Copy your Access Token from your user's profile settings \n### 3- Use the Access Token to set up the Header Auth for http request nodes hitting Chatwoot API",
        "height": 704,
        "width": 416
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -512,
        992
      ],
      "id": "556266f6-22ab-4ba0-aba7-cfcacb550a33",
      "name": "Sticky Note1"
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('extractedInfo').item.json.conversation_id }}"
      },
      "type": "@n8n/n8n-nodes-langchain.memoryPostgresChat",
      "typeVersion": 1.4,
      "position": [
        2688,
        848
      ],
      "id": "98a40ff3-3021-4841-be1f-a5828855b617",
      "name": "Postgres Chat Memory",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "content": "## Receiving messages from Zernio and Extracting necessary information",
        "height": 576,
        "width": 496,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        416
      ],
      "id": "385ad017-d233-4706-a8d1-7d3e2bad1892",
      "name": "Sticky Note2"
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "from-zernio",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        64,
        640
      ],
      "id": "97bdcbda-30e0-40a3-9d78-aa3d3fa94f3e",
      "name": "messageReceivedFromZernio"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "a17777f1-cb63-4cb3-89c5-b78189a95fde",
              "name": "user_name",
              "value": "={{ $('messageReceivedFromZernio').item.json.body.message.sender.name || $('messageReceivedFromZernio').item.json.body.message.sender.username}}",
              "type": "string"
            },
            {
              "id": "4720a382-3d72-473d-b438-d24112145845",
              "name": "user_message",
              "value": "={{ $('messageReceivedFromZernio').item.json.body.message.text }}",
              "type": "string"
            },
            {
              "id": "98a3b211-b42b-4daa-b6a3-4a0225cbda3b",
              "name": "sender_id",
              "value": "={{ $('messageReceivedFromZernio').item.json.body.message.sender.id }}",
              "type": "string"
            },
            {
              "id": "84b37e3d-9609-40c5-b810-6af86333d4e6",
              "name": "conversation_id",
              "value": "={{ $('messageReceivedFromZernio').item.json.body.conversation.id }}",
              "type": "string"
            },
            {
              "id": "9c32185c-61f0-485f-893c-0097d17ebc69",
              "name": "account_id",
              "value": "={{ $('messageReceivedFromZernio').item.json.body.account.id }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        320,
        640
      ],
      "id": "6a134d25-18b0-47f1-ba73-14eef61fb7bb",
      "name": "extractedInfo"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "c8091858-b243-4274-8a3d-9991303a4d05",
              "leftValue": "={{ $('searchChatwootContact').item.json.payload.length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "notEquals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        880,
        640
      ],
      "id": "36663916-45ef-4c8d-a699-92bb616fe275",
      "name": "contactExistInChatwoot?"
    },
    {
      "parameters": {
        "url": "https://YOUR_CHATWOOT_DOMAIN_HERE/api/v1/accounts/YOUR_ACCOUNT_NUMBER_HERE/contacts/search",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "q",
              "value": "={{ $('extractedInfo').item.json.user_name }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        656,
        640
      ],
      "id": "5db33f2e-8ab1-4f24-b619-36252457cafd",
      "name": "searchChatwootContact",
      "retryOnFail": true,
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://YOUR_CHATWOOT_DOMAIN_HERE/api/v1/accounts/YOUR_ACCOUNT_NUMBER_HERE/contacts",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"name\": \"{{ $('extractedInfo').item.json.user_name }}\",\n  \"inbox_id\": YOUR_INBOX_ID_HERE\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        1152,
        784
      ],
      "id": "9a4d664c-71c9-4631-b9ef-7b8b47f73b7f",
      "name": "createChatwootContact",
      "retryOnFail": true,
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://YOUR_CHATWOOT_DOMAIN_HERE/api/v1/accounts/YOUR_ACCOUNT_NUMBER_HERE/conversations",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"source_id\": \"{{ $json.payload.contact_inbox.source_id }}\",\n  \"inbox_id\": YOUR_INBOX_ID_HERE,\n  \"contact_id\": {{ $json.payload.contact.id }},\n  \"status\": \"open\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        1536,
        784
      ],
      "id": "bd2ed6c2-a5bb-4ef7-86e5-e0fd8a891279",
      "name": "createChatwootConversation",
      "retryOnFail": true,
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "content": "## 1- Searching for existing contact and conversation in Chatwoot \n## 2- If none exists then  creating a new contact and a conversation in Chatwoot\n## 3- Logging inbound message so it appears on Chatwoot inbox",
        "height": 816,
        "width": 1840,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        576,
        256
      ],
      "id": "0d3a4df2-512c-48d3-86ae-3bf73cfc4ea1",
      "name": "Sticky Note3"
    },
    {
      "parameters": {
        "content": "",
        "height": 224,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        3056,
        288
      ],
      "id": "d82928e0-7bf5-4a0e-989d-d93094cfbe85",
      "name": "Sticky Note4"
    },
    {
      "parameters": {
        "content": "",
        "height": 240,
        "width": 928,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        3040,
        544
      ],
      "id": "47d252fb-094b-495c-a612-8a3c5b9e51bd",
      "name": "Sticky Note5"
    },
    {
      "parameters": {
        "content": "",
        "height": 240,
        "width": 224,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        16,
        1104
      ],
      "id": "146c9f50-26d5-4ea0-a9dc-f3c1e517fe1e",
      "name": "Sticky Note6"
    },
    {
      "parameters": {
        "content": "",
        "height": 240,
        "width": 208,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        928,
        1104
      ],
      "id": "c4687949-52bc-4e74-92dc-7ad843d7d8da",
      "name": "Sticky Note7"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "e11ff378-8473-47bf-97af-232fe2d61894",
              "leftValue": "={{ $('searchContactConversations').item.json.payload.length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "notEquals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        1328,
        512
      ],
      "id": "58ea6f7f-c82c-4233-862b-6f1463d4579f",
      "name": "conversationExists?"
    },
    {
      "parameters": {
        "url": "=https://YOUR_CHATWOOT_DOMAIN_HERE/api/v1/accounts/YOUR_ACCOUNT_NUMBER_HERE/contacts/{{ $('contactExistInChatwoot?').item.json.payload[0].id }}/conversations",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        1104,
        512
      ],
      "id": "2ba741dd-d867-4119-b047-c263136850f4",
      "name": "searchContactConversations",
      "retryOnFail": true,
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://YOUR_CHATWOOT_DOMAIN_HERE/api/v1/accounts/YOUR_ACCOUNT_NUMBER_HERE/conversations",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"source_id\": \"{{ $('extractedInfo').item.json.conversation_id }}\",\n  \"inbox_id\": YOUR_INBOX_ID_HERE,\n  \"contact_id\": {{ $('searchChatwootContact').item.json.payload[0].id }},\n  \"status\": \"open\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        1536,
        608
      ],
      "id": "1d9bdbf4-54e3-4eae-9111-1edbaab3acb6",
      "name": "createChatwootConversation1",
      "retryOnFail": true,
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "04a79ba4-3bda-45f6-bc1e-41375599703e",
              "name": "chatwoot_conversation_id",
              "value": "={{ $json.id || $json.payload?.[0]?.id }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1808,
        608
      ],
      "id": "58b138f8-f615-46f8-8a5c-22c7bac61245",
      "name": "getConversationId"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://YOUR_CHATWOOT_DOMAIN_HERE/api/v1/accounts/YOUR_ACCOUNT_NUMBER_HERE/conversations/{{ $('getConversationId').item.json.chatwoot_conversation_id }}/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "content",
              "value": "={{ $('extractedInfo').item.json.user_message }}"
            },
            {
              "name": "message_type",
              "value": "incoming"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        2160,
        608
      ],
      "id": "84c458f1-eebb-4a9c-8ec9-11e43ebe8193",
      "name": "logInboundMessageToChatwoot",
      "retryOnFail": true,
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1d3ae884-d8ef-4aa8-a1e0-608c373662c0",
              "name": "chatwoot_conversation_id",
              "value": "={{ $json.payload[0].id }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1792,
        336
      ],
      "id": "64af937f-22d0-416d-9db4-8f40c0f2cffa",
      "name": "getConversationId1"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://YOUR_CHATWOOT_DOMAIN_HERE/api/v1/accounts/YOUR_ACCOUNT_NUMBER_HERE/conversations/{{ $json.chatwoot_conversation_id }}/custom_attributes",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"custom_attributes\": {\n    \"zernio_account_id\": \"{{ $('messageReceivedFromZernio').item.json.body.account.id }}\", \n   \"zernio_conversation_id\": \"{{ $('messageReceivedFromZernio').item.json.body.message.conversationId }}\"\n  }\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        2016,
        336
      ],
      "id": "70dd8094-aca0-4100-aaa6-6c98df4ba156",
      "name": "setCustomChatwootAttributes",
      "retryOnFail": true,
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://YOUR_CHATWOOT_DOMAIN_HERE/api/v1/accounts/YOUR_ACCOUNT_NUMBER_HERE/conversations/{{ $('getConversationId1').item.json.chatwoot_conversation_id }}/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"content\": \"{{ $('extractedInfo').item.json.user_message }}\",\n  \"message_type\": \"incoming\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        2224,
        336
      ],
      "id": "34c4b372-8e14-4db4-b46b-fae2635a05e9",
      "name": "logInboundMessageToChatwoot1",
      "retryOnFail": true,
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://zernio.com/api/v1/inbox/conversations/{{ $('messageReceivedFromZernio').item.json.body.message.conversationId }}/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "accountId",
              "value": "={{ $('extractedInfo').item.json.account_id }}"
            },
            {
              "name": "message",
              "value": "={{ $json.output }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        3120,
        368
      ],
      "id": "80f2e41d-799a-4f70-ac7f-6efa7034814f",
      "name": "sendReplyToUser",
      "retryOnFail": true,
      "credentials": {
        "httpBearerAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://YOUR_CHATWOOT_DOMAIN_HERE/api/v1/accounts/YOUR_ACCOUNT_NUMBER_HERE/conversations/{{ $('getConversationId').item.json.chatwoot_conversation_id }}/custom_attributes",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"custom_attributes\": {\n    \"zernio_account_id\": \"{{ $('messageReceivedFromZernio').item.json.body.account.id }}\", \n   \"zernio_conversation_id\": \"{{ $('messageReceivedFromZernio').item.json.body.message.conversationId }}\"\n  }\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        3136,
        608
      ],
      "id": "634258d7-35f6-4202-b512-fe2cd47ddf32",
      "name": "setChatwootCustomAttributes",
      "retryOnFail": true,
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://YOUR_CHATWOOT_DOMAIN_HERE/api/v1/accounts/YOUR_ACCOUNT_NUMBER_HERE/conversations/{{ $('getConversationId').item.json.chatwoot_conversation_id }}/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"content\": {{ JSON.stringify($('AI Agent').item.json.output.replace(/<\\/?[^>]+(>|$)/g, \"\")) }},\n  \"message_type\": \"outgoing\",\n  \"private\": false,\n  \"content_type\": \"text\",\n  \"submitted_at\": \"{{ new Date().toISOString() }}\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        3376,
        608
      ],
      "id": "4fbaaed1-3730-46bd-9cd9-b0cf18f37946",
      "name": "LogAIOutboundMessageToChatwoot",
      "retryOnFail": true,
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "85c19e8a-550f-4500-8fae-9e6126cc42d3",
              "leftValue": "={{ $('AI Agent').item.json.intermediateSteps?.some(step => step.action?.tool === 'transfer_to_supervisor') ?? false }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        3616,
        608
      ],
      "id": "5b89c5d4-56a7-4472-ad45-c3880ee1b19a",
      "name": "supervisorToolCalled?"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://YOUR_CHATWOOT_DOMAIN_HERE/api/v1/accounts/YOUR_ACCOUNT_NUMBER_HERE/conversations/{{ $('getConversationId').item.json.chatwoot_conversation_id }}/labels",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"labels\": [\"bot_paused\"]\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        3824,
        592
      ],
      "id": "e38bca67-3265-440e-8ba0-2aef58c0881f",
      "name": "setBotPausedLabel",
      "retryOnFail": true,
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "supervisor-reply-from-chatwoot",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        80,
        1168
      ],
      "id": "c1cd7552-89db-45c5-a573-22b658cd7b6f",
      "name": "supervisorReplyFromChatwoot"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "d70ed39b-ae03-4e63-920d-d38f82f5a255",
              "name": "supervisor_reply",
              "value": "={{ $json.body.messages[0].content.replace(/<|>/g, '') }}",
              "type": "string"
            },
            {
              "id": "57974fd0-c24c-4291-b10d-b4d5d754d586",
              "name": "label",
              "value": "={{ $json.body.labels[0] }}",
              "type": "string"
            },
            {
              "id": "441bfedf-cc17-4056-9c52-d6fa64a579ae",
              "name": "zernio_account_id",
              "value": "={{ $json.body.custom_attributes.zernio_account_id }}",
              "type": "string"
            },
            {
              "id": "cfeff45b-72d6-4cb0-a6c3-41272612f410",
              "name": "zernio_conversation_id",
              "value": "={{ $json.body.custom_attributes.zernio_conversation_id }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        400,
        1168
      ],
      "id": "3ea1c1e0-95ae-44ff-961e-32b09b0127f8",
      "name": "extractingFields"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "30c89e30-a4fd-41fc-b65c-ce59e2c85403",
              "leftValue": "={{ $json.label }}",
              "rightValue": "bot_paused",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        688,
        1168
      ],
      "id": "a1f2d360-c253-42cb-ab3b-4c23830c2761",
      "name": "isBotPaused?"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://zernio.com/api/v1/inbox/conversations/{{ $('extractingFields').item.json.zernio_conversation_id }}/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "accountId",
              "value": "={{ $('extractingFields').item.json.zernio_account_id }}"
            },
            {
              "name": "message",
              "value": "={{ $('extractingFields').item.json.supervisor_reply }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        992,
        1152
      ],
      "id": "3078090d-c57d-4970-91a2-3efcc8fe3980",
      "name": "sendSupervisorReply",
      "retryOnFail": true,
      "credentials": {
        "httpBearerAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "content": "# **Set up for Zernio to send DM to this webhook:**\n ### 1- Log into your Zernio dashboard.\n### 2- Go to webhooks.\n### 3- Create new webhook and paste the production URL of the n8n webhook you want Zernio to forward messages to.\n### 4- Check the \"message.received\" box. \n### 5- Save.",
        "height": 416,
        "width": 416
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "id": "60cdd7bd-70a6-4de9-a678-64d3b12b043a",
      "name": "Sticky Note8"
    },
    {
      "parameters": {
        "content": "# **Set up for Chatwoot to send supervisor replies to this webhook:**\n ## 1- Log into your Chatwoot account.\n## 2- Go to Settings -> Automation.\n## 3- Create new automation: \n### Event: \nmessage created\n### Conditions: \nmessage type = outgoing AND inbox = your_inbox_name AND labels = bot_paused\n### Actions: \nsend webhook event = your_production_url\n\n### 4- Save.",
        "height": 656,
        "width": 416
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        16,
        1344
      ],
      "id": "33549ebc-a6bd-47c6-98df-f6d86bd00602",
      "name": "Sticky Note9"
    },
    {
      "parameters": {
        "content": "# **Why use a code tool to notify supervisor and only add the bot_paused label after the message is logged in Chatwoot?**\n ## This prevent the Chatwoot automation that forwards supervisor's replies to accidentally fire up. As the AI \"supervisor will be there with you soon\" message will satisfy all conditions.\n",
        "height": 320,
        "width": 416
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        3680,
        784
      ],
      "id": "8b58c4ba-ea3d-4dd8-92cd-968dcde9edae",
      "name": "Sticky Note10"
    },
    {
      "parameters": {
        "chatId": "SUPERVISOR_CHAT_ID_HERE",
        "text": "=\ud83d\udea8 <b>*Handoff Alert*</b> \ud83d\udea8  \nA client is requesting a human supervisor.  \n\n\ud83d\udc64 <b>Customer:</b>\n{{ $('extractedInfo').item.json.user_name }} \n\n\ud83d\udcf1 <b>Platform:</b> WhatsApp \ud83d\udd17 \n\ud83d\udda5\ufe0f <b><a href=\"https://app.chatwoot.com/app/accounts/YOUR_ACCOUNT_NUMBER_HERE/conversations/{{ $('getConversationId').item.json.chatwoot_conversation_id }}\">Open Chatwoot</a></b>\n\nPlease check the dashboard to assist.",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        4032,
        592
      ],
      "id": "b379e326-7d97-4093-895a-1cc6714921aa",
      "name": "notifySupervisor",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "content": "# **Setting up http request nodes:**\n ### 1- Replace YOUR_CHATWOOT_DOMAIN_HERE with your actual domain / subdomain of your self-hosted instance.\n### 2- Replace YOUR_ACCOUNT_NUMBER_HERE with your real account number in Chatwoot (visible In the URL).\n### 3- Replace YOUR_INBOX_ID_HERE with your actual inbox ID (visible in the URL).",
        "height": 240,
        "width": 928
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        880,
        -32
      ],
      "id": "924a4a61-9d65-41b3-bbb0-9a6d6fa6197a",
      "name": "Sticky Note11"
    },
    {
      "parameters": {
        "content": "## Replace SUPERVISOR_CHAT_ID_HERE with a real chat ID",
        "height": 128,
        "width": 416
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        4000,
        432
      ],
      "id": "53b69609-6823-49da-9fb7-b144a95217da",
      "name": "Sticky Note12"
    },
    {
      "parameters": {
        "content": "# Add another automation to enable supervisor to intervene without Agent handoff by reopening a resolved conversation: \n## Create new automation: \n### Event: \nconversation opened\n### Conditions: \n inbox = your_inbox_name\n### Actions: \nAdd label = bot_paused\n\n### Save.",
        "height": 656,
        "width": 416
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        480,
        1344
      ],
      "id": "51cba191-3ffc-4c76-9b34-53e7eb419b47",
      "name": "Sticky Note13"
    },
    {
      "parameters": {
        "content": "![banner](https://www.kawtheron.tech/github/IG-FB-DM-banner.jpg)",
        "height": 848,
        "width": 2016,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -528,
        -960
      ],
      "id": "6caef322-ffea-4364-a869-159a789691f1",
      "name": "Sticky Note14"
    }
  ],
  "connections": {
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "sendReplyToUser",
            "type": "main",
            "index": 0
          },
          {
            "node": "setChatwootCustomAttributes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if Bot Paused": {
      "main": [
        [
          {
            "node": "getConversationId1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "getConversationId",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "transfer_to_supervisor": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Postgres Chat Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "messageReceivedFromZernio": {
      "main": [
        [
          {
            "node": "extractedInfo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "extractedInfo": {
      "main": [
        [
          {
            "node": "searchChatwootContact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "contactExistInChatwoot?": {
      "main": [
        [
          {
            "node": "searchContactConversations",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "createChatwootContact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "searchChatwootContact": {
      "main": [
        [
          {
            "node": "contactExistInChatwoot?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "createChatwootContact": {
      "main": [
        [
          {
            "node": "createChatwootConversation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "createChatwootConversation": {
      "main": [
        [
          {
            "node": "getConversationId",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "conversationExists?": {
      "main": [
        [
          {
            "node": "Check if Bot Paused",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "createChatwootConversation1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "searchContactConversations": {
      "main": [
        [
          {
            "node": "conversationExists?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "createChatwootConversation1": {
      "main": [
        [
          {
            "node": "getConversationId",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "getConversationId": {
      "main": [
        [
          {
            "node": "logInboundMessageToChatwoot",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "logInboundMessageToChatwoot": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "getConversationId1": {
      "main": [
        [
          {
            "node": "setCustomChatwootAttributes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "setCustomChatwootAttributes": {
      "main": [
        [
          {
            "node": "logInboundMessageToChatwoot1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "setChatwootCustomAttributes": {
      "main": [
        [
          {
            "node": "LogAIOutboundMessageToChatwoot",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "LogAIOutboundMessageToChatwoot": {
      "main": [
        [
          {
            "node": "supervisorToolCalled?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "supervisorToolCalled?": {
      "main": [
        [
          {
            "node": "setBotPausedLabel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "supervisorReplyFromChatwoot": {
      "main": [
        [
          {
            "node": "extractingFields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "extractingFields": {
      "main": [
        [
          {
            "node": "isBotPaused?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "isBotPaused?": {
      "main": [
        [
          {
            "node": "sendSupervisorReply",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "setBotPausedLabel": {
      "main": [
        [
          {
            "node": "notifySupervisor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "7a8abd58-5515-4233-a92a-22cc5e3f7722",
  "nodeGroups": [],
  "id": "m2plgrQ2UP5EaQI0",
  "tags": []
}