{
  "name": "Telegram_n8n",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message",
          "callback_query"
        ],
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        1392,
        3184
      ],
      "id": "a182002b-a6ad-458c-a994-f462ebb6212e",
      "name": "Telegram Trigger",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "leftValue": "={{ !!$json.message.photo }}",
                    "rightValue": "",
                    "operator": {
                      "type": "boolean",
                      "operation": "true",
                      "singleValue": true
                    },
                    "id": "07896ef2-b07c-45c8-ba24-3f74d94295ce"
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "3fb4d5ca-a2fc-479a-8fab-901a70c36327",
                    "leftValue": "={{ !!$json.message.location }}",
                    "rightValue": "",
                    "operator": {
                      "type": "boolean",
                      "operation": "true",
                      "singleValue": true
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "1f65ea0f-5075-443b-9d82-67dae959f613",
                    "leftValue": "={{ $json.message.text }}",
                    "rightValue": "/ayuda",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "53f84e86-bdb3-4b53-b83a-a15a68ea3d09",
                    "leftValue": "={{ $json.message.text }}",
                    "rightValue": "/start",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.4,
      "position": [
        1616,
        2656
      ],
      "id": "ca0776a2-c4c5-441f-bdb2-ea0afcf9ad16",
      "name": "Switch"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "DELETE FROM reportes WHERE chat_id = {{ $json.message.chat.id }} AND estado = 'esperando_ubicacion';\nINSERT INTO reportes (chat_id, foto_id, estado, telegram_username)\nVALUES (\n  {{ $json.message.chat.id }},\n  '{{ $json.message.photo[$json.message.photo.length - 1].file_id }}',\n  'esperando_ubicacion',\n  '{{ ($json.message.from.username || $json.message.from.first_name || \"\").replace(/'/g, \"''\") }}'\n);",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        1840,
        2416
      ],
      "id": "edd7d773-9e31-4da9-8607-ded72f7cc77d",
      "name": "Foto",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE reportes \nSET latitud = {{ $json.message.location.latitude }},\n    longitud = {{ $json.message.location.longitude }},\n    estado = 'completado'\nWHERE chat_id = {{ $json.message.chat.id }} \n  AND estado = 'esperando_ubicacion'\nRETURNING *;",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        1840,
        2608
      ],
      "id": "44e68905-009e-4ab7-934f-4f0b013d2f3b",
      "name": "Ubicacion",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "file",
        "fileId": "={{ $node[\"Ubicacion\"].json.foto_id }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        2064,
        2608
      ],
      "id": "09db9e4c-e84c-42bd-9ada-6b75e1e7ce20",
      "name": "Descargar foto",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "\u00a1Foto recibida! \ud83d\udcf8 Ahora m\u00e1ndame tu ubicaci\u00f3n.",
        "replyMarkup": "inlineKeyboard",
        "inlineKeyboard": {
          "rows": [
            {
              "row": {
                "buttons": [
                  {
                    "text": "\ud83d\udeab Cancelar reporte",
                    "additionalFields": {
                      "callback_data": "cancelar_reporte"
                    }
                  }
                ]
              }
            }
          ]
        },
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        2064,
        2416
      ],
      "id": "e23813b6-8422-49e3-95f4-1f5e07864d95",
      "name": "foto recibida",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://vision_qro_ai:8000/predict",
        "sendBody": true,
        "contentType": "multipart-form-data",
        "bodyParameters": {
          "parameters": [
            {
              "parameterType": "formBinaryData",
              "name": "file",
              "inputDataFieldName": "data"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        2288,
        2608
      ],
      "id": "456cc178-2bcb-4aa4-a134-dca5c8aff712",
      "name": "HTTP Request"
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "=\u2705 *Reporte registrado* en el mapa de Vision Qro.\n\n{{ $node[\"HTTP Request\"].json.mensaje_usuario }}\n\n{{ $node[\"HTTP Request\"].json.categoria === 'pendiente' ? '\u26a0\ufe0f Necesitamos que clasifiques este objeto manualmente.' : '\u00bfEs correcto el an\u00e1lisis?' }}",
        "replyMarkup": "inlineKeyboard",
        "inlineKeyboard": {
          "rows": [
            {
              "row": {
                "buttons": [
                  {
                    "text": "={{ $node[\"HTTP Request\"].json.categoria === 'pendiente' ? '\u270d\ufe0f Clasificar manualmente' : '\u2705 Es correcto' }}",
                    "additionalFields": {
                      "callback_data": "={{ $node[\"HTTP Request\"].json.categoria === 'pendiente' ? 'conf_err' : 'conf_ok' }}"
                    }
                  },
                  {
                    "text": "={{ $node[\"HTTP Request\"].json.categoria === 'pendiente' ? '\ud83d\udeab Cancelar reporte' : '\u274c Hay un error' }}",
                    "additionalFields": {
                      "callback_data": "={{ $node[\"HTTP Request\"].json.categoria === 'pendiente' ? 'cancelar_reporte' : 'conf_err' }}"
                    }
                  }
                ]
              }
            }
          ]
        },
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        2736,
        2608
      ],
      "id": "b9037799-c3dd-4086-8c8f-5b9ababd730b",
      "name": "Reporte Completado",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "=\ud83e\udd16 Soporte de Vision Qro  \n\nPara generar un reporte, sigue estos pasos:   \n\n\n1. Env\u00eda una foto clara del problema (bache, basura, etc.) usando el icono del clip \ud83d\udcce en la secci\u00f3n de \"Galeria\".  \n\n2. Env\u00eda tu ubicaci\u00f3n actual usando nuevamente el icono del clip \ud83d\udcce en la secci\u00f3n de \"ubicaci\u00f3n\", enviar ubicaci\u00f3n selecionada. \u26a0\ufe0f No envies ubicaci\u00f3n en tiempo real \u26a0\ufe0f, env\u00eda la que aparece con color azul \ud83d\udfe6).      \n\n3. Espera el an\u00e1lisis de IA. Si la IA Acierta el analisis darle al bot\u00f3n \"\u2705 Es correcto\" y listo! ya se genero tu reporte, si no acierta darle al bot\u00f3n \"\u274c Hay un error\"\n\n4. En caso de haber un error puedes ingresar los datos manualmente siguendo las instrucciones del chatbot.\n\nSi te equivocas puedes darle al bot\u00f3n \"\ud83d\udeab Cancelar reporte\"",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1840,
        2800
      ],
      "id": "b6e4f51b-3998-4ef1-96db-b6151bd9c153",
      "name": "/ayuda",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "aa11bb22-cc33-dd44-ee55-ff6677889900",
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "cancelar_reporte",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "conf_ok",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "8d86f368-b6d0-482e-9991-0357d0bcc25c"
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "1ac45654-4f3c-4abc-b312-1a6e412688e4",
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "conf_err",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "0c0e40c4-4384-4ea6-a56b-061be53c6855",
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "fix_bache",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "6071bd89-b4b8-4687-bef1-097b288724d8",
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "fix_basura",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "58c87f48-e1fd-4606-b0ad-7c41483e52a3",
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "size_p",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "5155c3b6-7582-4262-922d-0dae29010c49",
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "size_m",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "429319e8-a828-4a47-b7e1-35a92dc3fdf1",
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "size_g",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "9d23dc1a-fb62-4e4c-98d7-cbea89e5edf9",
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "mat_org",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "30c3b5a8-fd61-481d-b12a-88a449109ac8",
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "mat_inorg",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "8d37b308-839e-451a-8fc4-b1c267cceee5",
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "mat_otro",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "1da85689-96fd-4d88-bb77-fd8a3ca8e9ef",
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "mat_bolsa",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "174a428d-ccb2-417b-acb9-86c4e5215354",
                    "leftValue": "={{ $json.callback_query.data }}",
                    "rightValue": "cancelar_reporte_inline",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.4,
      "position": [
        1616,
        3504
      ],
      "id": "69a724f3-2478-443a-b6a7-3d6dba2f987b",
      "name": "Switch1"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE reportes \nSET estado = 'validado' \nWHERE chat_id = {{ $json.callback_query.message.chat.id }} \nAND estado = 'completado';",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        1840,
        3184
      ],
      "id": "48d7f751-d077-49aa-aae8-9176c7ed23ef",
      "name": "Validado",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').first().json.message?.chat.id || $('Telegram Trigger').first().json.callback_query?.message.chat.id }}",
        "text": "\u00a1Gracias por validar!",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        2064,
        3184
      ],
      "id": "bd794901-31ac-4977-a8c0-243606ca0e4b",
      "name": "confirmacion",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE reportes \nSET estado = 'esperando_nombre' \nWHERE chat_id = {{ $json.callback_query.message.chat.id }} \nAND estado = 'completado';",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        1840,
        3376
      ],
      "id": "6c855655-5e9a-4467-8aee-538aa915422f",
      "name": "Correcion",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').first().json.message?.chat.id || $('Telegram Trigger').first().json.callback_query?.message.chat.id }}",
        "text": "Perfecto. Ahora, \u00bfqu\u00e9 es lo que aparece realmente en la foto?",
        "replyMarkup": "inlineKeyboard",
        "inlineKeyboard": {
          "rows": [
            {
              "row": {
                "buttons": [
                  {
                    "text": "\ud83d\udd73\ufe0f Bache",
                    "additionalFields": {
                      "callback_data": "fix_bache"
                    }
                  },
                  {
                    "text": "\ud83d\uddd1\ufe0f Basura",
                    "additionalFields": {
                      "callback_data": "fix_basura"
                    }
                  }
                ]
              }
            }
          ]
        },
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        2288,
        2992
      ],
      "id": "11492260-03c1-4dcd-afb9-cd2acd83082b",
      "name": "solicitar clasificacion",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').first().json.message?.chat.id || $('Telegram Trigger').first().json.callback_query?.message.chat.id }}",
        "text": "\u00bfCu\u00e1l es la categor\u00eda del reporte de basura?",
        "replyMarkup": "inlineKeyboard",
        "inlineKeyboard": {
          "rows": [
            {
              "row": {
                "buttons": [
                  {
                    "text": "\ud83c\udf4e Org\u00e1nico",
                    "additionalFields": {
                      "callback_data": "mat_org"
                    }
                  },
                  {
                    "text": "\u267b\ufe0f Inorg\u00e1nico",
                    "additionalFields": {
                      "callback_data": "mat_inorg"
                    }
                  },
                  {
                    "text": "\ud83d\uddd1\ufe0f Bolsa de basura",
                    "additionalFields": {
                      "callback_data": "mat_bolsa"
                    }
                  },
                  {
                    "text": "\ud83c\udfd7\ufe0f Otros (Escombro/Animal)",
                    "additionalFields": {
                      "callback_data": "mat_otro"
                    }
                  }
                ]
              }
            },
            {
              "row": {
                "buttons": [
                  {
                    "text": "\ud83d\udeab Cancelar reporte",
                    "additionalFields": {
                      "callback_data": "cancelar_reporte_inline"
                    }
                  }
                ]
              }
            }
          ]
        },
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1840,
        3952
      ],
      "id": "fb5ea3fc-1366-4028-92c4-ab9809f04774",
      "name": "Categoria basura",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').first().json.message?.chat.id || $('Telegram Trigger').first().json.callback_query?.message.chat.id }}",
        "text": "=\u00bfDe qu\u00e9 tama\u00f1o es el bache?",
        "replyMarkup": "inlineKeyboard",
        "inlineKeyboard": {
          "rows": [
            {
              "row": {
                "buttons": [
                  {
                    "text": "Peque\u00f1o",
                    "additionalFields": {
                      "callback_data": "size_p"
                    }
                  },
                  {
                    "text": "Mediano",
                    "additionalFields": {
                      "callback_data": "size_m"
                    }
                  },
                  {
                    "text": "Grande",
                    "additionalFields": {
                      "callback_data": "size_g"
                    }
                  }
                ]
              }
            },
            {
              "row": {
                "buttons": [
                  {
                    "text": "\ud83d\udeab Cancelar reporte",
                    "additionalFields": {
                      "callback_data": "cancelar_reporte_inline"
                    }
                  }
                ]
              }
            }
          ]
        },
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1840,
        3760
      ],
      "id": "3c69b02b-3262-44bc-9923-a3e05283fc46",
      "name": "Tama\u00f1o bache",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE reportes \nSET subclase = '{{ $json.callback_query.data.split(\"_\")[1] }}', \n    estado = 'corregido_por_humano',\n    confianza = NULL,\n    telegram_username = '{{ ($json.callback_query.from.username || $json.callback_query.from.first_name || \"\").replace(/'/g, \"''\") }}'\nWHERE chat_id = {{ $json.callback_query.message.chat.id }} \nAND (estado = 'pendiente_correccion' OR estado = 'en_subclasificacion');",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        1840,
        4144
      ],
      "id": "6c0554a8-e598-427f-b972-c5b479c6af95",
      "name": "Correcion1",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').first().json.message?.chat.id || $('Telegram Trigger').first().json.callback_query?.message.chat.id }}",
        "text": "\u00a1Gracias por validar! ",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        2064,
        4144
      ],
      "id": "ee1f3654-676b-4583-bd3a-7216891c2917",
      "name": "confirmacion1",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE reportes\nSET clase_corregida = '{{ ($node[\"HTTP Request\"].json.clase || \"\").replace(/'/g, \"''\") }}',\n    confianza        = {{ $node[\"HTTP Request\"].json.confianza }},\n    subclase         = NULLIF('{{ $node[\"HTTP Request\"].json.categoria }}', 'pendiente'),\n    telegram_username = '{{ ($(\"Telegram Trigger\").item.json.message.from.username || $(\"Telegram Trigger\").item.json.message.from.first_name || \"\").replace(/'/g, \"''\") }}',\n    foto_url         = CASE\n        WHEN '{{ $node[\"Descargar foto\"].json.file_path }}' NOT IN ('undefined','null','')\n        THEN '{{ $node[\"Descargar foto\"].json.file_path }}'\n        ELSE foto_id\n    END\nWHERE chat_id = {{ $(\"Telegram Trigger\").item.json.message.chat.id }}\n  AND estado = 'completado';",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        2512,
        2608
      ],
      "id": "a5b72c36-710f-4cdd-9071-f55a9f55e2f3",
      "name": "Guardar resultado",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "DELETE FROM reportes\nWHERE chat_id = {{ $json.callback_query.message.chat.id }}\n  AND estado NOT IN ('validado', 'corregido_por_humano');\nSELECT 'cancelado' AS resultado;",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        1840,
        3568
      ],
      "id": "b22f1c98-b320-479c-8893-88d6bc887dcc",
      "name": "Cancelar inline",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').first().json.message?.chat.id || $('Telegram Trigger').first().json.callback_query?.message.chat.id }}",
        "text": "\ud83d\udeab Reporte cancelado. Puedes enviar una nueva foto cuando quieras.",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        2064,
        3568
      ],
      "id": "9307f36a-f388-4eea-8378-a4f08fbd3db5",
      "name": "Mensaje cancelar inline",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').first().json.message?.chat.id || $('Telegram Trigger').first().json.callback_query?.message.chat.id }}",
        "text": "\u00a1Entendido!\nPor favor, escribe el **nombre del objeto** que aparece en la foto:",
        "replyMarkup": "inlineKeyboard",
        "inlineKeyboard": {
          "rows": [
            {
              "row": {
                "buttons": [
                  {
                    "text": "\ud83d\udeab Cancelar reporte",
                    "additionalFields": {
                      "callback_data": "cancelar_reporte_inline"
                    }
                  }
                ]
              }
            }
          ]
        },
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        2064,
        3376
      ],
      "id": "63220283-fc66-428f-b374-6677b39f72c6",
      "name": "Solicitar nombre objeto",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT id FROM reportes WHERE chat_id = {{ $json.message?.chat?.id || 0 }} AND estado = 'esperando_nombre' LIMIT 1;",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        1840,
        2992
      ],
      "id": "a6eb30d7-0ceb-429f-b191-3de58cd3f28a",
      "name": "Check estado nombre",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE reportes SET clase_corregida = '{{ ($(\"Telegram Trigger\").item.json.message.text || \"\").replace(/'/g, \"''\") }}', estado = 'pendiente_correccion', telegram_username = '{{ ($(\"Telegram Trigger\").item.json.message.from.username || $(\"Telegram Trigger\").item.json.message.from.first_name || \"\").replace(/'/g, \"''\") }}' WHERE chat_id = {{ $(\"Telegram Trigger\").item.json.message.chat.id }} AND estado = 'esperando_nombre';",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        2064,
        2992
      ],
      "id": "2fa6ca88-ffb5-47eb-94a1-d222602b2fc8",
      "name": "Update nombre objeto",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          },
          {
            "node": "Switch1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Foto",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Ubicacion",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "/ayuda",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "/ayuda",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Check estado nombre",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ubicacion": {
      "main": [
        [
          {
            "node": "Descargar foto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Foto": {
      "main": [
        [
          {
            "node": "foto recibida",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Descargar foto": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Guardar resultado",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch1": {
      "main": [
        [
          {
            "node": "Cancelar inline",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Validado",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Correcion",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Tama\u00f1o bache",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Categoria basura",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Correcion1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Correcion1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Correcion1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Correcion1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Correcion1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Correcion1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Correcion1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Cancelar inline",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validado": {
      "main": [
        [
          {
            "node": "confirmacion",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Correcion": {
      "main": [
        [
          {
            "node": "Solicitar nombre objeto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Correcion1": {
      "main": [
        [
          {
            "node": "confirmacion1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Guardar resultado": {
      "main": [
        [
          {
            "node": "Reporte Completado",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cancelar inline": {
      "main": [
        [
          {
            "node": "Mensaje cancelar inline",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check estado nombre": {
      "main": [
        [
          {
            "node": "Update nombre objeto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update nombre objeto": {
      "main": [
        [
          {
            "node": "solicitar clasificacion",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "262b5e19-13e1-492e-829c-7975f9db5f2a",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodeGroups": [],
  "id": "E5TJDRpN0yJDD5c2",
  "tags": []
}