{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "59169fc1-d87e-4082-8edc-9e63a8ef63ee",
      "name": "Send a message",
      "type": "n8n-nodes-base.slack",
      "position": [
        2480,
        240
      ],
      "parameters": {
        "text": "={{ $json.output }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "id",
          "value": "YOUR_SLACK_CHANNEL_ID"
        },
        "otherOptions": {}
      },
      "typeVersion": 2.4
    },
    {
      "id": "7e3c4d6d-583f-46e3-8310-c34e137b24ce",
      "name": "Code in JavaScript",
      "type": "n8n-nodes-base.code",
      "position": [
        800,
        256
      ],
      "parameters": {
        "jsCode": "for (const item of $input.all()) {\n  // Regex matches any content wrapped in < > (Slack's ID format)\n  const dynamicMentionRegex = /<[^>]+>/g;\n\n  if (item.json.text) {\n    item.json.text = item.json.text.replace(dynamicMentionRegex, '').trim();\n  }\n}\n\nreturn $input.all();"
      },
      "typeVersion": 2
    },
    {
      "id": "44e8cff2-4812-483c-9b50-a0461026dd2e",
      "name": "Get many companies",
      "type": "n8n-nodes-base.hubspot",
      "position": [
        1328,
        256
      ],
      "parameters": {
        "options": {},
        "resource": "company",
        "operation": "getAll",
        "returnAll": true,
        "authentication": "appToken"
      },
      "typeVersion": 2.2
    },
    {
      "id": "b725517c-ca68-4257-b1e1-7631909a7dcc",
      "name": "Slack Trigger",
      "type": "n8n-nodes-base.slackTrigger",
      "position": [
        560,
        256
      ],
      "parameters": {
        "options": {},
        "trigger": [
          "app_mention"
        ],
        "watchWorkspace": true
      },
      "typeVersion": 1
    },
    {
      "id": "e9477df4-659b-496d-99ed-cd1128dde8c8",
      "name": "Get Contacts",
      "type": "n8n-nodes-base.hubspot",
      "position": [
        1328,
        432
      ],
      "parameters": {
        "operation": "getAll",
        "returnAll": true,
        "authentication": "appToken",
        "additionalFields": {}
      },
      "typeVersion": 2.2
    },
    {
      "id": "5428715e-0835-4cb8-90de-488dc9a651f8",
      "name": "Filter Contacts",
      "type": "n8n-nodes-base.filter",
      "onError": "continueRegularOutput",
      "position": [
        1648,
        448
      ],
      "parameters": {
        "options": {
          "ignoreCase": true
        },
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": false,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "d284ecab-a302-4a33-85dc-23a9c86f5382",
              "operator": {
                "type": "string",
                "operation": "contains"
              },
              "leftValue": "={{ $json.properties.firstname.value }}",
              "rightValue": "={{ $('Code in JavaScript').item.json.text }}"
            }
          ]
        }
      },
      "executeOnce": false,
      "notesInFlow": true,
      "typeVersion": 2.3
    },
    {
      "id": "161957eb-8f31-40a4-9d40-93234d010289",
      "name": "Filter Companies",
      "type": "n8n-nodes-base.filter",
      "onError": "continueRegularOutput",
      "position": [
        1648,
        256
      ],
      "parameters": {
        "options": {
          "ignoreCase": true
        },
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": false,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "d284ecab-a302-4a33-85dc-23a9c86f5382",
              "operator": {
                "type": "number",
                "operation": "equals"
              },
              "leftValue": "={{ $json.companyId }}",
              "rightValue": "={{ $('Code in JavaScript').item.json.text }}"
            }
          ]
        },
        "looseTypeValidation": "={{ false }}"
      },
      "typeVersion": 2.3
    },
    {
      "id": "e292c455-3a27-4d98-918b-114dbcbe091b",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        1904,
        224
      ],
      "parameters": {
        "mode": "combine",
        "options": {
          "includeUnpaired": true
        },
        "combineBy": "combineByPosition",
        "numberInputs": 3
      },
      "typeVersion": 3.2
    },
    {
      "id": "aad5ecec-7ce5-448e-aff1-fe5d8fab6ac3",
      "name": "Filter Deals",
      "type": "n8n-nodes-base.filter",
      "onError": "continueRegularOutput",
      "position": [
        1648,
        80
      ],
      "parameters": {
        "options": {
          "ignoreCase": true
        },
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": false,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "d284ecab-a302-4a33-85dc-23a9c86f5382",
              "operator": {
                "type": "string",
                "operation": "contains"
              },
              "leftValue": "={{ $json.properties.dealname }}",
              "rightValue": "={{ $('Code in JavaScript').item.json.text }}"
            }
          ]
        }
      },
      "typeVersion": 2.3,
      "alwaysOutputData": false
    },
    {
      "id": "8343c864-aa85-4f81-a373-a45364fa7416",
      "name": "Get Deals",
      "type": "n8n-nodes-base.hubspot",
      "position": [
        1328,
        96
      ],
      "parameters": {
        "resource": "deal",
        "operation": "search",
        "authentication": "appToken",
        "additionalFields": {}
      },
      "typeVersion": 2.2,
      "alwaysOutputData": false
    },
    {
      "id": "f2e1623c-e61c-4372-9575-6f7b6cd5fcee",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2048,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 624,
        "height": 624,
        "content": "## Step 3: AI Response\nGenerates formatted output and sends reply back to Slack."
      },
      "typeVersion": 1
    },
    {
      "id": "3da6b59f-19b5-4dcb-82a8-f553ebab0dda",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1040,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 992,
        "height": 624,
        "content": "## Step 2: CRM Retrieval\nFetches and filters HubSpot deals, companies, and contacts."
      },
      "typeVersion": 1
    },
    {
      "id": "abd050a5-e899-4c9a-846c-af6a190c0a8e",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        528,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 496,
        "height": 624,
        "content": "## Step 1: Trigger & Clean\nCaptures Slack mentions and removes IDs to extract clean user queries."
      },
      "typeVersion": 1
    },
    {
      "id": "1e2bf892-4b7c-450b-8013-a98efbe3b12a",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "width": 512,
        "height": 624,
        "content": "### How it works\nThis workflow creates a Slack-based CRM assistant that lets users query HubSpot data using natural language. When a user mentions the bot in Slack, the workflow captures the message and removes Slack-specific formatting (like mentions and IDs) using a JavaScript node.\n\nIt then retrieves data from HubSpot across Deals, Companies, and Contacts. Each dataset is filtered based on the cleaned query using flexible matching logic. The results are merged and passed to an AI Agent, which formats a clean, structured summary.\n\nFinally, the formatted response is sent back to Slack as a readable message.\n\n### Setup steps\n1. Connect Slack credentials (Bot Token with app_mention and chat:write scopes).\n2. Invite the bot to your Slack channel using /invite @BotName.\n3. Connect HubSpot App Token credentials.\n4. Configure Google Gemini API credentials.\n5. Test with a Slack mention like: @Bot deal name.\n\n### Customization tips\n- Adjust filters to improve matching accuracy.\n- Modify AI prompt for different output formats.\n- Add additional HubSpot fields if needed."
      },
      "typeVersion": 1
    },
    {
      "id": "f91e0ba4-99a2-4bda-9d20-1145d45de1e6",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        2176,
        416
      ],
      "parameters": {
        "options": {
          "temperature": 0.1
        }
      },
      "typeVersion": 1
    },
    {
      "id": "4ec9f98d-6f2c-43d3-bd98-a6ab7cdeaa95",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        2144,
        240
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    }
  ],
  "connections": {
    "Merge": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Send a message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Deals": {
      "main": [
        [
          {
            "node": "Filter Deals",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Deals": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Contacts": {
      "main": [
        [
          {
            "node": "Filter Contacts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Slack Trigger": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Contacts": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 2
          }
        ]
      ]
    },
    "Filter Companies": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Get many companies",
            "type": "main",
            "index": 0
          },
          {
            "node": "Get Contacts",
            "type": "main",
            "index": 0
          },
          {
            "node": "Get Deals",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many companies": {
      "main": [
        [
          {
            "node": "Filter Companies",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  }
}