{
  "name": "EchoRadar → Notion page summary",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "echoradar-notion-summary",
        "responseMode": "lastNode",
        "options": {
          "rawBody": true
        }
      },
      "id": "b1a1d0f2-0001-4a00-9000-000000000001",
      "name": "EchoRadar webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -200,
        0
      ],
      "webhookId": "b1a1d0f2-0001-4a00-9000-000000000001"
    },
    {
      "parameters": {
        "jsCode": "// EchoRadar → Notion: verify the delivery, then read the event.\n//\n// Two things must be true before anything reaches Notion: the request really came from your\n// EchoRadar, and it is the event this workflow knows how to render. Everything below is that,\n// and nothing else.\n//\n// ── EDIT THESE TWO ───────────────────────────────────────────────────────────\n// Plain constants on purpose. n8n blocks a Code node from reading environment variables\n// unless the instance is started with that access turned on, so `$env` here would fail for\n// everybody who imported this file and followed the steps — the one thing a template must\n// not do. Reading them from the environment instead is in the README under \"Advanced\".\n//\n// Whichever you choose: this file is shared, exported and pasted into issues. Do not commit\n// it with a real secret in it.\nconst SECRET = 'PUT-YOUR-WEBHOOK-SECRET-HERE';\nconst NOTION_PAGE_ID = 'PUT-YOUR-NOTION-PAGE-ID-HERE';\n// ─────────────────────────────────────────────────────────────────────────────\n\n// n8n's Code node sandbox refuses built-in modules unless the instance allows them, so this\n// line is why the README starts n8n with NODE_FUNCTION_ALLOW_BUILTIN=crypto. One module, not\n// a wildcard: the workflow needs HMAC and nothing else.\nconst crypto = require('crypto');\nconst item = $input.first();\nconst headers = item.json.headers || {};\n\n// The signature is computed over the exact bytes EchoRadar sent. Re-serializing the parsed\n// JSON would produce different bytes — same value, different key order and spacing — and the\n// signature would never match. That is why the Webhook node must have \"Raw Body\" enabled.\nconst binary = item.binary && item.binary.data;\nif (!binary) {\n  throw new Error(\n    'No raw body. Open the Webhook node → Options → add \"Raw Body\" and turn it on, then send ' +\n      'the event again. Without the exact bytes the signature cannot be checked.',\n  );\n}\nconst raw = Buffer.from(binary.data, 'base64');\n\nconst contract = headers['x-echoradar-contract'];\nif (contract !== 'event.v1') {\n  throw new Error(`Unexpected envelope version: ${contract}. This workflow reads event.v1.`);\n}\n\nconst signature = headers['x-echoradar-signature'];\nconst expected = crypto.createHmac('sha256', SECRET).update(raw).digest('hex');\n\n// Constant-time: a plain === leaks, byte by byte, how much of a guess was right.\nconst ok =\n  typeof signature === 'string' &&\n  signature.length === expected.length &&\n  crypto.timingSafeEqual(Buffer.from(signature, 'utf8'), Buffer.from(expected, 'utf8'));\n\nif (!ok) {\n  throw new Error(\n    'Signature does not match. The secret here and the one in EchoRadar → Settings → ' +\n      'Integrations must be the same string.',\n  );\n}\n\nconst event = JSON.parse(raw.toString('utf8'));\nif (event.eventType !== 'post_call_package_ready') {\n  throw new Error(`Nothing to render for ${event.eventType}.`);\n}\n\nconst payload = event.payload || {};\n\nreturn [\n  {\n    json: {\n      notionPageId: NOTION_PAGE_ID,\n      // The envelope's key, not the payload's: this is the one the delivery layer puts in\n      // X-EchoRadar-Idempotency-Key, so it is the one to dedupe on if you add dedupe later.\n      idempotencyKey: event.idempotencyKey,\n      eventId: event.eventId,\n      conversationId: payload.provenance ? payload.provenance.conversationId : null,\n      payload,\n    },\n  },\n];\n"
      },
      "id": "b1a1d0f2-0002-4a00-9000-000000000002",
      "name": "Verify signature and read event",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        40,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// Turn one post-call package into Notion blocks.\n//\n// Only sections that have something in them are rendered. A page that always prints five empty\n// headings teaches the reader to stop looking at it.\n\nconst NOTION_TEXT_LIMIT = 2000; // Notion refuses a rich_text item longer than this.\nconst MAX_BLOCKS = 100; // and more than this many children in one request.\n\nconst { notionPageId, idempotencyKey, eventId, conversationId, payload } = $input.first().json;\n\nconst clip = (value) => String(value == null ? '' : value).slice(0, NOTION_TEXT_LIMIT);\nconst text = (content, annotations) => ({\n  type: 'text',\n  text: { content: clip(content) },\n  ...(annotations ? { annotations } : {}),\n});\n\nconst heading = (content) => ({\n  object: 'block',\n  type: 'heading_3',\n  heading_3: { rich_text: [text(content)] },\n});\nconst paragraph = (rich) => ({ object: 'block', type: 'paragraph', paragraph: { rich_text: rich } });\nconst bullet = (rich) => ({\n  object: 'block',\n  type: 'bulleted_list_item',\n  bulleted_list_item: { rich_text: rich },\n});\n\nconst meta = payload.metadata || {};\nconst when = meta.meetingDate ? new Date(meta.meetingDate).toISOString().slice(0, 16).replace('T', ' ') : '';\nconst who = meta.guestName || 'Call';\n\nconst blocks = [\n  {\n    object: 'block',\n    type: 'heading_2',\n    heading_2: { rich_text: [text(`${who} — ${when} (${meta.scenarioType || 'call'})`)] },\n  },\n];\n\nif (payload.summary) {\n  blocks.push(paragraph([text(payload.summary)]));\n}\n\nconst listSection = (title, values) => {\n  if (!values || values.length === 0) return;\n  blocks.push(heading(title));\n  for (const value of values) blocks.push(bullet([text(value)]));\n};\n\n// Findings carry who said it and the sentence it was taken from. The speaker is the whole\n// point: \"we promised\" and \"they promised\" are different rows in anybody's follow-up list, and\n// a transcript alone cannot tell them apart.\nconst findingSection = (title, items) => {\n  if (!items || items.length === 0) return;\n  blocks.push(heading(title));\n  for (const finding of items) {\n    const speaker = finding.speaker === 'counterpart' ? 'They' : 'We';\n    blocks.push(\n      bullet([\n        text(`${speaker}: `, { bold: true }),\n        text(finding.text),\n        text(` — “${finding.quote}”`, { italic: true, color: 'gray' }),\n      ]),\n    );\n  }\n};\n\nlistSection('Next steps', payload.nextSteps);\nlistSection('Open questions', payload.openQuestions);\nfindingSection('Commitments', payload.commitments);\nfindingSection('Risk flags', payload.riskFlags);\n\nblocks.push({ object: 'block', type: 'divider', divider: {} });\nblocks.push(\n  paragraph([\n    text(`EchoRadar · event ${eventId} · conversation ${conversationId} · key ${idempotencyKey}`, {\n      color: 'gray',\n    }),\n  ]),\n);\n\n// Notion takes at most 100 children per request. Long calls do reach this.\nreturn [{ json: { notionPageId, children: blocks.slice(0, MAX_BLOCKS) } }];\n"
      },
      "id": "b1a1d0f2-0003-4a00-9000-000000000003",
      "name": "Build Notion blocks",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        280,
        0
      ]
    },
    {
      "parameters": {
        "method": "PATCH",
        "url": "=https://api.notion.com/v1/blocks/{{ $json.notionPageId }}/children",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "notionApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Notion-Version",
              "value": "2022-06-28"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ children: $json.children }) }}",
        "options": {}
      },
      "id": "b1a1d0f2-0004-4a00-9000-000000000004",
      "name": "Append to Notion page",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        520,
        0
      ]
    }
  ],
  "connections": {
    "EchoRadar webhook": {
      "main": [
        [
          {
            "node": "Verify signature and read event",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verify signature and read event": {
      "main": [
        [
          {
            "node": "Build Notion blocks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Notion blocks": {
      "main": [
        [
          {
            "node": "Append to Notion page",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "active": false
}
