{
  "name": "Instagram Follow-up Workflow",
  "nodes": [
    {
      "parameters": {
        "triggerTimes": [
          {
            "mode": "everyHour"
          }
        ]
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        200,
        100
      ]
    },
    {
      "parameters": {
        "operation": "list",
        "baseId": "YOUR_BASE_ID",
        "tableId": "YOUR_TABLE_NAME",
        "additionalOptions": {
          "filterByFormula": "AND({replied} = FALSE(), OR(AND(NOT({first_followup_sent}), DATETIME_DIFF(NOW(), {last_message_timestamp}, 'hours') >= 12), AND(NOT({second_followup_sent}), DATETIME_DIFF(NOW(), {last_message_timestamp}, 'hours') >= 23)))"
        }
      },
      "name": "Fetch Active Users",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2,
      "position": [
        400,
        100
      ],
      "credentials": {
        "airtableApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "return items.map(item => {\n  const now = new Date();\n  const lastMsg = new Date(item.json.last_message_timestamp);\n  const hoursSince = (now - lastMsg) / (1000 * 60 * 60);\n\n  if (!item.json.first_followup_sent && hoursSince >= 12 && hoursSince < 23) {\n    item.json.followupStage = 'first';\n    return item;\n  }\n\n  if (!item.json.second_followup_sent && hoursSince >= 23) {\n    item.json.followupStage = 'second';\n    return item;\n  }\n\n  return undefined;\n}).filter(item => item);"
      },
      "name": "Determine Follow-up Stage",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        100
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.followupStage}}",
              "operation": "equal",
              "value2": "first"
            }
          ]
        }
      },
      "name": "First or Second Follow-up?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        800,
        100
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "https://graph.facebook.com/v19.0/YOUR_IG_USER_ID/messages",
        "authentication": "headerAuth",
        "jsonParameters": true,
        "bodyContentType": "json",
        "bodyParametersJson": "{\n  \"recipient\": { \"id\": \"{{$json.user_id}}\" },\n  \"message\": { \"text\": \"Hey! Just following up. Let me know if you have any questions \ud83d\ude0a\" }\n}"
      },
      "name": "Send First Follow-up",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        1000,
        0
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "baseId": "YOUR_BASE_ID",
        "tableId": "YOUR_TABLE_NAME",
        "id": "={{$json.id}}",
        "fields": {
          "first_followup_sent": true
        }
      },
      "name": "Update First Follow-up",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2,
      "position": [
        1200,
        0
      ],
      "credentials": {
        "airtableApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "https://graph.facebook.com/v19.0/YOUR_IG_USER_ID/messages",
        "authentication": "headerAuth",
        "jsonParameters": true,
        "bodyContentType": "json",
        "bodyParametersJson": "{\n  \"recipient\": { \"id\": \"{{$json.user_id}}\" },\n  \"message\": { \"text\": \"Just following up again. Feel free to reply if you need anything!\" }\n}"
      },
      "name": "Send Second Follow-up",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 2,
      "position": [
        1000,
        200
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "baseId": "YOUR_BASE_ID",
        "tableId": "YOUR_TABLE_NAME",
        "id": "={{$json.id}}",
        "fields": {
          "second_followup_sent": true
        }
      },
      "name": "Update Second Follow-up",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2,
      "position": [
        1200,
        200
      ],
      "credentials": {
        "airtableApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Cron": {
      "main": [
        [
          {
            "node": "Fetch Active Users",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Active Users": {
      "main": [
        [
          {
            "node": "Determine Follow-up Stage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Determine Follow-up Stage": {
      "main": [
        [
          {
            "node": "First or Second Follow-up?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "First or Second Follow-up?": {
      "main": [
        [
          {
            "node": "Send First Follow-up",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Second Follow-up",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send First Follow-up": {
      "main": [
        [
          {
            "node": "Update First Follow-up",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Second Follow-up": {
      "main": [
        [
          {
            "node": "Update Second Follow-up",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}