{
  "name": "Bot / CRM Nurture Worker (welcome \u2192 followup chain)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 1
            }
          ]
        }
      },
      "name": "Cron every 1m",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT * FROM public.claim_jobs('n8n-crm-worker', 10, 'crm', 120);"
      },
      "name": "PG claim crm jobs",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        460,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "job_id",
              "value": "={{ $json.id }}"
            },
            {
              "name": "step",
              "value": "={{ $json.payload.step }}"
            },
            {
              "name": "phone",
              "value": "={{ $json.payload.phone }}"
            },
            {
              "name": "work_type",
              "value": "={{ $json.payload.work_type }}"
            },
            {
              "name": "city",
              "value": "={{ $json.payload.city }}"
            },
            {
              "name": "name",
              "value": "={{ $json.payload.name || '' }}"
            },
            {
              "name": "lead_id",
              "value": "={{ $json.payload.lead_id || '' }}"
            }
          ]
        }
      },
      "name": "Set step vars",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "string": [
                  {
                    "value1": "={{ .step }}",
                    "operation": "equals",
                    "value2": "welcome"
                  }
                ]
              }
            },
            {
              "conditions": {
                "string": [
                  {
                    "value1": "={{ .step }}",
                    "operation": "equals",
                    "value2": "followup_2h"
                  }
                ]
              }
            },
            {
              "conditions": {
                "string": [
                  {
                    "value1": "={{ .step }}",
                    "operation": "equals",
                    "value2": "followup_24h"
                  }
                ]
              }
            },
            {
              "conditions": {
                "string": [
                  {
                    "value1": "={{ .step }}",
                    "operation": "equals",
                    "value2": "followup_72h"
                  }
                ]
              }
            }
          ]
        }
      },
      "name": "Switch by step",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 2,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "tg_msg",
              "value": "\ud83d\udc4b {{$json.name || '\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435'}}! \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0437\u0430\u044f\u0432\u043a\u0443 \u043d\u0430 \u041f\u043e\u0434\u0440\u044f\u0434 PRO.\n\n\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0432\u0430\u0448 \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u00ab{{$json.work_type}}\u00bb \u0432 \u0433\u043e\u0440\u043e\u0434\u0435 {{$json.city || '\u041e\u043c\u0441\u043a'}}.\n\n\u041d\u0430\u0448 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0441\u0432\u044f\u0436\u0435\u0442\u0441\u044f \u0441 \u0432\u0430\u043c\u0438 \u0432 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0434\u043b\u044f \u0443\u0442\u043e\u0447\u043d\u0435\u043d\u0438\u044f \u0434\u0435\u0442\u0430\u043b\u0435\u0439."
            }
          ]
        }
      },
      "name": "Set welcome msg",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        1560,
        120
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "tg_msg",
              "value": "\u23f3 {{$json.name || '\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435'}}! \u0412\u0430\u0448\u0430 \u0437\u0430\u044f\u0432\u043a\u0430 \u043f\u0440\u0438\u043d\u044f\u0442\u0430 \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443.\n\n\u041d\u0430\u0448 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0441\u043a\u043e\u0440\u043e \u0441\u0432\u044f\u0436\u0435\u0442\u0441\u044f \u0441 \u0432\u0430\u043c\u0438. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0440\u043e\u0447\u043d\u044b\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b, \u043d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u043d\u0430\u043c."
            }
          ]
        }
      },
      "name": "Set 2h msg",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        1560,
        260
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "tg_msg",
              "value": "\ud83d\udccb {{$json.name || '\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435'}}! \u041d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u0432\u0430\u0448\u0430 \u0437\u0430\u044f\u0432\u043a\u0430 \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435.\n\n\u0425\u043e\u0442\u0438\u0442\u0435 \u0443\u0442\u043e\u0447\u043d\u0438\u0442\u044c \u0434\u0435\u0442\u0430\u043b\u0438 \u0438\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0437\u0430\u043a\u0430\u0437? \u041f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u0432\u0435\u0442\u044c\u0442\u0435 \u043d\u0430 \u044d\u0442\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435."
            }
          ]
        }
      },
      "name": "Set 24h msg",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        1560,
        400
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "tg_msg",
              "value": "\ud83d\udd14 {{$json.name || '\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435'}}! \u0412\u0441\u0451 \u0435\u0449\u0451 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u0430 \u0437\u0430\u044f\u0432\u043a\u0430 \u043f\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u00ab{{$json.work_type}}\u00bb?\n\n\u041e\u0442\u0432\u0435\u0442\u044c\u0442\u0435 \u00ab\u0434\u0430\u00bb \u2014 \u0438 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0441\u0432\u044f\u0436\u0435\u0442\u0441\u044f \u0441 \u0432\u0430\u043c\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e."
            }
          ]
        }
      },
      "name": "Set 72h msg",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        1560,
        540
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT bci.external_id as chat_id, bci.channel\nFROM public.bot_contacts bc\nJOIN public.bot_contact_identities bci ON bci.contact_id = bc.id AND bci.is_blocked = false\nWHERE bc.phone = '={{ ($json.phone || '').toString().replace(/'/g, \"''\") }}'\nLIMIT 1;"
      },
      "name": "PG lookup client chat",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        1780,
        200
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ .chat_id }}",
              "operation": "isNotEmpty"
            },
            {
              "value1": "={{ .channel }}",
              "operation": "equals",
              "value2": "telegram"
            }
          ],
          "options": {
            "combinator": "and"
          }
        }
      },
      "name": "Client has Telegram?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        2000,
        200
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $json.chat_id }}",
        "text": "={{ $json.tg_msg }}",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "name": "TG send to client",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.1,
      "position": [
        2220,
        120
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE public.job_queue SET status = 'completed', completed_at = now(), updated_at = now() WHERE id = '={{ ($json.job_id || '').toString().replace(/'/g, \"''\") }}'::uuid;"
      },
      "name": "PG complete job",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        2440,
        200
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Cron every 1m": {
      "main": [
        [
          {
            "node": "PG claim crm jobs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PG claim crm jobs": {
      "main": [
        [
          {
            "node": "Set step vars",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set step vars": {
      "main": [
        [
          {
            "node": "Switch by step",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch by step": {
      "main": [
        [
          {
            "node": "Set welcome msg",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set 2h msg",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set 24h msg",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set 72h msg",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set welcome msg": {
      "main": [
        [
          {
            "node": "PG lookup client chat",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set 2h msg": {
      "main": [
        [
          {
            "node": "PG lookup client chat",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set 24h msg": {
      "main": [
        [
          {
            "node": "PG lookup client chat",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set 72h msg": {
      "main": [
        [
          {
            "node": "PG lookup client chat",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PG lookup client chat": {
      "main": [
        [
          {
            "node": "Client has Telegram?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Client has Telegram?": {
      "main": [
        [
          {
            "node": "TG send to client",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "PG complete job",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "TG send to client": {
      "main": [
        [
          {
            "node": "PG complete job",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false,
  "tags": [
    "bot",
    "crm",
    "nurture"
  ]
}