{
  "name": "Aula-Agente-de-IA-1",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "37d2bdc5-0aa0",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -160,
        0
      ],
      "id": "d14f3157-cf56-44e4-aff8-9310a372ca3b",
      "name": "Webhook",
      "webhookId": "37d2bdc5-0aa0-4afe-959f-fe5001c6a3ce"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "6e15f616-8e17-461c-b609-05998e87b597",
              "name": "body.id",
              "value": "={{ $json.body.id }}",
              "type": "number"
            },
            {
              "id": "b7921dec-f01f-43a6-8531-63b552dcbc2d",
              "name": "body.messages[0].content",
              "value": "={{ $json.body.messages[0].content }}",
              "type": "string"
            },
            {
              "id": "38416516-e2f9-41f7-b178-336e96b678da",
              "name": "body.messages[0].account_id",
              "value": "={{ $json.body.messages[0].account_id }}",
              "type": "number"
            },
            {
              "id": "fa9302fa-af56-44de-bf29-200ac70134df",
              "name": "body.messages[0].conversation.contact_inbox.source_id",
              "value": "={{ $json.body.messages[0].conversation.contact_inbox.source_id }}",
              "type": "string"
            },
            {
              "id": "5f5b6db0-c4fd-401a-b1da-b596f72452fe",
              "name": "body.messages[0].message_type",
              "value": "={{ $json.body.messages[0].message_type }}",
              "type": "number"
            },
            {
              "id": "71839183-084a-48f5-8c20-6bf61697299b",
              "name": "body.custom_attributes.pausar_agente",
              "value": "={{ $json.body.custom_attributes.pausar_agente }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        48,
        0
      ],
      "id": "7f2c8f07-b934-4040-9118-1c667132816a",
      "name": "Edit Fields"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "70b55961-418d-47dc-8e4b-38cbf0512633",
              "leftValue": "={{ $json.body.messages[0].message_type }}",
              "rightValue": 1,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            },
            {
              "id": "f70ae64d-b639-45c1-a18d-ec17fee88a07",
              "leftValue": "={{ $json.body.custom_attributes.pausar_agente }}",
              "rightValue": "Desligado",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "or"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        256,
        0
      ],
      "id": "a3d2bd0c-1c7f-4a72-a384-337fa9f4747c",
      "name": "If",
      "alwaysOutputData": true
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        464,
        -96
      ],
      "id": "2860c1ee-b4e0-4a18-8b9e-fe62fb84af6d",
      "name": "No Operation, do nothing"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.body.messages[0].content }}",
        "options": {
          "systemMessage": ""
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [
        464,
        96
      ],
      "id": "53fc62bc-fae7-44f5-8b2e-d941c9b01b12",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-5.2",
          "mode": "list",
          "cachedResultName": "gpt-5.2"
        },
        "builtInTools": {},
        "options": {
          "temperature": 0.3
        }
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        336,
        304
      ],
      "id": "2f02eac1-38ac-47cb-b2d0-1b5f326b665d",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "id": "PE5pvfwpcD1p6z1Q",
          "name": "OpenAi account 2"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $json.body.messages[0].conversation.contact_inbox.source_id }}",
        "contextWindowLength": 15
      },
      "type": "@n8n/n8n-nodes-langchain.memoryPostgresChat",
      "typeVersion": 1.3,
      "position": [
        480,
        304
      ],
      "id": "fa8ce97a-4f99-4362-844c-0e6f2a8035a6",
      "name": "Postgres Chat Memory",
      "credentials": {
        "postgres": {
          "id": "m6SnSwLQciNHqcEV",
          "name": "Postgres Self"
        }
      }
    },
    {
      "parameters": {
        "description": "{\n  \"nome\": \"Michel\",\n  \"whatsapp\": \"5515998558323\",\n  \"carro_id\": \"CHE-001\",\n  \"dia\": \"sábado\",\n  \"turno\": \"tarde\",\n  \"horario\": \"13:30\",\n  \"obs\": \"Pediu foto. Perfil família.\"\n}",
        "workflowId": {
          "__rl": true,
          "value": "mUr8BtGLoUmjcgID",
          "mode": "list",
          "cachedResultUrl": "/workflow/mUr8BtGLoUmjcgID",
          "cachedResultName": "handoffLunaDemo"
        },
        "workflowInputs": {
          "mappingMode": "defineBelow",
          "value": {
            "nome": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('nome', `Acrescente o Nome do Cliente`, 'string') }}",
            "carro_id": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('carro_id', `Adcione o mesmo id da tool estoque.`, 'string') }}",
            "dia": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('dia', ``, 'string') }}",
            "turno": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('turno', ``, 'string') }}",
            "horario": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('horario', ``, 'string') }}",
            "obs": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('obs', ``, 'string') }}",
            "whatsapp": "={{ $json.body.messages[0].conversation.contact_inbox.source_id }}",
            "id": "={{ $json.body.id }}",
            "setor": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('setor', ``, 'string') }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "nome",
              "displayName": "nome",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "whatsapp",
              "displayName": "whatsapp",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "carro_id",
              "displayName": "carro_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "setor",
              "displayName": "setor",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "id",
              "displayName": "id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "dia",
              "displayName": "dia",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "turno",
              "displayName": "turno",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "horario",
              "displayName": "horario",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "obs",
              "displayName": "obs",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "canBeUsedToMatch": true,
              "type": "string",
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      },
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "typeVersion": 2.2,
      "position": [
        752,
        304
      ],
      "id": "e4f581a9-3696-4434-a056-02a2ca59e70b",
      "name": "handoff"
    },
    {
      "parameters": {
        "outputFieldName": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Output_Field_Name', `America/Sao_Paulo`, 'string') }}",
        "options": {}
      },
      "type": "n8n-nodes-base.dateTimeTool",
      "typeVersion": 2,
      "position": [
        624,
        304
      ],
      "id": "bcc945c3-ba6c-4e23-b2d8-153129067e72",
      "name": "Date & Time"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://app.zappronto.com.br/api/v1/accounts/1/conversations/{{ $('If').item.json.body.id }}/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "content",
              "value": "={{ $json.output }}"
            },
            {
              "name": "message_type",
              "value": "outgoing"
            },
            {
              "name": "private",
              "value": "false"
            },
            {
              "name": "content_type",
              "value": "text"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        816,
        96
      ],
      "id": "fc3fe6e5-85b1-4adf-a82c-90a18bbc189b",
      "name": "HTTP Request",
      "credentials": {
        "httpHeaderAuth": {
          "id": "kgGi1AYowGAi79Mx",
          "name": "AulaTeste2"
        }
      }
    }
  ],
  "pinData": {
    "Webhook": [
      {
        "json": {
          "headers": {
            "host": "webhook.digitalinterativo.com.br",
            "user-agent": "rest-client/2.1.0 (linux-musl x86_64) ruby/3.4.4p34",
            "content-length": "3443",
            "accept": "application/json",
            "accept-encoding": "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
            "content-type": "application/json",
            "x-forwarded-for": "10.0.0.2",
            "x-forwarded-host": "webhook.digitalinterativo.com.br",
            "x-forwarded-port": "443",
            "x-forwarded-proto": "https",
            "x-forwarded-server": "c1b36e56366a",
            "x-real-ip": "10.0.0.2"
          },
          "params": {},
          "query": {},
          "body": {
            "additional_attributes": {},
            "can_reply": true,
            "channel": "Channel::Whatsapp",
            "contact_inbox": {
              "id": 4,
              "contact_id": 3,
              "inbox_id": 2,
              "source_id": "5515998558323",
              "created_at": "2026-06-17T21:01:04.615Z",
              "updated_at": "2026-06-17T21:01:04.615Z",
              "hmac_verified": false,
              "pubsub_token": "jJM9G3QcztYQSoh3BMcgirRW"
            },
            "id": 107,
            "inbox_id": 2,
            "messages": [
              {
                "id": 2272,
                "content": "Michel Menezes",
                "account_id": 1,
                "inbox_id": 2,
                "conversation_id": 107,
                "message_type": 0,
                "created_at": 1783438859,
                "updated_at": "2026-07-07T15:40:59.222Z",
                "private": false,
                "status": "sent",
                "source_id": "wamid.HBgNNTUxNTk5ODU1ODMyMxUCABIYFjNFQjBFQ0Y4MkQyNjlCRDUwRDhFRUIA",
                "content_type": "text",
                "content_attributes": {},
                "sender_type": "Contact",
                "sender_id": 3,
                "external_source_ids": {},
                "additional_attributes": {},
                "processed_message_content": "Michel Menezes",
                "sentiment": {},
                "conversation": {
                  "assignee_id": 1,
                  "unread_count": 4,
                  "last_activity_at": 1783438859,
                  "contact_inbox": {
                    "source_id": "5515998558323"
                  }
                },
                "sender": {
                  "additional_attributes": {
                    "avatar_url_hash": "95f810677267923f4e05d9ac76eb855e9acf332e2de0911739a54b22b42cc133",
                    "last_avatar_sync_at": "2026-07-01T16:45:26Z"
                  },
                  "custom_attributes": {
                    "waha_whatsapp_jid": "5515998558323@c.us",
                    "waha_whatsapp_lid": "107804132143304@lid",
                    "waha_whatsapp_chat_id": "107804132143304@lid"
                  },
                  "email": null,
                  "id": 3,
                  "identifier": "5515998558323@s.whatsapp.net",
                  "name": "Digital Interativo",
                  "phone_number": "+5515998558323",
                  "thumbnail": "https://app.zappronto.com.br/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBcHNCIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--25d45b93da92488d33b07926196b05e7a454274b/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lJYW5CbkJqb0dSVlE2RTNKbGMybDZaVjkwYjE5bWFXeHNXd2RwQWZvdyIsImV4cCI6bnVsbCwicHVyIjoidmFyaWF0aW9uIn19--165c27d0d6a1d87fc52e7d404dfea38783ec6d44/660809009_1458656402622831_7208356604544671645_n.jpg",
                  "blocked": false,
                  "type": "contact"
                }
              }
            ],
            "labels": [],
            "meta": {
              "sender": {
                "additional_attributes": {
                  "avatar_url_hash": "95f810677267923f4e05d9ac76eb855e9acf332e2de0911739a54b22b42cc133",
                  "last_avatar_sync_at": "2026-07-01T16:45:26Z"
                },
                "custom_attributes": {
                  "waha_whatsapp_jid": "5515998558323@c.us",
                  "waha_whatsapp_lid": "107804132143304@lid",
                  "waha_whatsapp_chat_id": "107804132143304@lid"
                },
                "email": null,
                "id": 3,
                "identifier": "5515998558323@s.whatsapp.net",
                "name": "Digital Interativo",
                "phone_number": "+5515998558323",
                "thumbnail": "https://app.zappronto.com.br/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBcHNCIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--25d45b93da92488d33b07926196b05e7a454274b/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lJYW5CbkJqb0dSVlE2RTNKbGMybDZaVjkwYjE5bWFXeHNXd2RwQWZvdyIsImV4cCI6bnVsbCwicHVyIjoidmFyaWF0aW9uIn19--165c27d0d6a1d87fc52e7d404dfea38783ec6d44/660809009_1458656402622831_7208356604544671645_n.jpg",
                "blocked": false,
                "type": "contact"
              },
              "assignee": {
                "id": 1,
                "name": "Michel Menezes",
                "available_name": "Michel Menezes",
                "avatar_url": "",
                "type": "user",
                "availability_status": null,
                "thumbnail": ""
              },
              "assignee_type": "User",
              "team": null,
              "hmac_verified": false
            },
            "status": "open",
            "custom_attributes": {
              "pausar_agente": "Ligado"
            },
            "snoozed_until": null,
            "unread_count": 4,
            "first_reply_created_at": "2026-07-07T15:21:50.522Z",
            "priority": null,
            "waiting_since": 1783438859,
            "agent_last_seen_at": 1783437710,
            "contact_last_seen_at": 0,
            "last_activity_at": 1783438859,
            "timestamp": 1783438859,
            "created_at": 1783437693,
            "updated_at": 1783438859.2611892,
            "event": "automation_event.message_created"
          },
          "webhookUrl": "https://webhook.digitalinterativo.com.br/webhook/37d2bdc5-0aa0-4afe-959f-fe5001c6a3c7",
          "executionMode": "production"
        },
        "pairedItem": {
          "item": 0
        }
      }
    ]
  },
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Postgres Chat Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "handoff": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Date & Time": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "52604963-de1a-4a31-99ed-1c59948f5f43",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "80afdd69636a10d35bc8493e7495a6b23b2c0e05cb8f637894d88d9761df5e27"
  },
  "id": "AbN4n3A4Cx4tTjrd",
  "tags": []
}