{
  "format": "jsonpage-zap-template",
  "version": 1,
  "name": "Jsonpage - Monitoring alert to Slack or Teams and a HubSpot task",
  "description": "A Jsonpage monitoring alert posts a message in Slack or Teams and creates a task in HubSpot.",
  "importable": false,
  "importNote": "Zapier cannot import a Zap from a file: create the Zap with the steps below, in order. Each step lists the app, the action and the exact settings. {{...}} marks a field to map from a previous step.",
  "apiKeyPlaceholder": "PASTE_YOUR_JSONPAGE_API_KEY",
  "trigger": {
    "app": "Webhooks by Zapier",
    "event": "Catch Hook",
    "note": "Paste the address of the hook in the Monitoring section of your Jsonpage dashboard, then use the test button there to send a test event."
  },
  "steps": [
    {
      "order": 1,
      "title": "Company name",
      "app": "Webhooks by Zapier",
      "action": "Custom Request",
      "method": "GET",
      "url": "https://api.jsonpage.com/v2/companies/{{trigger.company_id}}",
      "headers": {
        "X-API-Key": "PASTE_YOUR_JSONPAGE_API_KEY"
      }
    },
    {
      "order": 2,
      "title": "Build the message",
      "app": "Code by Zapier",
      "action": "Run Javascript",
      "inputData": {
        "type": "{{trigger.type}}",
        "company_id": "{{trigger.company_id}}",
        "detected_at": "{{trigger.detected_at}}",
        "before": "{{trigger.data__before}}",
        "after": "{{trigger.data__after}}",
        "code": "{{trigger.data__code}}",
        "detail": "{{trigger.data__detail}}",
        "name": "{{step1.data__name}}",
        "chat": "slack"
      },
      "code": "// Code by Zapier (JavaScript): builds the alert text and the payloads of the chat message and of the task.\n// Input data: type, company_id, detected_at, before, after, code, detail, name (company name from the previous step), chat = slack or teams\nconst labels = {\n  'company.status_changed': 'status changed', 'company.name_changed': 'name changed', 'company.address_changed': 'address changed',\n  'company.activity_changed': 'activity changed', 'company.legal_form_changed': 'legal form changed',\n  'company.removed_from_register': 'removed from the register', 'signal.added': 'warning signal added', 'signal.removed': 'warning signal removed',\n  'legal_event.published': 'legal announcement published', 'sanctions.changed': 'sanctions status changed',\n  'owner.added': 'owner added', 'owner.removed': 'owner removed', test: 'test event'\n};\nconst name = inputData.name || inputData.company_id;\nlet detail = '';\nif (inputData.before || inputData.after) detail = ' (' + (inputData.before || '-') + ' -> ' + (inputData.after || '-') + ')';\nelse if (inputData.code) detail = ' (' + inputData.code + (inputData.detail ? ': ' + inputData.detail : '') + ')';\nconst label = labels[inputData.type] || inputData.type;\nconst text = 'Jsonpage: ' + (inputData.company_id ? name + ' (' + inputData.company_id + ') ' : '') + label + detail + ' - ' + inputData.detected_at;\nconst chat = String(inputData.chat || 'slack').toLowerCase() === 'teams'\n  ? { type: 'message', attachments: [{ contentType: 'application/vnd.microsoft.card.adaptive', content: { $schema: 'http://adaptivecards.io/schemas/adaptive-card.json', type: 'AdaptiveCard', version: '1.4', body: [{ type: 'TextBlock', text, wrap: true }] } }] }\n  : { text };\nconst task = { properties: { hs_timestamp: new Date().toISOString(), hs_task_subject: 'Check ' + name + ': ' + label, hs_task_body: text, hs_task_status: 'NOT_STARTED', hs_task_priority: 'HIGH' } };\nreturn { text, chat: JSON.stringify(chat), task: JSON.stringify(task), isTest: inputData.type === 'test' ? 'yes' : '' };"
    },
    {
      "order": 3,
      "title": "Chat message (Slack or Teams)",
      "app": "Webhooks by Zapier",
      "action": "Custom Request",
      "method": "POST",
      "url": "PASTE_YOUR_SLACK_OR_TEAMS_WEBHOOK_URL",
      "headers": {
        "Content-Type": "application/json"
      },
      "data": "{{step2.chat}}",
      "unflatten": "no",
      "alternative": {
        "app": "Slack",
        "action": "Send Channel Message",
        "fields": {
          "Message Text": "{{step2.text}}"
        }
      }
    },
    {
      "order": 4,
      "title": "Only for real events",
      "app": "Filter by Zapier",
      "action": "Only continue if",
      "condition": "{{trigger.type}} does not exactly match test"
    },
    {
      "order": 5,
      "title": "Task in HubSpot",
      "app": "Webhooks by Zapier",
      "action": "Custom Request",
      "method": "POST",
      "url": "https://api.hubapi.com/crm/v3/objects/tasks",
      "headers": {
        "Authorization": "Bearer PASTE_YOUR_HUBSPOT_PRIVATE_APP_TOKEN",
        "Content-Type": "application/json"
      },
      "data": "{{step2.task}}",
      "unflatten": "no"
    }
  ],
  "notes": [
    "Step 1 answers 404 for the test event (empty company_id): in the step, keep \"Unflatten\" on and let the Zap continue; the message then uses the identifier.",
    "Zapier does not give access to the signature header: keep the address of the hook secret.",
    "Teams: use the address of a Workflows webhook (\"Post to a channel when a webhook request is received\")."
  ]
}
