{
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ]
      },
      "id": "1",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1,
      "position": [
        200,
        300
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "const text = $json.message.text.toLowerCase();\nlet type = 'general';\n\nif(text.includes('finance')||text.includes('argent')) type='finance';\nif(text.includes('plan')) type='plan';\nif(text.includes('idee')) type='business';\nif(text.includes('client')||text.includes('maps')) type='leads';\nif(text.includes('site')) type='site';\n\nreturn [{json:{text,type,chatId:$json.message.chat.id}}];"
      },
      "id": "2",
      "name": "Router",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://api.deepseek.com/v1/chat/completions",
        "method": "POST",
        "jsonParameters": true,
        "headers": {
          "Authorization": "Bearer YOUR_API_KEY",
          "Content-Type": "application/json"
        },
        "bodyParametersJson": "={\"model\":\"deepseek-chat\",\"messages\":[{\"role\":\"system\",\"content\":\"Tu es un agent business IA. Tu fais finance, planning, prospection, sites web et analyse.\"},{\"role\":\"user\",\"content\":\"TYPE: {{$json.type}}\\nDEMANDE: {{$json.text}}\"}]}"
      },
      "id": "3",
      "name": "AI Core",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        700,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "let output = $json.choices?.[0]?.message?.content || JSON.stringify($json);\nreturn [{json:{output,chatId:$node[\"Router\"].json.chatId}}];"
      },
      "id": "4",
      "name": "Format AI",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        950,
        300
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "sheetId": "YOUR_SHEET_ID",
        "range": "CRM!A:D",
        "options": {},
        "data": "={{[$json.chatId, $json.output, new Date(), $node[\"Router\"].json.type]}}"
      },
      "id": "5",
      "name": "Memory CRM Save",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 1,
      "position": [
        950,
        500
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{$json.chatId}}",
        "text": "\ud83e\udd16 AGENT BUSINESS IA :\\n\\n{{$json.output}}"
      },
      "id": "6",
      "name": "Send Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        1200,
        300
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Router",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Router": {
      "main": [
        [
          {
            "node": "AI Core",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Core": {
      "main": [
        [
          {
            "node": "Format AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format AI": {
      "main": [
        [
          {
            "node": "Memory CRM Save",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}