{
 "name": "n8n × Nextcloud #1: the morning briefing from calendar and tasks, straight into Talk",
 "nodes": [
  {
   "parameters": {
    "rule": {
     "interval": [
      {
       "field": "cronExpression",
       "expression": "30 7 * * 1-5"
      }
     ]
    }
   },
   "id": "0c2e0a77-738a-5a02-b5bb-94bd00d419b0",
   "name": "Weekdays at 07:30",
   "position": [
    0,
    0
   ],
   "type": "n8n-nodes-base.scheduleTrigger",
   "typeVersion": 1.2
  },
  {
   "parameters": {
    "assignments": {
     "assignments": [
      {
       "id": "33ff9357-2fc1-5c49-80d4-095ecb1e0e7e",
       "name": "nc_url",
       "value": "https://cloud.example.com",
       "type": "string"
      },
      {
       "id": "258be886-7a9b-50ea-94e7-28915f1d9a56",
       "name": "nc_user",
       "value": "your-user",
       "type": "string"
      },
      {
       "id": "4a33968b-b37d-590c-8427-391df3e656c5",
       "name": "calendar",
       "value": "personal",
       "type": "string"
      },
      {
       "id": "174513db-81d4-50f2-b821-ecb2a29890ad",
       "name": "tasks_calendar",
       "value": "personal",
       "type": "string"
      },
      {
       "id": "9e7b8d23-25d7-593c-b694-93fc271c8fee",
       "name": "talk_room",
       "value": "your-talk-room-token",
       "type": "string"
      },
      {
       "id": "9fbbcf42-88c5-5762-b54c-28a27c16d9de",
       "name": "language",
       "value": "German",
       "type": "string"
      },
      {
       "id": "76bd2d0b-99c1-5b5d-97f4-32c11910666a",
       "name": "locale",
       "value": "de",
       "type": "string"
      },
      {
       "id": "df01a9af-4638-5377-ba23-555fee8f38aa",
       "name": "use_ai",
       "value": "yes",
       "type": "string"
      },
      {
       "id": "9c2da602-83c7-5797-aebe-a969e0905f8a",
       "name": "ai_url",
       "value": "https://openai.inference.de-txl.ionos.com/v1",
       "type": "string"
      },
      {
       "id": "e51ad496-9309-5bb1-821f-2ceef5dc9ed3",
       "name": "ai_model",
       "value": "meta-llama/Llama-3.3-70B-Instruct",
       "type": "string"
      },
      {
       "id": "7cede41f-6889-52f1-a9d7-cd9527dfe623",
       "name": "day_start",
       "value": "={{ Math.floor($now.startOf('day').toSeconds()) }}",
       "type": "string"
      },
      {
       "id": "c907bb0a-5fb2-579c-90ed-0c7d8b124963",
       "name": "day_end",
       "value": "={{ Math.floor($now.endOf('day').toSeconds()) }}",
       "type": "string"
      }
     ]
    },
    "options": {}
   },
   "id": "6105da5e-71b6-5186-ab81-d59303d6e558",
   "name": "Config",
   "position": [
    240,
    0
   ],
   "type": "n8n-nodes-base.set",
   "typeVersion": 3.4
  },
  {
   "parameters": {
    "method": "GET",
    "url": "={{ $('Config').first().json.nc_url }}/remote.php/dav/calendars/{{ $('Config').first().json.nc_user }}/{{ $('Config').first().json.calendar }}/",
    "sendHeaders": true,
    "headerParameters": {
     "parameters": [
      {
       "name": "User-Agent",
       "value": "n8n workflow (+https://daniele-michelin.com/n8n-nextcloud/)"
      }
     ]
    },
    "options": {
     "timeout": 30000,
     "response": {
      "response": {
       "responseFormat": "text"
      }
     }
    },
    "sendQuery": true,
    "queryParameters": {
     "parameters": [
      {
       "name": "export",
       "value": ""
      },
      {
       "name": "accept",
       "value": "jcal"
      },
      {
       "name": "expand",
       "value": "1"
      },
      {
       "name": "start",
       "value": "={{ $('Config').first().json.day_start }}"
      },
      {
       "name": "end",
       "value": "={{ $('Config').first().json.day_end }}"
      }
     ]
    },
    "authentication": "genericCredentialType",
    "genericAuthType": "httpBasicAuth"
   },
   "id": "9461e9d7-1ead-55ab-b2ce-34e31208549b",
   "name": "Today's events",
   "position": [
    480,
    -70
   ],
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "credentials": {
    "httpBasicAuth": {
     "id": "",
     "name": "Nextcloud (user + app password)"
    }
   }
  },
  {
   "parameters": {
    "method": "GET",
    "url": "={{ $('Config').first().json.nc_url }}/remote.php/dav/calendars/{{ $('Config').first().json.nc_user }}/{{ $('Config').first().json.tasks_calendar }}/",
    "sendHeaders": true,
    "headerParameters": {
     "parameters": [
      {
       "name": "User-Agent",
       "value": "n8n workflow (+https://daniele-michelin.com/n8n-nextcloud/)"
      }
     ]
    },
    "options": {
     "timeout": 30000,
     "response": {
      "response": {
       "responseFormat": "text"
      }
     }
    },
    "sendQuery": true,
    "queryParameters": {
     "parameters": [
      {
       "name": "export",
       "value": ""
      },
      {
       "name": "accept",
       "value": "jcal"
      },
      {
       "name": "componentType",
       "value": "VTODO"
      }
     ]
    },
    "authentication": "genericCredentialType",
    "genericAuthType": "httpBasicAuth"
   },
   "id": "d370e150-7c88-53e1-9bca-e80c34e9b6a5",
   "name": "Open tasks",
   "position": [
    480,
    70
   ],
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "credentials": {
    "httpBasicAuth": {
     "id": "",
     "name": "Nextcloud (user + app password)"
    }
   }
  },
  {
   "parameters": {
    "numberInputs": 2
   },
   "id": "cfc38bed-212c-5ca0-9a80-b4f88706a3a6",
   "name": "Wait for both",
   "position": [
    720,
    0
   ],
   "type": "n8n-nodes-base.merge",
   "typeVersion": 3
  },
  {
   "parameters": {
    "mode": "runOnceForAllItems",
    "jsCode": "// Reads the two jCal exports, keeps today's meetings and the tasks that are due or important.\nconst cfg = $('Config').first().json;\nconst tz = $now.zoneName;\nconst today = $now.toFormat('yyyy-MM-dd');\nfunction comps(raw, type) {                      // jCal: [\"vcalendar\", props, [[type, props, subs], …]]\n  let cal; try { cal = JSON.parse(raw || '[]'); } catch (e) { return []; }\n  return (cal[2] || []).filter(c => c[0] === type)\n    .map(c => Object.fromEntries(c[1].map(p => [p[0], p[3]])));\n}\nconst local = v => /^\\d{4}-\\d{2}-\\d{2}$/.test(v || '') ? null : DateTime.fromISO(v, { zone: 'utc' }).setZone(tz);\nconst events = comps($('Today\\'s events').first().json.data, 'vevent')\n  .map(e => ({ s: local(e.dtstart), e: local(e.dtend), title: e.summary || '(no title)', where: e.location || '' }))\n  .sort((a, b) => (a.s ? a.s.toMillis() : 0) - (b.s ? b.s.toMillis() : 0));\nconst overlaps = [];\nfor (let i = 1; i < events.length; i++) {\n  const a = events[i - 1], b = events[i];\n  if (a.e && b.s && b.s < a.e) overlaps.push(`${a.title} / ${b.title}`);\n}\nconst tasks = comps($('Open tasks').first().json.data, 'vtodo')\n  .filter(t => t.status !== 'COMPLETED' && !t.completed)\n  .map(t => ({ title: t.summary || '(no title)', due: (t.due || '').slice(0, 10), prio: Number(t.priority || 0) }))\n  .filter(t => (t.due && t.due <= today) || (t.prio >= 1 && t.prio <= 4))\n  .sort((a, b) => (a.due || '9') < (b.due || '9') ? -1 : 1)\n  .slice(0, 8);\nconst lines = [`☀️ ${$now.setLocale(cfg.locale || 'en').toFormat('cccc, dd.MM.')}`];\nlines.push(...(events.length ? events.map(e => `📅 ${e.s ? e.s.toFormat('HH:mm') : 'all day'} ${e.title}${e.where ? ' (' + e.where + ')' : ''}`) : ['📅 –']));\nif (overlaps.length) lines.push(...overlaps.map(o => `⚠️ ${o}`));\nlines.push(...tasks.map(t => `${t.due && t.due < today ? '🔴' : '✅'} ${t.title}${t.due ? ' · ' + t.due.slice(8, 10) + '.' + t.due.slice(5, 7) + '.' : ''}`));\nconst plain = lines.join('\\n');\nconst body = { model: cfg.ai_model, temperature: 0.3, max_tokens: 400, messages: [\n  { role: 'system', content: `You write a short, friendly morning briefing for an office worker in ${cfg.language}. ` +\n    'Use only the facts given, invent nothing. At most 6 lines. Mention overlapping meetings (⚠️) and overdue tasks (🔴) first.' },\n  { role: 'user', content: plain } ] };\nreturn [{ json: { plain, body, events: events.length, tasks: tasks.length, use_ai: cfg.use_ai === 'yes' } }];"
   },
   "id": "35e4bfa0-ec7a-515d-a0fb-f99f1783fca5",
   "name": "Build briefing",
   "position": [
    960,
    0
   ],
   "type": "n8n-nodes-base.code",
   "typeVersion": 2
  },
  {
   "parameters": {
    "conditions": {
     "options": {
      "caseSensitive": true,
      "leftValue": "",
      "typeValidation": "strict"
     },
     "conditions": [
      {
       "id": "dee5350b-29e9-528f-a8d1-183b3eb80433",
       "leftValue": "={{ $json.use_ai }}",
       "rightValue": "",
       "operator": {
        "type": "boolean",
        "operation": "true",
        "singleValue": true
       }
      }
     ],
     "combinator": "and"
    },
    "options": {}
   },
   "id": "225effbd-ac87-547b-8946-89e2c6c75637",
   "name": "Use AI?",
   "position": [
    1200,
    0
   ],
   "type": "n8n-nodes-base.if",
   "typeVersion": 2
  },
  {
   "parameters": {
    "method": "POST",
    "url": "={{ $('Config').first().json.ai_url }}/chat/completions",
    "options": {
     "timeout": 90000
    },
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify($json.body) }}",
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth"
   },
   "id": "bb192cfe-d1e7-5ad5-b8e6-92a37ab46d66",
   "name": "AI summary",
   "position": [
    1440,
    0
   ],
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "credentials": {
    "httpHeaderAuth": {
     "id": "",
     "name": "AI API key (Header Auth: Authorization = Bearer …)"
    }
   },
   "onError": "continueRegularOutput"
  },
  {
   "parameters": {
    "mode": "runOnceForAllItems",
    "jsCode": "// AI text if there is one, otherwise the plain list: the briefing always arrives.\nconst ai = $input.first().json;\nconst text = ai.choices && ai.choices[0] && ai.choices[0].message ? String(ai.choices[0].message.content || '').trim() : '';\nconst plain = $('Build briefing').first().json.plain;\nreturn [{ json: { message: text ? `${text}\\n\\n— n8n · ${$now.toFormat('HH:mm')}` : plain } }];"
   },
   "id": "fecd18b9-4b5c-5380-86f4-3a9510a06df5",
   "name": "Compose message",
   "position": [
    1680,
    0
   ],
   "type": "n8n-nodes-base.code",
   "typeVersion": 2
  },
  {
   "parameters": {
    "method": "POST",
    "url": "={{ $('Config').first().json.nc_url }}/ocs/v2.php/apps/spreed/api/v1/chat/{{ $('Config').first().json.talk_room }}",
    "sendHeaders": true,
    "headerParameters": {
     "parameters": [
      {
       "name": "User-Agent",
       "value": "n8n workflow (+https://daniele-michelin.com/n8n-nextcloud/)"
      },
      {
       "name": "OCS-APIRequest",
       "value": "true"
      },
      {
       "name": "Accept",
       "value": "application/json"
      }
     ]
    },
    "options": {
     "timeout": 30000
    },
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify({ message: $json.message }) }}",
    "authentication": "genericCredentialType",
    "genericAuthType": "httpBasicAuth"
   },
   "id": "fdbca671-aa38-514f-9b29-7f19c85a03c9",
   "name": "Post to Talk",
   "position": [
    1920,
    0
   ],
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "credentials": {
    "httpBasicAuth": {
     "id": "",
     "name": "Nextcloud (user + app password)"
    }
   }
  }
 ],
 "connections": {
  "Weekdays at 07:30": {
   "main": [
    [
     {
      "node": "Config",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Config": {
   "main": [
    [
     {
      "node": "Today's events",
      "type": "main",
      "index": 0
     },
     {
      "node": "Open tasks",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Today's events": {
   "main": [
    [
     {
      "node": "Wait for both",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Open tasks": {
   "main": [
    [
     {
      "node": "Wait for both",
      "type": "main",
      "index": 1
     }
    ]
   ]
  },
  "Wait for both": {
   "main": [
    [
     {
      "node": "Build briefing",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Build briefing": {
   "main": [
    [
     {
      "node": "Use AI?",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Use AI?": {
   "main": [
    [
     {
      "node": "AI summary",
      "type": "main",
      "index": 0
     }
    ],
    [
     {
      "node": "Compose message",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "AI summary": {
   "main": [
    [
     {
      "node": "Compose message",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Compose message": {
   "main": [
    [
     {
      "node": "Post to Talk",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 },
 "settings": {
  "executionOrder": "v1"
 },
 "pinData": {},
 "meta": {
  "source": "https://daniele-michelin.com/n8n-nextcloud/",
  "nextcloud_apps": [
   "Calendar",
   "Tasks",
   "Talk",
   "Assistant/AI"
  ]
 }
}