{
  "name": "Premium / Recurring lawn mowing (cron)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 9 * * 1,3,5"
            }
          ]
        }
      },
      "name": "Cron (Mon/Wed/Fri 09:00)",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "select * from public.segment_for_recurring_lawn_mowing(12, 'recurring_mowing', null);"
      },
      "name": "Postgres (segment)",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        460,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "batchSize": 20,
        "options": {
          "reset": false
        }
      },
      "name": "SplitInBatches (20)",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "amount": 1,
        "unit": "seconds"
      },
      "name": "Wait (1s between msgs)",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.channel}}",
              "operation": "equals",
              "value2": "telegram"
            }
          ]
        }
      },
      "name": "IF (telegram?)",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "chatId": "={{$json.external_id}}",
        "text": "\ud83c\udf31 \u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435, {{$json.full_name || '\u0434\u0440\u0443\u0437\u044c\u044f'}}!\n\u0412\u0430\u0448 \u0433\u0430\u0437\u043e\u043d \u0432 {{$json.district || '\u041e\u043c\u0441\u043a\u0435'}} \u0443\u0436\u0435 \u043e\u0442\u0440\u043e\u0441 \u2014 \u043f\u043e\u0440\u0430 \u0441\u043d\u043e\u0432\u0430 \u043d\u0430 \u043f\u043e\u043a\u043e\u0441.\n\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0430\u0441 \u043d\u0430 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0435 \u0434\u043d\u0438? \u041e\u0442\u0432\u0435\u0442\u044c\u0442\u0435 \u00ab\u0434\u0430\u00bb \u0438\u043b\u0438 \u043d\u0430\u0436\u043c\u0438\u0442\u0435 /start.",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "name": "Telegram (send)",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.1,
      "position": [
        1340,
        200
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.MAX_API_URL}}/messages?access_token={{$env.MAX_BOT_TOKEN}}&user_id={{$json.external_id}}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={ \"text\": \"\ud83c\udf31 \u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435, {{$json.full_name || '\u0434\u0440\u0443\u0437\u044c\u044f'}}! \u041f\u043e\u0440\u0430 \u0441\u043d\u043e\u0432\u0430 \u043d\u0430 \u043f\u043e\u043a\u043e\u0441 \u2014 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0430\u0441?\" }"
      },
      "name": "HTTP (MAX send)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1340,
        400
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "insert into public.campaign_recipients (campaign_id, contact_id, channel, status, sent_at)\nselect c.id, '{{$json.contact_id}}'::uuid, '{{$json.channel}}'::messenger_channel, 'sent', now()\n  from public.campaigns c where c.code = 'recurring_mowing'\non conflict (campaign_id, contact_id, channel) do update set status='sent', sent_at=now();"
      },
      "name": "Postgres (mark sent)",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        1560,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Cron (Mon/Wed/Fri 09:00)": {
      "main": [
        [
          {
            "node": "Postgres (segment)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres (segment)": {
      "main": [
        [
          {
            "node": "SplitInBatches (20)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches (20)": {
      "main": [
        [
          {
            "node": "Wait (1s between msgs)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait (1s between msgs)": {
      "main": [
        [
          {
            "node": "IF (telegram?)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF (telegram?)": {
      "main": [
        [
          {
            "node": "Telegram (send)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP (MAX send)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram (send)": {
      "main": [
        [
          {
            "node": "Postgres (mark sent)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP (MAX send)": {
      "main": [
        [
          {
            "node": "Postgres (mark sent)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false,
  "tags": [
    "premium",
    "campaigns",
    "lawn"
  ]
}