{
  "id": "SoJXbESQ4lWQWcVt",
  "meta": {
    "instanceId": "89af474e60c70a1e45bf242e266e9ca1446dd974ab86f6d185b8ccded3effed2",
    "templateCredsSetupCompleted": true
  },
  "name": "Strava Social Manager (AI Title & Description Generation)",
  "tags": [
    {
      "id": "NabIyPgRf7ZggE5B",
      "name": "Showcase",
      "createdAt": "2025-06-03T15:41:30.076Z",
      "updatedAt": "2025-06-03T15:41:30.076Z"
    }
  ],
  "nodes": [
    {
      "id": "58ef8481-49d7-4522-9797-f191db60fa33",
      "name": "Combine Everything",
      "type": "n8n-nodes-base.code",
      "position": [
        -80,
        340
      ],
      "parameters": {
        "jsCode": "// Recursive function to flatten JSON into a single string\nfunction flattenJson(obj, prefix = '') {\n    let str = '';\n    for (const key in obj) {\n        if (typeof obj[key] === 'object' && obj[key] !== null) {\n            str += flattenJson(obj[key], `${prefix}${key}.`);\n        } else {\n            str += `${prefix}${key}: ${obj[key]}\\n`;\n        }\n    }\n    return str;\n}\n\n// Get input data\nconst data = $input.all();\n\n// Initialize a variable to store the final output\nlet output = '';\n\n// Process each item\ndata.forEach(item => {\n    output += flattenJson(item.json);\n    output += '\\n---\\n'; // Separator between records\n});\n\n// Return the merged string as output\nreturn [{ json: { data: output } }];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "d81a3699-09c6-4114-ad6e-652ce34854e8",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "position": [
        -300,
        340
      ],
      "parameters": {
        "jsCode": "// Loop over input items and add a new field called 'myNewField' to the JSON of each one\nfor (const item of $input.all()) {\n  item.json.myNewField = 1;\n}\n\nreturn $input.all();"
      },
      "typeVersion": 2
    },
    {
      "id": "ee87a5ae-331f-4066-84e8-43acdfaaf038",
      "name": "OpenRouter Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        160,
        520
      ],
      "parameters": {
        "model": "deepseek/deepseek-r1:free",
        "options": {}
      },
      "credentials": {
        "openRouterApi": {
          "id": "credential-id",
          "name": "openRouterApi Credential"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "7690c43e-282c-4fc6-af91-ba468040fdba",
      "name": "Strava Trigger",
      "type": "n8n-nodes-base.stravaTrigger",
      "position": [
        -520,
        340
      ],
      "webhookId": "d5e76c3e-5828-45f6-ae2a-ab70d8bfa93f",
      "parameters": {
        "event": "create",
        "object": "activity",
        "options": {}
      },
      "credentials": {
        "stravaOAuth2Api": {
          "id": "credential-id",
          "name": "stravaOAuth2Api Credential"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "b0dbf6f6-d3c9-43e3-997a-3593229e891e",
      "name": "Edit Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        520,
        340
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "fe37d921-5f51-4809-8fa9-e93b0983a566",
              "name": "Name",
              "type": "string",
              "value": "={{ $json.output.split('\\n')[0].split(': ')[1] }}"
            },
            {
              "id": "528a732c-7b96-47c1-8c8e-4354258569d9",
              "name": "Description",
              "type": "string",
              "value": "={{ $json.output.split('\\n')[1].split(': ')[1] }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "38316beb-1f92-43e5-ac48-ac982be6407f",
      "name": "Strava",
      "type": "n8n-nodes-base.strava",
      "position": [
        740,
        340
      ],
      "parameters": {
        "operation": "update",
        "activityId": "={{ $('Strava Trigger').item.json.object_id }}",
        "updateFields": {
          "name": "={{ $json.Name }}",
          "description": "={{ $json.Description }}"
        }
      },
      "credentials": {
        "stravaOAuth2Api": {
          "id": "credential-id",
          "name": "stravaOAuth2Api Credential"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "4be1f7de-7f50-45fb-8aa0-4707d25a23fb",
      "name": "Strava Social Manager",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        140,
        340
      ],
      "parameters": {
        "text": "=Tu es un assistant virtuel passionné de cyclisme.  \nÀ partir des informations ci-dessous sur ma sortie vélo, génère :\n- Un **titre accrocheur** (max 10 mots), original et adapté au contexte de la sortie.\n- Une **description** motivante, détaillée et personnalisée (3 à 5 phrases), qui met en valeur les aspects marquants de la sortie (lieux traversés, météo, performance, sensations, anecdotes, etc.).  \n\nUtilise un ton positif, dynamique et inspirant, comme si tu écrivais pour un carnet d’aventure cycliste.  \n\n**Données de la sortie :**\n- Distance : [X] km\n- Durée : [X] h [X] min\n- Dénivelé : [X] m\n- Vitesse moyenne : [X] km/h\n- Lieu/départ : [Ville/lieu]\n- Météo : [ensoleillé, pluvieux, vent, etc.]\n- Ressenti : [facile, difficile, super sensations, etc.]\n- Particularités : [découverte d’un nouveau parcours, sortie en groupe, crevaison, etc.]\n\n**Format attendu :**\n```\nTitre : [Titre généré]\nDescription : [Description générée]\n```\n\n---\n\nVoici les données de l'activité : \n{{ $json.data }}",
        "agent": "conversationalAgent",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 1.7
    },
    {
      "id": "f2941949-f743-4214-88a6-37899770e4df",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -940,
        0
      ],
      "parameters": {
        "color": 5,
        "width": 340,
        "height": 680,
        "content": "### **Step 0 – Connecting to the Strava API**\nBefore anything else, connect your Strava account to n8n via OAuth2:\n- Go to **Credentials** > **Strava OAuth2 API** in n8n.\n- Enter the Client ID and Client Secret obtained from the Strava developer portal.\n- Authorize the necessary scopes (`activity:read_all`, `activity:write`).\n- This connection allows n8n to securely access and modify your Strava activities.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "885370b1-7f2f-4411-b35e-3090dcbec2b8",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -580,
        0
      ],
      "parameters": {
        "width": 220,
        "height": 680,
        "content": "### **Step 1 – Strava Trigger**\nThis node automatically monitors the creation of new activities on your Strava account.\n- For each new activity, it retrieves all associated data (distance, duration, elevation gain, etc.).\n- It serves as the workflow’s entry point and passes the raw activity data to the next steps."
      },
      "typeVersion": 1
    },
    {
      "id": "28121d2d-ddd0-422b-b56d-099a32708216",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -340,
        0
      ],
      "parameters": {
        "width": 420,
        "height": 680,
        "content": "### **Step 2a – Data Processing (Code)**\nA Code node (JavaScript) adds or modifies fields in the activity data.\n- Example: Adding a custom field `myNewField` to enrich or prepare the data for use by the AI or other nodes.\n\n### **Step 2b – Data Flattening and Formatting (Combine Everything)**\nThis Code node \"flattens\" the complex Strava activity JSON structure into a simple text string.\n- It makes the data easily usable by the AI by presenting it as a readable, well-structured text.\n- Each activity key/value is listed on a line, making contextual analysis easier."
      },
      "typeVersion": 1
    },
    {
      "id": "fb1139ae-b154-49fa-a2f0-dd9e1d440f17",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        100,
        0
      ],
      "parameters": {
        "width": 340,
        "height": 680,
        "content": "### **Step 3 – AI Generation of Title and Description (Strava Social Manager)**\nThis node calls an AI agent (via OpenRouter or other service) with a detailed prompt:\n- It sends the activity data (flattened previously) to the AI.\n- The AI generates a catchy title and a personalized description, inspired by the context, performance, and highlights of the ride.\n- The prompt is designed to get an inspiring result tailored to the cycling community."
      },
      "typeVersion": 1
    },
    {
      "id": "c2897c26-5e64-44a4-8201-12f25f3a8e13",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        460,
        0
      ],
      "parameters": {
        "height": 680,
        "content": "### **Step 4 – Extracting AI Results (Edit Fields)**\nThe Set node extracts the AI-generated title and description from the output text.\n- It splits the text to isolate the title and description, storing them in separate fields.\n- This step prepares the data for updating on Strava.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "88418116-adc3-4438-8fa5-0fbfd399495b",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        720,
        0
      ],
      "parameters": {
        "height": 680,
        "content": "### **Step 5 – Automatic Update of the Activity on Strava**\nThe Strava node updates the relevant activity with the generated title and description.\n- It uses the activity ID provided by the initial trigger.\n- The `name` (title) and `description` fields are updated directly on Strava, making the ride more attractive and personalized."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "faf93495-ffbf-40f8-8dfe-89be52af5ba1",
  "connections": {
    "Code": {
      "main": [
        [
          {
            "node": "Combine Everything",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Strava",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Strava Trigger": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine Everything": {
      "main": [
        [
          {
            "node": "Strava Social Manager",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Strava Social Manager",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Strava Social Manager": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}