{
  "name": "Memorize Ghost Alerts",
  "nodes": [
    {
      "name": "Cron Trigger",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        250,
        300
      ],
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "hour": 9,
              "minute": 0
            }
          ]
        }
      }
    },
    {
      "name": "Query Neon DB",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        450,
        300
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT t.id, t.title, t.retrievability, t.subject_type, u.telegram_chat_id FROM topics t JOIN users u ON t.user_id = u.id WHERE t.retrievability < 0.25 AND t.next_review <= NOW()",
        "additionalFields": {}
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "name": "Filter Users With Telegram",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        300
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.telegram_chat_id }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      }
    },
    {
      "name": "Send Telegram Alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        850,
        300
      ],
      "parameters": {
        "chatId": "={{ $json.telegram_chat_id }}",
        "text": "\ud83e\udde0 *Memorize Alert*\n\nYou're losing: *{{ $json.title }}*\nRetention: {{ Math.round($json.retrievability * 100) }}%\n\nReady for a 60-second save? \ud83d\udc49 [Open Memorize](https://memorize.yourdomain.com/topic/{{ $json.id }})",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Cron Trigger": {
      "main": [
        [
          {
            "node": "Query Neon DB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Query Neon DB": {
      "main": [
        [
          {
            "node": "Filter Users With Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Users With Telegram": {
      "main": [
        [
          {
            "node": "Send Telegram Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}