{
  "nodes": [
    {
      "parameters": {
        "resource": "draft",
        "subject": "={{ $json.sujet_ia }}",
        "message": "={{ $json.accroche_ia }}",
        "options": {
          "sendTo": "={{ $json.contact_email }}"
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        736,
        1424
      ],
      "id": "54a61fb0-ac75-4fb3-9b08-b29b093020c7",
      "name": "Create a draft1",
      "webhookId": "276ebe7e-3585-4428-ba4d-1c71b995863b",
      "credentials": {
        "gmailOAuth2": {
          "id": "ARtnDsHtGUuglWcD",
          "name": "Gmail account 9"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ $json.openai_payload }}",
        "options": {}
      },
      "id": "97b2c97a-d24a-4200-a151-c66099868ab2",
      "name": "Generation IA1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -160,
        1216
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "QjWVbTtkwF9nls9p",
          "name": "Header Auth account 3"
        }
      },
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const input = $input.item.json;\n\nconst calendlyUrl = \"https://calendly.com/\"; // 👈 remplace ici\n\nconst body = {\n  model: \"gpt-4.1-mini\",\n  max_tokens: 600,\n  messages: [\n    {\n      role: \"system\",\n      content: \"Tu es un SDR senior. Tu ecris en francais, sans superlatif creux, sans tournure generique. Tu reponds UNIQUEMENT avec un JSON valide, sans texte avant ni apres : {\\\"subject\\\": string, \\\"opening\\\": string, \\\"follow_up\\\": string, \\\"url_rdv\\\": string}\"\n    },\n    {\n      role: \"user\",\n      content: [\n        \"Contexte entreprise (source SocieteInfo) :\",\n        \"- Nom : \" + (input.entreprise || \"\"),\n        \"- Secteur : \" + (input.secteur || \"\"),\n        \"- Activite declaree : \" + (input.activite_declaree || \"\"),\n        \"- Taille : \" + (input.effectif || \"\") + \" salaries\" + (input.ca ? \", CA \" + input.ca + \" euros\" : \"\"),\n        \"- Ville : \" + (input.ville || \"\"),\n        \"- LinkedIn entreprise : \" + (input.linkedin_url || \"non renseigne\"),\n        \"\",\n        \"Contact cible :\",\n        \"- Prenom : \" + (input.contact_prenom || \"\"),\n        \"- Nom : \" + (input.contact_nom || \"\"),\n        \"- Role : \" + (input.contact_role || \"\"),\n        \"- LinkedIn : \" + (input.contact_linkedin || \"non renseigne\"),\n        \"\",\n        \"Notre proposition de valeur :\",\n        (input.pitch || \"\"),\n        \"\",\n        \"Redige :\",\n        \"1. subject : objet d'email, 60 caracteres max, sans clickbait\",\n        \"2. opening : 2 phrases d'accroche qui montrent une vraie comprehension du contexte (pas de generalite) Integre naturellement ce lien de prise de rendez-vous : \" + calendlyUrl,\n        \"3. follow_up : une relance courte a J+4 si pas de reponse. Integre naturellement ce lien de prise de rendez-vous : \" + calendlyUrl\n      ].join(\"\\n\")\n    }\n  ]\n};\n\nreturn {\n  json: {\n    entreprise: input.entreprise,\n    secteur: input.secteur,\n    activite_declaree: input.activite_declaree,\n    ville: input.ville,\n    effectif: input.effectif,\n    ca: input.ca,\n    linkedin_url: input.linkedin_url,\n    Siren: input.Siren,\n    contact_prenom: input.contact_prenom,\n    contact_nom: input.contact_nom,\n    contact_role: input.contact_role,\n    contact_email: input.contact_email,\n    contact_email_test: input.contact_email_test,\n    contact_linkedin: input.contact_linkedin,\n    pitch: input.pitch,\n    url: calendlyUrl,\n    openai_payload: body\n  }\n};"
      },
      "id": "7019d139-9daf-401d-b182-ec85215e9a35",
      "name": "Preparer payload IA1",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -384,
        1216
      ]
    },
    {
      "parameters": {
        "url": "=https://societeinfo.com/app/rest/api/v2/contacts.json/{{ $json.result.organization.registration_number }}",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "key"
            }
          ]
        },
        "options": {}
      },
      "id": "c013c78a-4f7c-4c3e-ba33-2708da627c7b",
      "name": "Contacts entreprise",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -1504,
        1280
      ],
      "onError": "continueErrorOutput",
      "notes": "Forme exacte de la reponse a verifier dans api-doc/#get-contacts-by-company (on suppose ici result = tableau de contacts)."
    },
    {
      "parameters": {
        "url": "=https://societeinfo.com/app/rest/api/v2/company.json/{{ $json.registration_number }}",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "key"
            },
            {
              "name": "withContactApiCount",
              "value": "true"
            }
          ]
        },
        "options": {}
      },
      "id": "63d085c2-0c5f-4162-8dce-40da2a3cdae3",
      "name": "Fiche complete entreprise",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -1952,
        1360
      ],
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "url": "https://societeinfo.com/app/rest/api/v2/companies.json",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "key"
            },
            {
              "name": "query",
              "value": "={{ $json['Mots-cles / activite'] }}"
            },
            {
              "name": "where",
              "value": "={{ $json['Zone (code postal ou departement)'] }}"
            },
            {
              "name": "minstaff",
              "value": "={{ $json['Effectif min'] }}"
            },
            {
              "name": "maxstaff",
              "value": "={{ $json['Effectif max'] }}"
            },
            {
              "name": "minsales",
              "value": "={{ $json['CA min'] }}"
            },
            {
              "name": "maxsales",
              "value": "={{ $json['CA max'] }}"
            },
            {
              "name": "active",
              "value": "true"
            },
            {
              "name": "sort",
              "value": "score"
            },
            {
              "name": "limit",
              "value": "25"
            }
          ]
        },
        "options": {}
      },
      "id": "6ba94661-03d8-4301-8e69-9c4c2e4eb013",
      "name": "Recherche ICP",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -2624,
        1664
      ],
      "authentication": "none",
      "notesInFlow": true,
      "notes": "1 credit par requete reussie. Remplace VOTRE_CLE_API_SOCIETEINFO par ta clé trouvée dans Mon compte > Paramètre API sur societeinfo.com"
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "",
          "cachedResultUrl": ""
        }
      },
      "id": "cc0e177f-f045-4eb7-a3d7-27b36e73f061",
      "name": "Logger erreur1",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        64,
        1456
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "uGeAZ1RkwxnN1TnT",
          "name": "Google Sheets account 6"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "",
          "cachedResultUrl": ""
        }
      },
      "id": "35ed8dce-d6b7-46f2-9f5f-e738cb86e485",
      "name": "Ajout file de validation1",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        512,
        1424
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "uGeAZ1RkwxnN1TnT",
          "name": "Google Sheets account 6"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "date",
              "type": "string",
              "value": "={{ $now.toISO() }}"
            },
            {
              "id": "2",
              "name": "siren",
              "type": "string",
              "value": "={{ $json.Siren }}"
            },
            {
              "id": "3",
              "name": "entreprise",
              "type": "string",
              "value": "={{ $json.entreprise }}"
            },
            {
              "id": "4",
              "name": "secteur",
              "type": "string",
              "value": "={{ $json.secteur }}"
            },
            {
              "id": "5",
              "name": "ville",
              "type": "string",
              "value": "={{ $json.Ville }}"
            },
            {
              "id": "6",
              "name": "effectif",
              "type": "string",
              "value": "={{ $json.effectif }}"
            },
            {
              "id": "7",
              "name": "ca",
              "type": "string",
              "value": "={{ $json.ca }}"
            },
            {
              "id": "8",
              "name": "contact_prenom",
              "type": "string",
              "value": "={{ $json.contact_prenom }}"
            },
            {
              "id": "9",
              "name": "contact_role",
              "type": "string",
              "value": "={{ $json.contact_role }}"
            },
            {
              "id": "10",
              "name": "contact_email",
              "type": "string",
              "value": "={{ $json.contact_email }}"
            },
            {
              "id": "12",
              "name": "contact_linkedin",
              "type": "string",
              "value": "={{ $json.contact_linkedin }}"
            },
            {
              "id": "13",
              "name": "sujet_ia",
              "type": "string",
              "value": "={{ $json.ai_subject }}"
            },
            {
              "id": "14",
              "name": "accroche_ia",
              "type": "string",
              "value": "={{ $json.ai_opening }}"
            },
            {
              "id": "15",
              "name": "relance_ia",
              "type": "string",
              "value": "={{ $json.ai_follow_up }}"
            },
            {
              "id": "16",
              "name": "lien_fiche_societeinfo",
              "type": "string",
              "value": "=https://societeinfo.com/app/recherche/societe/{{ $json.Siren }}"
            },
            {
              "id": "17",
              "name": "statut",
              "type": "string",
              "value": "A valider"
            },
            {
              "id": "455a04d3-1a53-47b9-8b5f-410753865607",
              "name": "url_calendly",
              "value": "={{ $json.url_calendly }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "1cfc3319-c694-48b8-8e98-63fee61fd2cb",
      "name": "Ligne finale1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        288,
        1168
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const aiResponse = $input.item.json;\nconst context = $('Contexte injecte1').item.json;\n\n// OpenAI : choices[0].message.content\nconst raw = aiResponse?.choices?.[0]?.message?.content || '{}';\n\nlet parsed = {};\ntry {\n  parsed = JSON.parse(raw);\n} catch (e) {\n  parsed = { subject: '', opening: '', follow_up: '', url: '' };\n}\n\nreturn {\n  json: {\n    ...context,\n    ai_subject: parsed.subject || '',\n    ai_opening: parsed.opening || '',\n    ai_follow_up: parsed.follow_up || '',\n    url_calendly: parsed.url_rdv || ''\n  }\n};"
      },
      "id": "6f86495e-9a79-477f-a766-065cac90debc",
      "name": "Parser sortie IA1",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        64,
        1168
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "entreprise",
              "type": "string",
              "value": "={{ $json.company.name }}"
            },
            {
              "id": "2",
              "name": "secteur",
              "type": "string",
              "value": "={{ $json.company.ape_name }}"
            },
            {
              "id": "3",
              "name": "activite_declaree",
              "type": "string",
              "value": "={{ $json.company.activity }}"
            },
            {
              "id": "4",
              "name": "Ville",
              "type": "string",
              "value": "={{ $json.company.address.city }}"
            },
            {
              "id": "5",
              "name": "effectif",
              "type": "string",
              "value": "={{ $json.company.last_staff }}"
            },
            {
              "id": "6",
              "name": "ca",
              "type": "string",
              "value": "={{ $json.company.last_sales }}"
            },
            {
              "id": "8",
              "name": "linkedin_url",
              "type": "string",
              "value": "={{ $json.company.linkedin_url }}"
            },
            {
              "id": "9",
              "name": "Siren",
              "type": "string",
              "value": "={{ $('Contacts entreprise').item.json.company.full_registration_number }}"
            },
            {
              "id": "10",
              "name": "contact_prenom",
              "type": "string",
              "value": "={{ $('Contacts entreprise').item.json.contacts[0].firstName }}"
            },
            {
              "id": "14a7d027-a53e-4552-817b-fafdd7155aca",
              "name": "contact_nom",
              "value": "={{ $('Contacts entreprise').item.json.contacts[0].lastName }}",
              "type": "string"
            },
            {
              "id": "11",
              "name": "contact_role",
              "type": "string",
              "value": "={{ $('Contacts entreprise').item.json.contacts[0].role }}"
            },
            {
              "id": "12",
              "name": "contact_email",
              "type": "string",
              "value": "={{ $json.contacts.email }}"
            },
            {
              "id": "14",
              "name": "contact_linkedin",
              "type": "string",
              "value": "={{ $('Contacts entreprise').item.json.contacts[0].linkedin_url }}"
            },
            {
              "id": "15",
              "name": "pitch",
              "type": "string",
              "value": "={{ $('Criteres de campagne1').item.json['Proposition de valeur / pitch'] }}"
            }
          ]
        },
        "options": {}
      },
      "id": "b0ce3a9c-6435-4489-b761-a3aa507e9592",
      "name": "Contexte injecte1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -608,
        1216
      ]
    },
    {
      "parameters": {},
      "id": "57937ea7-61e3-4ace-aee9-bed7ed7423e7",
      "name": "Meilleur contact1",
      "type": "n8n-nodes-base.limit",
      "typeVersion": 1,
      "position": [
        -832,
        1216
      ]
    },
    {
      "parameters": {
        "sortFieldsUi": {
          "sortField": [
            {
              "fieldName": "contacts.contact_score",
              "order": "descending"
            }
          ]
        },
        "options": {}
      },
      "id": "78883576-4bc1-422b-a27c-5474252a5723",
      "name": "Trier par score1",
      "type": "n8n-nodes-base.sort",
      "typeVersion": 1,
      "position": [
        -1056,
        1216
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "contacts",
        "include": "selectedOtherFields",
        "fieldsToInclude": "company",
        "options": {}
      },
      "id": "930c4fee-bc84-4964-92de-bf67bec2e64f",
      "name": "Extraire contacts1",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        -1280,
        1216
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "leftValue": "={{ $json.result.organization.status }}",
              "rightValue": "Active",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            },
            {
              "leftValue": "={{ $json.result.organization.risk.risk_level }}",
              "rightValue": 3,
              "operator": {
                "type": "number",
                "operation": "lt"
              }
            }
          ]
        },
        "options": {}
      },
      "id": "bbee5a34-7cc7-4f8a-bf85-874356dbe74c",
      "name": "Filtre qualite1",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -1728,
        1280
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "5636d5a9-c1e0-4443-929b-1dca285b451e",
      "name": "Boucle entreprises1",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        -2176,
        1664
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "result",
        "options": {}
      },
      "id": "2b58a0ac-4591-4f57-bacb-ac733161a1bf",
      "name": "Extraire la liste1",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        -2400,
        1664
      ]
    },
    {
      "parameters": {
        "formTitle": "Criteres de campagne ICP - SocieteInfo",
        "formDescription": "Renseigne le ciblage de la campagne. Le workflow se charge du sourcing, de l'enrichissement et de la personnalisation IA.",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Mots-cles / activite",
              "placeholder": "ex. agence marketing",
              "requiredField": true
            },
            {
              "fieldLabel": "Zone (code postal ou departement)",
              "placeholder": "ex. 75, 92, 35000"
            },
            {
              "fieldLabel": "Effectif min",
              "fieldType": "number"
            },
            {
              "fieldLabel": "Effectif max",
              "fieldType": "number"
            },
            {
              "fieldLabel": "CA min",
              "fieldType": "number"
            },
            {
              "fieldLabel": "CA max",
              "fieldType": "number"
            },
            {
              "fieldLabel": "Domaine du contact cible",
              "fieldType": "dropdown",
              "fieldOptions": {
                "values": [
                  {
                    "option": "DG"
                  },
                  {
                    "option": "COMMERCIAL"
                  },
                  {
                    "option": "MARKETING"
                  },
                  {
                    "option": "RH"
                  },
                  {
                    "option": "IT"
                  }
                ]
              }
            },
            {
              "fieldLabel": "Niveau de responsabilite cible",
              "fieldType": "dropdown",
              "fieldOptions": {
                "values": [
                  {
                    "option": "DIRIGEANT"
                  },
                  {
                    "option": "DIRECTEUR"
                  },
                  {
                    "option": "RESPONSABLE"
                  }
                ]
              }
            },
            {
              "fieldLabel": "Proposition de valeur / pitch",
              "fieldType": "textarea",
              "requiredField": true
            }
          ]
        },
        "options": {}
      },
      "id": "3e3fbb6f-f065-43c2-aa5d-5cae3137216f",
      "name": "Criteres de campagne1",
      "type": "n8n-nodes-base.formTrigger",
      "typeVersion": 2.2,
      "position": [
        -2848,
        1664
      ],
      "webhookId": "7af5a93b-d471-4d08-8d8e-9c61205b7834"
    }
  ],
  "connections": {
    "Generation IA1": {
      "main": [
        [
          {
            "node": "Parser sortie IA1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Logger erreur1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Preparer payload IA1": {
      "main": [
        [
          {
            "node": "Generation IA1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Contacts entreprise": {
      "main": [
        [
          {
            "node": "Extraire contacts1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Logger erreur1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fiche complete entreprise": {
      "main": [
        [
          {
            "node": "Filtre qualite1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Logger erreur1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Recherche ICP": {
      "main": [
        [
          {
            "node": "Extraire la liste1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Logger erreur1": {
      "main": [
        [
          {
            "node": "Boucle entreprises1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ajout file de validation1": {
      "main": [
        [
          {
            "node": "Create a draft1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Boucle entreprises1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ligne finale1": {
      "main": [
        [
          {
            "node": "Ajout file de validation1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parser sortie IA1": {
      "main": [
        [
          {
            "node": "Ligne finale1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Contexte injecte1": {
      "main": [
        [
          {
            "node": "Preparer payload IA1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Meilleur contact1": {
      "main": [
        [
          {
            "node": "Contexte injecte1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Trier par score1": {
      "main": [
        [
          {
            "node": "Meilleur contact1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extraire contacts1": {
      "main": [
        [
          {
            "node": "Trier par score1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filtre qualite1": {
      "main": [
        [
          {
            "node": "Contacts entreprise",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Boucle entreprises1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Boucle entreprises1": {
      "main": [
        [],
        [
          {
            "node": "Fiche complete entreprise",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extraire la liste1": {
      "main": [
        [
          {
            "node": "Boucle entreprises1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Criteres de campagne1": {
      "main": [
        [
          {
            "node": "Recherche ICP",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "9dee12df844381439a70dd379f6ba42db52f05a4418537c99f051fe5852e5b31"
  }
}