[
  {
    "name": "Blueticks: follow up on a failed message",
    "nodes": [
      {
        "id": "1",
        "name": "Blueticks webhook",
        "type": "n8n-nodes-base.webhook",
        "typeVersion": 2,
        "position": [
          0,
          0
        ],
        "webhookId": "blueticks-failed",
        "parameters": {
          "httpMethod": "POST",
          "path": "blueticks",
          "options": {
            "rawBody": true
          }
        }
      },
      {
        "id": "2",
        "name": "Verify signature",
        "type": "n8n-nodes-base.code",
        "typeVersion": 2,
        "position": [
          240,
          0
        ],
        "parameters": {
          "jsCode": "\nconst crypto = require('crypto');\nconst item = $input.first();\nconst headers = item.json.headers || {};\nconst ts = headers['blueticks-webhook-timestamp'];\nconst sig = headers['blueticks-webhook-signature'] || '';\nconst raw = Buffer.from(item.binary.data.data, 'base64').toString('utf8');\nif (!ts || !sig) throw new Error('unsigned delivery rejected');\nif (Math.abs(Date.now() / 1000 - Number(ts)) > 300) throw new Error('timestamp outside 5 min window');\nconst expected = 'v1=' + crypto.createHmac('sha256', $env.BLUETICKS_WEBHOOK_SECRET).update(ts + '.' + raw).digest('hex');\nconst a = Buffer.from(sig), b = Buffer.from(expected);\nif (a.length !== b.length || !crypto.timingSafeEqual(a, b)) throw new Error('bad signature');\nconst event = JSON.parse(raw);\nreturn [{ json: { eventId: event.id, type: event.type, data: event.data } }];\n"
        }
      },
      {
        "id": "3",
        "name": "Message failed?",
        "type": "n8n-nodes-base.if",
        "typeVersion": 2,
        "position": [
          480,
          0
        ],
        "parameters": {
          "conditions": {
            "options": {
              "caseSensitive": true,
              "typeValidation": "strict",
              "version": 2
            },
            "combinator": "and",
            "conditions": [
              {
                "id": "c1",
                "leftValue": "={{ $json.type }}",
                "rightValue": "message.failed",
                "operator": {
                  "type": "string",
                  "operation": "equals"
                }
              }
            ]
          }
        }
      },
      {
        "id": "4",
        "name": "Schedule follow-up",
        "type": "n8n-nodes-base.httpRequest",
        "typeVersion": 4.2,
        "position": [
          720,
          -80
        ],
        "parameters": {
          "method": "POST",
          "url": "={{ $env.BLUETICKS_API + \"/v1/scheduled-messages/\" + $json.data.to }}",
          "sendHeaders": true,
          "headerParameters": {
            "parameters": [
              {
                "name": "Authorization",
                "value": "={{ \"Bearer \" + $env.BLUETICKS_API_KEY }}"
              }
            ]
          },
          "sendBody": true,
          "specifyBody": "json",
          "jsonBody": "={{ JSON.stringify({ type: \"text\", text: \"Hi, following up on my earlier message. Can we pick this up?\", sendAt: new Date(Date.now() + 24*3600*1000).toISOString() }) }}"
        }
      },
      {
        "id": "5",
        "name": "Ignore other events",
        "type": "n8n-nodes-base.noOp",
        "typeVersion": 1,
        "position": [
          720,
          100
        ],
        "parameters": {}
      }
    ],
    "connections": {
      "Blueticks webhook": {
        "main": [
          [
            {
              "node": "Verify signature",
              "type": "main",
              "index": 0
            }
          ]
        ]
      },
      "Verify signature": {
        "main": [
          [
            {
              "node": "Message failed?",
              "type": "main",
              "index": 0
            }
          ]
        ]
      },
      "Message failed?": {
        "main": [
          [
            {
              "node": "Schedule follow-up",
              "type": "main",
              "index": 0
            }
          ],
          [
            {
              "node": "Ignore other events",
              "type": "main",
              "index": 0
            }
          ]
        ]
      }
    },
    "settings": {},
    "active": false
  }
]