{
  "name": "ZapZap: alert when an instance disconnects",
  "nodes": [
    {
      "parameters": {
        "content": "Receives connection events and warns an admin on WhatsApp. Use a DIFFERENT, connected instance in Config for the alert (the disconnected one cannot send). Open the Config node and fill instanceId, apiKey and apiSecret (dashboard: Settings and API). Then point the instance webhook to this workflow's Production URL (dashboard, or PUT /instances/{id}/webhook). Subscribe the monitored instance to the 'connection' event.",
        "width": 460,
        "height": 270
      },
      "id": "n1",
      "name": "Instructions",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -260,
        -140
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "zapzap-connection",
        "options": {}
      },
      "id": "n2",
      "name": "Connection event",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        100
      ],
      "webhookId": "zapzap-connection"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "n3",
              "name": "baseUrl",
              "value": "https://api.zapzapapi.com/api/v1",
              "type": "string"
            },
            {
              "id": "n4",
              "name": "instanceId",
              "value": "YOUR_INSTANCE_ID",
              "type": "string"
            },
            {
              "id": "n5",
              "name": "apiKey",
              "value": "YOUR_API_KEY",
              "type": "string"
            },
            {
              "id": "n6",
              "name": "apiSecret",
              "value": "YOUR_API_SECRET",
              "type": "string"
            },
            {
              "id": "n7",
              "name": "adminNumber",
              "value": "5511999999999",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "n8",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        100
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "n9",
              "leftValue": "={{ $json.body.data.instance.status }}",
              "rightValue": "disconnected",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "n10",
      "name": "Is disconnected",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        440,
        100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $(\"Config\").first().json.baseUrl }}/{{ $(\"Config\").first().json.instanceId }}/send/text",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-api-key",
              "value": "={{ $(\"Config\").first().json.apiKey }}"
            },
            {
              "name": "x-api-secret",
              "value": "={{ $(\"Config\").first().json.apiSecret }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ number: $json.adminNumber, text: 'Instance ' + $json.body.instance_name + ' disconnected. Open app.zapzapapi.com to reconnect it.' }) }}",
        "options": {}
      },
      "id": "n11",
      "name": "Alert admin",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        660,
        100
      ]
    }
  ],
  "connections": {
    "Connection event": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config": {
      "main": [
        [
          {
            "node": "Is disconnected",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is disconnected": {
      "main": [
        [
          {
            "node": "Alert admin",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  }
}
