{
  "name": "WhatSync: daily digest of unanswered chats",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "triggerAtHour": 9
            }
          ]
        }
      },
      "name": "Every morning",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ],
      "id": "c8e409a1-13a9-4aff-aff5-497a3b3348e2"
    },
    {
      "parameters": {},
      "name": "Try it by hand",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        200
      ],
      "id": "a53e1f07-1bf6-4fed-ae35-b6b31e4fef55"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "fd6d7546-de46-4a4c-a30f-03b89e5ef555",
              "name": "whatsyncUrl",
              "value": "https://whatsync.app",
              "type": "string"
            },
            {
              "id": "06e34b19-3946-43cf-a332-851557025a81",
              "name": "alertUrl",
              "value": "https://hooks.slack.com/services/REPLACE/WITH/YOURS",
              "type": "string"
            },
            {
              "id": "56411202-d6bf-468d-acd0-b60abb5b4220",
              "name": "timezone",
              "value": "Europe/Madrid",
              "type": "string"
            },
            {
              "id": "d7048f89-da9c-4071-a27c-20a8e30671a9",
              "name": "lookBackDays",
              "value": 7,
              "type": "number"
            }
          ]
        },
        "options": {}
      },
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        0
      ],
      "id": "0dc2b094-9cd7-4493-ae6a-af4324841d2c"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Settings').first().json.whatsyncUrl }}/api/v1/tools/list_unanswered_conversations",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ after: $now.minus({ days: $json.lookBackDays }).toUTC().toISO(), limit: 50 }) }}",
        "options": {
          "timeout": 30000
        }
      },
      "name": "Unanswered chats",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ],
      "id": "fac7e5d3-5585-4790-a5a2-6583315c5516",
      "credentials": {
        "httpHeaderAuth": {
          "id": "WHATSYNC_API_KEY",
          "name": "WhatSync API key"
        }
      },
      "notes": "One-to-one chats where the last message came from the other person. Read only: nothing is sent to anyone on WhatsApp."
    },
    {
      "parameters": {
        "jsCode": "// Turns the list of unanswered chats into one short message.\nconst settings = $('Settings').first().json;\nconst chats = ($input.first().json.conversations || []);\nif (!chats.length) return [{ json: { count: 0, text: '' } }];\nconst now = $now.setZone(settings.timezone);\nconst lines = chats.map((chat) => {\n  const waited = now.diff(DateTime.fromISO(chat.last_message_at), ['days', 'hours']).toObject();\n  const days = Math.floor(waited.days || 0), hours = Math.floor(waited.hours || 0);\n  const age = days ? days + (days === 1 ? ' day' : ' days') : hours + (hours === 1 ? ' hour' : ' hours');\n  const text = String(chat.last_inbound_text || '(no text)').replace(/\\s+/g, ' ').slice(0, 120);\n  return '• ' + (chat.contact_name || '+' + chat.phone_number) + ' (+' + chat.phone_number + '), waiting ' + age + ': \"' + text + '\"';\n});\nreturn [{ json: { count: chats.length, text: chats.length + (chats.length === 1 ? ' WhatsApp chat is' : ' WhatsApp chats are') + ' waiting for a reply:\\n' + lines.join('\\n') } }];\n"
      },
      "name": "Write the digest",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        0
      ],
      "id": "cde165d0-77a2-4f4b-ac72-89dec1187409"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "6969416a-bb92-435c-a1f8-cedc4569acfb",
              "leftValue": "={{ $json.count }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "name": "Anything waiting?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        880,
        0
      ],
      "id": "be5086b0-c2ee-4225-addb-f7e0c50b2f3f"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Settings').first().json.alertUrl }}",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ text: $json.text }) }}",
        "options": {
          "timeout": 15000
        }
      },
      "name": "Send the digest",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        -100
      ],
      "id": "5a0258d4-4d5e-4cda-a51f-9b07ae971220",
      "notes": "Works as is with a Slack incoming webhook. Swap this node for Email, Telegram or anything else that reaches a person."
    },
    {
      "parameters": {
        "content": "## Daily digest of unanswered WhatsApp chats\nEvery morning at 9, post the chats that are still waiting for your reply to Slack or email. It only reads. It never sends a WhatsApp message.\n\n**Set up**\n1. Pick your **WhatSync API key** credential in **Unanswered chats**.\n2. Put your alert URL and time zone in **Settings**.\n3. Click **Execute workflow** to try it, then publish.\n\nGuide: https://whatsync.app/docs/guides/daily-unanswered-digest",
        "height": 230,
        "width": 560,
        "color": 4
      },
      "name": "Read me",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        -310
      ],
      "id": "f86ab308-c4f3-4ee9-aa27-6f0f5fe8b250"
    }
  ],
  "connections": {
    "Every morning": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Unanswered chats",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Unanswered chats": {
      "main": [
        [
          {
            "node": "Write the digest",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write the digest": {
      "main": [
        [
          {
            "node": "Anything waiting?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Try it by hand": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Anything waiting?": {
      "main": [
        [
          {
            "node": "Send the digest",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  }
}
