{
  "name": "DzRouter · 07 · Commandes WhatsApp/Messenger → tableau (COD)",
  "nodes": [
    {
      "id": "2eee4fe9-af25-5224-a851-bf4ca0ae73cc",
      "name": "Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -520,
        -260
      ],
      "parameters": {
        "content": "## Commandes COD (58 wilayas)\n\n1. Installez le nœud communautaire **n8n-nodes-dzrouter** (Paramètres → Community nodes).\n2. Ouvrez chaque nœud DzRouter et choisissez votre identifiant **DzRouter API** (clé `sk-dz-…`).\n3. Cliquez **Test workflow** : un exemple algérien est déjà chargé.\n\nPuis connectez **Google Sheets** (colonnes : date, nom, telephone, wilaya_code, wilaya, commune, produit, quantite, taille, couleur, livraison, message) et **activez** le nœud.\n\n---\n\n<div dir=\"rtl\">\n\n### طلبيات الدفع عند الاستلام\n\n1. ثبّت العقدة **n8n-nodes-dzrouter** (الإعدادات ← Community nodes).\n2. افتح كل عقدة DzRouter واختر بيانات اعتماد **DzRouter API** (مفتاح `sk-dz-…`).\n3. اضغط **Test workflow**: مثال جزائري جاهز للتجربة.\n\n</div>\n\n---\n\n### COD orders\n\n1. Install the **n8n-nodes-dzrouter** community node (Settings → Community nodes).\n2. Open each DzRouter node and pick your **DzRouter API** credential (`sk-dz-…` key).\n3. Click **Test workflow**: an Algerian sample is already loaded.",
        "height": 640,
        "width": 440,
        "color": 4
      }
    },
    {
      "id": "7638086a-ae8a-5a1c-9e28-d9a5e51d3f8f",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        0
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "commande",
        "responseMode": "responseNode",
        "options": {}
      }
    },
    {
      "id": "4cc3574f-943f-5f9b-9ae3-2f594e440a15",
      "name": "Classify",
      "type": "n8n-nodes-dzrouter.dzRouter",
      "typeVersion": 1,
      "position": [
        260,
        0
      ],
      "parameters": {
        "resource": "text",
        "operation": "classify",
        "model": {
          "__rl": true,
          "mode": "id",
          "value": "openai/gpt-4o-mini"
        },
        "text": "={{ $json.body.message }}",
        "categories": "order, question, complaint",
        "options": {
          "fallback": "other",
          "instructions": "« order » = le client veut acheter ou passer commande, même en darja (nheb nchri, n7ab nkoumandi, حابة نطلب)."
        }
      }
    },
    {
      "id": "61953f2e-0f72-57cd-8c89-2c33b1acb6a1",
      "name": "Is it an order?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        520,
        0
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "order",
              "leftValue": "={{ $json.category }}",
              "rightValue": "order",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      }
    },
    {
      "id": "14a85ee8-6d27-5aa2-9dd7-eceaa0ad1d4e",
      "name": "Extract order",
      "type": "n8n-nodes-dzrouter.dzRouter",
      "typeVersion": 1,
      "position": [
        780,
        0
      ],
      "parameters": {
        "resource": "text",
        "operation": "extract",
        "model": {
          "__rl": true,
          "mode": "id",
          "value": "openai/gpt-4o-mini"
        },
        "text": "={{ $('Webhook').item.json.body.message }}",
        "fields": {
          "values": [
            {
              "name": "nom",
              "type": "string",
              "description": "Prénom et nom du client"
            },
            {
              "name": "telephone",
              "type": "string",
              "description": "Numéro de téléphone algérien tel qu'écrit"
            },
            {
              "name": "wilaya",
              "type": "string",
              "description": "Wilaya de livraison, nom officiel en français (ex : Alger, Sétif, Tizi Ouzou)"
            },
            {
              "name": "commune",
              "type": "string",
              "description": "Commune ou quartier"
            },
            {
              "name": "produit",
              "type": "string",
              "description": "Produit commandé"
            },
            {
              "name": "quantite",
              "type": "number",
              "description": "Quantité, 1 si non précisée"
            },
            {
              "name": "taille",
              "type": "string",
              "description": "Taille ou pointure"
            },
            {
              "name": "couleur",
              "type": "string",
              "description": "Couleur"
            },
            {
              "name": "livraison",
              "type": "string",
              "description": "« domicile » ou « stop desk »"
            }
          ]
        },
        "options": {
          "instructions": "Le message peut être en darja (lettres latines ou arabes), en français ou en arabe."
        }
      }
    },
    {
      "id": "4a1cab80-685f-5ff0-9dc2-9fd4798948a3",
      "name": "Clean phone & wilaya",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1040,
        0
      ],
      "parameters": {
        "jsCode": "const WILAYAS = [\"Adrar\", \"Chlef\", \"Laghouat\", \"Oum El Bouaghi\", \"Batna\", \"Béjaïa\", \"Biskra\", \"Béchar\", \"Blida\", \"Bouira\", \"Tamanrasset\", \"Tébessa\", \"Tlemcen\", \"Tiaret\", \"Tizi Ouzou\", \"Alger\", \"Djelfa\", \"Jijel\", \"Sétif\", \"Saïda\", \"Skikda\", \"Sidi Bel Abbès\", \"Annaba\", \"Guelma\", \"Constantine\", \"Médéa\", \"Mostaganem\", \"M'Sila\", \"Mascara\", \"Ouargla\", \"Oran\", \"El Bayadh\", \"Illizi\", \"Bordj Bou Arréridj\", \"Boumerdès\", \"El Tarf\", \"Tindouf\", \"Tissemsilt\", \"El Oued\", \"Khenchela\", \"Souk Ahras\", \"Tipaza\", \"Mila\", \"Aïn Defla\", \"Naâma\", \"Aïn Témouchent\", \"Ghardaïa\", \"Relizane\", \"Timimoun\", \"Bordj Badji Mokhtar\", \"Ouled Djellal\", \"Béni Abbès\", \"In Salah\", \"In Guezzam\", \"Touggourt\", \"Djanet\", \"El M'Ghair\", \"El Meniaa\"];\nconst norm = (s) => String(s ?? '').normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase().replace(/[^a-z]/g, '');\nconst ALIASES = { algiers: 16, alger: 16, eldjazair: 16, algeria: 16, setif: 19, tiziouzou: 15, bejaia: 6, bougie: 6, msila: 28, oran: 31, constantine: 25 };\nconst o = $input.first().json;\nconst msg = $('Webhook').first().json.body;\nlet digits = String(o.telephone ?? '').replace(/\\D/g, '');\nif (digits.startsWith('213')) digits = '0' + digits.slice(3);\nconst phoneOk = /^0[567]\\d{8}$/.test(digits) || /^0[234]\\d{7,8}$/.test(digits);\nconst key = norm(o.wilaya);\nlet code = WILAYAS.findIndex((w) => norm(w) === key) + 1 || ALIASES[key] || null;\nreturn [{ json: {\n  date: new Date().toISOString(), nom: o.nom, telephone: phoneOk ? '+213' + digits.slice(1) : o.telephone, telephone_valide: phoneOk,\n  wilaya_code: code, wilaya: code ? WILAYAS[code - 1] : o.wilaya, commune: o.commune, produit: o.produit, quantite: o.quantite ?? 1,\n  taille: o.taille, couleur: o.couleur, livraison: o.livraison, message: msg.message, source: msg.from,\n} }];"
      }
    },
    {
      "id": "f2529d2d-c999-5d70-9c90-9699659dc269",
      "name": "Add to Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1300,
        0
      ],
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Commandes"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "disabled": true
    },
    {
      "id": "82d57c67-e4de-5faa-989a-08f4315e61b9",
      "name": "Reply: order",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        1560,
        0
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ status: 'order', order: $('Clean phone & wilaya').item.json }) }}",
        "options": {}
      }
    },
    {
      "id": "83439140-2a32-5dc7-81f3-33435fbf4600",
      "name": "Reply: other",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        1040,
        220
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ status: $('Classify').item.json.category, message: $('Webhook').item.json.body.message }) }}",
        "options": {}
      }
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Classify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Classify": {
      "main": [
        [
          {
            "node": "Is it an order?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is it an order?": {
      "main": [
        [
          {
            "node": "Extract order",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Reply: other",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract order": {
      "main": [
        [
          {
            "node": "Clean phone & wilaya",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Clean phone & wilaya": {
      "main": [
        [
          {
            "node": "Add to Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add to Google Sheets": {
      "main": [
        [
          {
            "node": "Reply: order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {
    "Webhook": [
      {
        "json": {
          "headers": {
            "content-type": "application/json"
          },
          "params": {},
          "query": {},
          "body": {
            "message": "Salam, nheb nchri 2 robes taille M couleur noir, livraison à domicile Bab Ezzouar Alger. Smiti Amina, 0555 12 34 56",
            "from": "whatsapp:+213555123456"
          },
          "webhookUrl": "https://your-n8n/webhook/commande",
          "executionMode": "test"
        }
      }
    ]
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false,
    "templateId": "dzrouter"
  }
}
