{
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        -832,
        -208
      ],
      "id": "9b38e520-23e8-4908-a7fb-bc5f2fe23b1a",
      "name": "Telegram Trigger",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "78f86cfc-21f6-4a5d-9d71-5c130d380835",
              "leftValue": "={{ $json.message.text }}",
              "rightValue": "/start",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -368,
        -208
      ],
      "id": "6b709356-42da-4b5b-bd25-37c040689dcf",
      "name": "Mensagem n\u00e3o \u00e9 de Consulta?"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "00ae4a99-bf39-49e2-a253-d6b235708421",
              "leftValue": "={{ $json.cod }}",
              "rightValue": 200,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        1072,
        208
      ],
      "id": "465986b7-05ce-4911-b2f1-74c6cf2e17d2",
      "name": "Cidade Existe?"
    },
    {
      "parameters": {
        "mode": "raw",
        "jsonOutput": "={\n  \"nomeCidade\":  \"{{ $json.message.text }}\",\n  \"nomeCliente\": \"{{ $json.message.from.first_name }}\"\n}\n",
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -80,
        96
      ],
      "id": "09d60f96-2bb6-4598-856c-34b80c6faa14",
      "name": "Edit Fields"
    },
    {
      "parameters": {
        "jsCode": "// Captura a string de entrada\nconst pais = 'BR'\nlet rawText =   $input.first().json.nomeCidade || \"\"; \n\n// 1. Separa Cidade e Estado\nlet partes = rawText.split(',').map(s => s.trim());\nlet cidade = partes[0];\nlet estado = partes[1] ? partes[1].toUpperCase() : \"\";\n\n// 2. Monta a string no padr\u00e3o\nlet combinada = `${cidade},${estado},${pais}`;\n\nreturn {\n  query_clima: combinada,\n  cidade_limpa: cidade,\n  estado_limpo: estado\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        384,
        208
      ],
      "id": "0d4f6eb1-94ce-4ba5-987b-c072cea650b3",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "key",
              "value": "={{ $env[\"GEMINI_KEY\"] }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "= {\n  \"contents\": [{\n    \"parts\":[{\n      \"text\": \"<tom> Escreva uma mensagem personalizada sobre o clima para o cliente com o tom animado, amigavel e informativo e use emojis e leve em considera\u00e7\u00e3o o hor\u00e1rio da requisi\u00e7\u00e3o para criar a mensagem adequada</tom> <dados>  use os dados abaixo:\\n\\nNome da cidade: {{ $json.name }}\\nTemperatura:  {{ Math.round($json.main.temp)  }}C\\nDescri\u00e7\u00e3o do clima: {{ $json.weather[0].description }}, Nome da pessoa: {{ $('Edit Fields').item.json.nomeCliente }} </dados> <instrucoesTexto> .Divida o texto em dois, primeiro a informa\u00e7\u00e3o da temperatura com o nome da pessoa, depois quebre a linha e siga com o texto.  retorne a mensagem direto, sem a formata\u00e7\u00e3o json no fim. Deixe a mensagem formatada. a mensagem final \u00e9 o Telegram, leve em considera\u00e7\u00e3o as regras de exibi\u00e7\u00e3o, \\\\n, \\\\t nao aparecer. </instrucoesTexto>\"\n    }]\n  }],\n\"generationConfig\": {\n    \"temperature\": 0.2,\n    \"topK\": 40,\n    \"topP\": 0.95,\n    \"maxOutputTokens\": 10192,\n    \"responseMimeType\": \"text/plain\"\n  }\n}\n",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        1488,
        192
      ],
      "id": "0f6d86f2-1e97-4113-a1af-da88264efeca",
      "name": "Gemini",
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "url": "https://api.openweathermap.org/data/2.5/weather",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "q",
              "value": "={{ $json.query_clima }}"
            },
            {
              "name": "units",
              "value": "metric"
            },
            {
              "name": "lang",
              "value": "pt_br"
            },
            {
              "name": "appid",
              "value": "={{ $env[\"OPENWEATHER_KEY\"] }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        736,
        208
      ],
      "id": "11f43f81-232f-4719-92a5-119dc49c5658",
      "name": "Api Clima",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.candidates[0].content.parts[0].text }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        2368,
        48
      ],
      "id": "dd1c0e75-d6cd-43c5-97e9-701d619579d0",
      "name": "Mandar Mensagem Formatada Sucesso!",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "=\u2600\ufe0f Ol\u00e1 {{ $('Edit Fields').item.json.nomeCliente }}. A temperatura em {{ $('Api Clima').item.json.name }} \u00e9 de {{ Math.round($('Api Clima').item.json.main.temp)  }}\u00b0C .",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        2080,
        480
      ],
      "id": "2defaa0d-e049-43ff-b0ee-c1f64eaf7bd2",
      "name": "Mandar Mensagem Formatada Sucesso-Simplificada",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "=\u274c Ol\u00e1 {{ $('Telegram Trigger').item.json.message.chat.first_name }}! cidade n\u00e3o encontrada, tente mandar exemplo: Santo Andr\u00e9, SP",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        816,
        1040
      ],
      "id": "5d82e108-9d18-4b23-b1a9-e8a4334c8961",
      "name": "Mandar Mensagem Erroi1",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.nomeCliente }}, consulta em processamento...",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        368,
        -64
      ],
      "id": "4e913207-2a25-4e48-9a22-44590e6a869e",
      "name": "Mandar-Mensagem-Processamento",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $json.message.chat.id }}",
        "text": "=Ol\u00e1 {{ $('Telegram Trigger').item.json.message.chat.first_name }} Seja bem vindo(a) ao Rob\u00f4 do Clima \u2600\ufe0f, mande a mensagem no formato exemplo: \"Santo Andr\u00e9, SP\" para come\u00e7ar",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        752,
        -224
      ],
      "id": "87a7fcc2-dc56-451a-88fd-bd36ff4cd67c",
      "name": "Mandar-Mensagem-Inicial",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "4e095d9f-6e29-42ee-bad5-4d2f19d8e540",
              "leftValue": "={{ /^.+, ?[A-Z]{2}$/i.test($json.message.text.trim()) && ['AC', 'AL', 'AP', 'AM', 'BA', 'CE', 'DF', 'ES', 'GO', 'MA', 'MT', 'MS', 'MG', 'PA', 'PB', 'PR', 'PE', 'PI', 'RJ', 'RN', 'RS', 'RO', 'RR', 'SC', 'SP', 'SE', 'TO'].includes($json.message.text.trim().slice(-2).toUpperCase()) }}",
              "rightValue": "=true",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -656,
        496
      ],
      "id": "8a8f0570-29e9-4029-9805-ae3f313c176b",
      "name": "Mensagem est\u00e1 no formato Valido?"
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Mensagem n\u00e3o \u00e9 de Consulta?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mensagem n\u00e3o \u00e9 de Consulta?": {
      "main": [
        [
          {
            "node": "Mandar-Mensagem-Inicial",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mensagem est\u00e1 no formato Valido?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cidade Existe?": {
      "main": [
        [
          {
            "node": "Gemini",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mandar Mensagem Erroi1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Mandar-Mensagem-Processamento",
            "type": "main",
            "index": 0
          },
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Api Clima",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini": {
      "main": [
        [
          {
            "node": "Mandar Mensagem Formatada Sucesso!",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mandar Mensagem Formatada Sucesso-Simplificada",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Api Clima": {
      "main": [
        [
          {
            "node": "Cidade Existe?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mensagem est\u00e1 no formato Valido?": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mandar Mensagem Erroi1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "meta": {
    "templateCredsSetupCompleted": true
  }
}