{
  "name": "CommSync: inbound text to to-do",
  "nodes": [
    {
      "id": "b1",
      "name": "CommSync event",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "webhookId": "51077f4d-c069-4afa-8adf-071dd5c1a78a",
      "parameters": {
        "httpMethod": "POST",
        "path": "commsync-events",
        "responseMode": "responseNode",
        "options": {}
      }
    },
    {
      "id": "b2",
      "name": "Prepare check",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        0
      ],
      "parameters": {
        "mode": "manual",
        "includeOtherFields": false,
        "assignments": {
          "assignments": [
            {
              "id": "s1",
              "name": "t",
              "type": "string",
              "value": "={{ ($('CommSync event').item.json.headers['x-commsync-signature'] || '').split(',').map(part => part.trim()).filter(part => /^t=\\d+$/.test(part)).map(part => part.slice(2))[0] || '' }}"
            },
            {
              "id": "s2",
              "name": "signed",
              "type": "string",
              "value": "={{ (($('CommSync event').item.json.headers['x-commsync-signature'] || '').split(',').map(part => part.trim()).filter(part => /^t=\\d+$/.test(part)).map(part => part.slice(2))[0] || '') + '.' + JSON.stringify($json.body) }}"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "b3",
      "name": "HMAC-SHA256",
      "type": "n8n-nodes-base.crypto",
      "typeVersion": 2,
      "position": [
        440,
        0
      ],
      "parameters": {
        "action": "hmac",
        "type": "SHA256",
        "value": "={{ $json.signed }}",
        "dataPropertyName": "expected",
        "encoding": "hex"
      },
      "credentials": {
        "crypto": {
          "name": "CommSync signing secret"
        }
      }
    },
    {
      "id": "b4",
      "name": "Signature valid?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        660,
        0
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "c1",
              "leftValue": "={{ ($('CommSync event').item.json.headers['x-commsync-signature'] || '').split(',').includes('v1=' + $json.expected) && Math.abs(Date.now() / 1000 - Number($json.t)) <= 300 }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "b5",
      "name": "Accept",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        880,
        -120
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"ok\": true }",
        "options": {
          "responseCode": 200
        }
      }
    },
    {
      "id": "b6",
      "name": "Reject",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        880,
        120
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"ok\": false }",
        "options": {
          "responseCode": 401
        }
      }
    },
    {
      "id": "b7",
      "name": "Inbound message?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1100,
        -120
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "c2",
              "leftValue": "={{ $('CommSync event').item.json.body.type === 'message.received' && $('CommSync event').item.json.body.data.message.direction === 'INBOUND' }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "b8",
      "name": "Create to-do",
      "type": "@n8n/n8n-nodes-langchain.mcpClient",
      "typeVersion": 1.1,
      "position": [
        1320,
        -120
      ],
      "parameters": {
        "serverTransport": "httpStreamable",
        "endpointUrl": "https://server.commsync.ai/api/mcp",
        "authentication": "bearerAuth",
        "tool": {
          "__rl": true,
          "mode": "id",
          "value": "create_todo"
        },
        "inputMode": "json",
        "jsonInput": "={{ JSON.stringify({ threadId: $('CommSync event').item.json.body.data.thread.id, title: ('Reply to ' + (($('CommSync event').item.json.body.data.person && $('CommSync event').item.json.body.data.person.displayName) || ($('CommSync event').item.json.body.data.message.fromIdentity && $('CommSync event').item.json.body.data.message.fromIdentity.value) || 'unknown sender')).replace(/\\u0000/g, '').slice(0, 500), details: String($('CommSync event').item.json.body.data.message.body || '').replace(/\\u0000/g, '').slice(0, 4000) }) }}",
        "options": {}
      },
      "credentials": {
        "httpBearerAuth": {
          "name": "CommSync API key"
        }
      }
    }
  ],
  "connections": {
    "CommSync event": {
      "main": [
        [
          {
            "node": "Prepare check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare check": {
      "main": [
        [
          {
            "node": "HMAC-SHA256",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HMAC-SHA256": {
      "main": [
        [
          {
            "node": "Signature valid?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Signature valid?": {
      "main": [
        [
          {
            "node": "Accept",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Reject",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Accept": {
      "main": [
        [
          {
            "node": "Inbound message?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Inbound message?": {
      "main": [
        [
          {
            "node": "Create to-do",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  }
}
