{
  "name": "Citizen Service (Complaint Management) Automation System (Government)",
  "nodes": [
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list",
          "cachedResultName": "gpt-4o-mini"
        },
        "builtInTools": {},
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        352,
        256
      ],
      "id": "e54efb12-a9cd-4cad-b5fc-7a7972d9ee83",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "id": "zHTJ5AI7kBmTnuvv",
          "name": "InfyOm"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Citizen Request Webhook').item.json.body.From }}"
      },
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        512,
        256
      ],
      "id": "b24ba70f-9dd3-46d4-af4f-0b6ac727e634",
      "name": "Simple Memory"
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "citizen-request",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        0
      ],
      "id": "9614c12a-bb91-4735-9fc6-e405aac4398d",
      "name": "Citizen Request Webhook",
      "webhookId": "fa67272f-23fc-4a14-8823-a03826239c2a"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "53c9bc94-0be1-4502-9596-3bff8fd5059f",
              "name": "Body",
              "value": "={{ $json.body.Body }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        208,
        0
      ],
      "id": "e2f52d72-d10f-4efd-8e15-800ec16d6167",
      "name": "Extract WhatsApp Message"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.Body }}",
        "options": {
          "systemMessage": "Classify this complaint into:\n1. Category (water / electricity / road / garbage)\n2. Priority (low / medium / high)\n\nComplaint:\n{{ $json.description }}\n\nReturn JSON:\n{\n \"category\": \"\",\n \"priority\": \"\"\n}"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        416,
        0
      ],
      "id": "b6f8fa79-f308-4c7d-91c4-6c2356d1b290",
      "name": "AI Complaint Classifier"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "leftValue": "={{ JSON.parse($json.output).category }}",
                    "rightValue": "false",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "70932fbc-1a18-4c27-bf99-8ddeb92b0043"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Water Dept"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "867f6c41-d976-4158-98fa-83c6335123bd",
                    "leftValue": "={{ JSON.parse($json.output).category }}",
                    "rightValue": "electricity",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Electricity Dept"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "9ade3cac-5393-498b-b6c5-5d5dc5fbcdf7",
                    "leftValue": "={{ JSON.parse($json.output).category }}",
                    "rightValue": "garbage",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Sanitation Dept"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.4,
      "position": [
        768,
        -16
      ],
      "id": "f73fb947-f46f-40c2-ab72-d253d2c75ee3",
      "name": "Route to Department",
      "alwaysOutputData": false
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "3087101d-5e63-4418-8849-446ec7b5ba61",
              "name": "=department",
              "value": "Water Department",
              "type": "string"
            },
            {
              "id": "31f8e36c-0cdc-465e-95d2-5b27190abace",
              "name": "assigned_to",
              "value": "Water Officer A",
              "type": "string"
            },
            {
              "id": "68120776-0a73-4309-bcf1-fad0e0bb5f97",
              "name": "status",
              "value": "assigned",
              "type": "string"
            },
            {
              "id": "a25ae993-4f88-42d4-a5b3-550a69e21659",
              "name": "complaint_id",
              "value": "={{$now}}-{{ JSON.parse($json.output).category }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1120,
        -176
      ],
      "id": "fa917e8e-c1ca-4f26-af88-4b6eb84892f9",
      "name": "Assign Water Department"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "3087101d-5e63-4418-8849-446ec7b5ba61",
              "name": "=department",
              "value": "Electricity Department",
              "type": "string"
            },
            {
              "id": "31f8e36c-0cdc-465e-95d2-5b27190abace",
              "name": "assigned_to",
              "value": "Electricity Officer A",
              "type": "string"
            },
            {
              "id": "68120776-0a73-4309-bcf1-fad0e0bb5f97",
              "name": "status",
              "value": "assigned",
              "type": "string"
            },
            {
              "id": "e819fc0b-3a30-412b-b0f6-58773252cba3",
              "name": "complaint_id",
              "value": "={{$now}}-{{ JSON.parse($json.output).category }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1120,
        0
      ],
      "id": "c952a3e5-a278-4b77-8a7f-1badeb5ef4d3",
      "name": "Assign Electricity Department"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "3087101d-5e63-4418-8849-446ec7b5ba61",
              "name": "=department",
              "value": "Sanitation Department",
              "type": "string"
            },
            {
              "id": "31f8e36c-0cdc-465e-95d2-5b27190abace",
              "name": "assigned_to",
              "value": "Sanitation Officer A",
              "type": "string"
            },
            {
              "id": "68120776-0a73-4309-bcf1-fad0e0bb5f97",
              "name": "status",
              "value": "assigned",
              "type": "string"
            },
            {
              "id": "ce93af1b-a7fb-4a71-8ede-b0e97f22bdf0",
              "name": "complaint_id",
              "value": "={{$now}}-{{ JSON.parse($json.output).category }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1120,
        176
      ],
      "id": "2ad59d72-aa00-4960-80d2-1225d4e5775d",
      "name": "Assign Sanitation Department"
    },
    {
      "parameters": {
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "table": {
          "__rl": true,
          "value": "complaints",
          "mode": "list",
          "cachedResultName": "complaints"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "complaint_id": "={{ $json.complaint_id }}",
            "department": "={{ $json.department }}",
            "category": "={{ JSON.parse($('AI Complaint Classifier').item.json.output).category }}",
            "priority": "={{ JSON.parse($('AI Complaint Classifier').item.json.output).priority }}",
            "status": "={{ $json.status }}",
            "description": "={{ $('Citizen Request Webhook').item.json.body.Body }}"
          },
          "matchingColumns": [
            "id"
          ],
          "schema": [
            {
              "id": "id",
              "displayName": "id",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "complaint_id",
              "displayName": "complaint_id",
              "required": true,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "category",
              "displayName": "category",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "department",
              "displayName": "department",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "status",
              "displayName": "status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "priority",
              "displayName": "priority",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "description",
              "displayName": "description",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "created_at",
              "displayName": "created_at",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "phone",
              "displayName": "phone",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        1520,
        0
      ],
      "id": "e36d4c82-3930-41bf-a1b9-a0bfd32262df",
      "name": "Save Complaint to Database",
      "credentials": {
        "postgres": {
          "id": "itggcZOWl9tlVFAe",
          "name": "heet - Postgres account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const message = `Hello 👋\n\nYour complaint has been registered.\n\nComplaint ID: ${$input.first().json.complaint_id}\nCategory: ${$input.first().json.category}\nDepartment: ${$input.first().json.department}\n\nWe will update you soon.`;\n\nreturn {\n  output: {\n    payload: {\n      message\n    }\n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1728,
        0
      ],
      "id": "90735f07-aaa2-4b3d-a50b-c08cdd8658f2",
      "name": "Generate Confirmation Message"
    },
    {
      "parameters": {
        "from": "={{ $('Citizen Request Webhook').item.json.body.To }}",
        "to": "={{ $('Citizen Request Webhook').item.json.body.From }}",
        "message": "={{ $json.output.payload.message }}",
        "options": {}
      },
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        1936,
        0
      ],
      "id": "119ed22a-f252-4dc6-8317-aa7b549fee93",
      "name": "Send WhatsApp Notification",
      "credentials": {
        "twilioApi": {
          "id": "R6dWWTJFACSwxUGe",
          "name": "tarec16401 - twilio account"
        }
      }
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        0,
        640
      ],
      "id": "2b741bd8-cf34-445e-8803-d827fe6054ec",
      "name": "SLA Scheduler (Every 5 Minutes)"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT *\nFROM complaints\nWHERE status != 'resolved'",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        208,
        640
      ],
      "id": "ed671e23-4c5f-4dfe-8c0f-4a4f9ef77d22",
      "name": "Fetch Pending Complaints",
      "credentials": {
        "postgres": {
          "id": "itggcZOWl9tlVFAe",
          "name": "heet - Postgres account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "0585c5ce-7dec-4447-9ce7-c60431fcdd4c",
              "leftValue": "={{ new Date() - new Date($json.created_at) }}",
              "rightValue": 3600000,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        416,
        640
      ],
      "id": "7540dd07-ab38-4a09-a1da-0405df3df799",
      "name": "Check SLA Breach"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE complaints\nSET status = 'escalated'\nWHERE complaint_id = '{{ $json.complaint_id }}'",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        720,
        496
      ],
      "id": "2fae6f1a-4fed-4d57-90b8-f85e1c89a914",
      "name": "Mark Complaint as Escalated",
      "credentials": {
        "postgres": {
          "id": "itggcZOWl9tlVFAe",
          "name": "heet - Postgres account"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "c51c54e3-2e5c-4eb2-bbb4-ea3eff860530",
              "name": "assigned_to",
              "value": "Senior Officer",
              "type": "string"
            },
            {
              "id": "6879f56f-21d2-4a01-ae72-c2acd1c8ddad",
              "name": "status",
              "value": "escalated",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        928,
        496
      ],
      "id": "b4390a0e-ae47-42fd-a646-bc15202f6cf8",
      "name": "Assign Senior Officer"
    },
    {
      "parameters": {
        "jsCode": "const message = `⚠️ Alert!\n\nYour complaint ${$('Fetch Pending Complaints').first().json.complaint_id} has been escalated due to delay.\n\nWe are prioritizing it now.`;\n\nreturn {\n  output: {\n    payload: {\n      message\n    }\n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1136,
        496
      ],
      "id": "60689fc2-8481-425d-bb43-014d30cee20f",
      "name": "Generate Escalation Alert Message"
    },
    {
      "parameters": {
        "from": "=whatsapp:+14155238886",
        "to": "=whatsapp:+{{ $('Fetch Pending Complaints').first().json.phone }}",
        "message": "={{ $json.output.payload.message }}",
        "options": {}
      },
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        1344,
        496
      ],
      "id": "963beaf4-565f-4c40-8064-5a95529f1e28",
      "name": "Send Escalation WhatsApp Alert",
      "credentials": {
        "twilioApi": {
          "id": "R6dWWTJFACSwxUGe",
          "name": "tarec16401 - twilio account"
        }
      }
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        720,
        784
      ],
      "id": "4fa8eaf4-6e3d-4afb-b97c-ebd19d414fe5",
      "name": "No SLA Breach (Skip)"
    },
    {
      "parameters": {
        "content": "## Automated SLA Monitoring, Escalation & Notification System",
        "width": 304
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -352,
        624
      ],
      "id": "9d6484e5-c44a-47d0-a98b-3cc8d85d0bca",
      "name": "Sticky Note1"
    },
    {
      "parameters": {
        "content": "## Citizen Complaint Intake, Classification & Department Routing System",
        "width": 320
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -352,
        -16
      ],
      "id": "ede7bf8d-a3b5-4313-accc-e0a2aef31c41",
      "name": "Sticky Note"
    }
  ],
  "pinData": {},
  "connections": {
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Complaint Classifier",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Complaint Classifier",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Citizen Request Webhook": {
      "main": [
        [
          {
            "node": "Extract WhatsApp Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract WhatsApp Message": {
      "main": [
        [
          {
            "node": "AI Complaint Classifier",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Complaint Classifier": {
      "main": [
        [
          {
            "node": "Route to Department",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route to Department": {
      "main": [
        [
          {
            "node": "Assign Water Department",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Assign Electricity Department",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Assign Sanitation Department",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assign Water Department": {
      "main": [
        [
          {
            "node": "Save Complaint to Database",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assign Electricity Department": {
      "main": [
        [
          {
            "node": "Save Complaint to Database",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assign Sanitation Department": {
      "main": [
        [
          {
            "node": "Save Complaint to Database",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Complaint to Database": {
      "main": [
        [
          {
            "node": "Generate Confirmation Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Confirmation Message": {
      "main": [
        [
          {
            "node": "Send WhatsApp Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SLA Scheduler (Every 5 Minutes)": {
      "main": [
        [
          {
            "node": "Fetch Pending Complaints",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Pending Complaints": {
      "main": [
        [
          {
            "node": "Check SLA Breach",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check SLA Breach": {
      "main": [
        [
          {
            "node": "Mark Complaint as Escalated",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No SLA Breach (Skip)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mark Complaint as Escalated": {
      "main": [
        [
          {
            "node": "Assign Senior Officer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assign Senior Officer": {
      "main": [
        [
          {
            "node": "Generate Escalation Alert Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Escalation Alert Message": {
      "main": [
        [
          {
            "node": "Send Escalation WhatsApp Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "39bf05b1-deee-4616-a913-46eb7610c52a",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "9272721148ea09184b6bbb7ce6219dab088562dd450e2df8280d57c2e34c7d84"
  },
  "id": "jUVZdwdlY8vNbQRQ",
  "tags": []
}