{
  "name": "Workflow_HPP-ML",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "id": "b3528dec-3b54-4be8-a385-bc2a29bddddf",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1,
      "position": [
        -560,
        160
      ],
      "webhookUrl": "your-ngrok-url-here",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "// Code node: detectar modo (welcome / predict / error)\n// Lee correctamente tanto message.text (Telegram) como message directo.\nconst message = ($json?.message?.text ?? $json?.message ?? $json?.text ?? \"\").toString().trim();\n\nlet mode = \"\";\nconst m = message.toLowerCase();\n\n// /start -> welcome\nif (m === \"/start\") {\n  mode = \"welcome\";\n} \n// Predict -> exactamente 8 n\u00fameros separados por comas (acepta decimales con . o , y signos -)\nelse if (/^\\s*-?\\d+([.,]\\d+)?\\s*(,\\s*-?\\d+([.,]\\d+)?\\s*){7}$/.test(message)) {\n  mode = \"predict\";\n} \n// En cualquier otro caso -> error\nelse {\n  mode = \"error\";\n}\n\nreturn [{\n  json: {\n    chat_id: $json?.message?.chat?.id ?? $json?.chat_id ?? null,\n    message,\n    mode\n  }\n}];\n"
      },
      "name": "Code",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -384,
        160
      ],
      "id": "1520d31e-6a6d-462d-81ab-495f9d8c5fcd"
    },
    {
      "parameters": {
        "chatId": "={{$node[\"Switch\"].json[\"chat_id\"]}}",
        "text": "={{$node[\"HTTP Request\"].json[\"message_html\"]}}",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "name": "Send Prediction",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        464,
        -16
      ],
      "id": "2a9f05b6-055a-4eb6-9ae9-af222f9b9f0a",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{$json[\"chat_id\"]}}",
        "text": "=\ud83d\udc4b Welcome to the *Housing Price Predictor Bot*! \ud83c\udfe0\n\nTo get an estimated price for a house, please send the required data in this exact order, **separated by commas**:\n\n1\ufe0f\u20e3 MedInc \u2192 Median Income (in tens of thousands of dollars)  \n2\ufe0f\u20e3 HouseAge \u2192 Age of the house (in years)  \n3\ufe0f\u20e3 AveRooms \u2192 Average number of rooms  \n4\ufe0f\u20e3 AveBedrms \u2192 Average number of bedrooms  \n5\ufe0f\u20e3 Population \u2192 Population in the area  \n6\ufe0f\u20e3 AveOccup \u2192 Average household occupancy  \n7\ufe0f\u20e3 Latitude \u2192 Geographic latitude  \n8\ufe0f\u20e3 Longitude \u2192 Geographic longitude\n\n\ud83d\udccc Example (copy & paste):\n\n4.2,15,5.3,1.2,1800,3.1,34.05,-118.25\n\n\n\ud83d\udd39 Make sure to:\n- Use **dots** for decimals (e.g. `3.1`, not `3,1`)\n- Add no spaces between values\n- Send only numbers in the correct order\n\nOnce I receive your message in the correct format, I\u2019ll return an estimated price \ud83d\udcb5 using machine learning.\n\n",
        "additionalFields": {}
      },
      "name": "Send Welcome",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        64,
        176
      ],
      "id": "b959811d-030e-4358-9668-7b52f470c5a6",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{$json[\"chat_id\"]}}",
        "text": "=\u26a0\ufe0f Invalid input format.\n\nPlease send the data as **8 comma-separated numbers**, in the following order:\n\nMedInc,HouseAge,AveRooms,AveBedrms,Population,AveOccup,Latitude,Longitude\n\n\n\ud83d\udccc Correct example:\n4.2,15,5.3,1.2,1800,3.1,34.05,-118.25\n\n\n\ud83d\udd38 Make sure you:\n- Use periods for decimals (e.g. 2.5)\n- Include no spaces between values\n- Do not add extra text\n\nTry again when ready \u2014 I\u2019m here to help you predict housing prices! \ud83d\udcca\n\n\n\n",
        "additionalFields": {}
      },
      "name": "Send Error",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        64,
        384
      ],
      "id": "d37e1ff4-de3c-4041-96e7-c09507a366fc",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://50c8e604b988.ngrok-free.app/predict-from-string",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "=input",
              "value": "={{$json.message}}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        256,
        -16
      ],
      "id": "14fdab72-188a-42c1-b586-0a3c18c858be",
      "name": "HTTP Request"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "loose",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{$json[\"mode\"]}}",
                    "rightValue": "predict",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "e8d5ac5a-77da-4a32-b7a8-09a6ad1c5a5c"
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "loose",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "ce92b40e-5fa9-4f6b-86ed-b254a075a943",
                    "leftValue": "={{$json[\"mode\"]}}",
                    "rightValue": "welcome",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "loose",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "b55b564d-4fba-4d28-be11-6723485212c4",
                    "leftValue": "={{$json[\"mode\"]}}",
                    "rightValue": "error",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            }
          ]
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        -176,
        160
      ],
      "id": "a351c6ab-a444-4601-96e0-c64b8b6cf207",
      "name": "Switch"
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Send Prediction",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Welcome",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "828abecf-04e3-447e-9f09-af3d38fa947a",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "ii3pmL1eQc5YiCP8",
  "tags": []
}