{
 "name": "منشیوا — رویدادها با امضای معتبر",
 "nodes": [
  {
   "parameters": {
    "content": "## منشیوا ← n8n\n۱) این گردش‌کار را فعال کنید و نشانی **Production URL** گرهٔ Webhook را در پنل منشیوا ← API و وب‌هوک ← «وب‌هوک تازه» ثبت کنید.\n۲) secret نمایش‌داده‌شده (whsec_…) را در گرهٔ «محاسبهٔ امضا» بگذارید.\n۳) خروجی «پیام آماده» را به تلگرام، بله، ایمیل یا هر گرهٔ دیگری وصل کنید؛ متن در `text` است.\n\nرویدادهای نامعتبر یا قدیمی‌تر از ۵ دقیقه دور ریخته می‌شوند.",
    "width": 560,
    "height": 270
   },
   "id": "263fe3a9-2973-5ebc-b2b5-71cefc766f72",
   "name": "راهنما",
   "type": "n8n-nodes-base.stickyNote",
   "typeVersion": 1,
   "position": [
    -40,
    -320
   ]
  },
  {
   "parameters": {
    "httpMethod": "POST",
    "path": "monshiva-events",
    "options": {
     "rawBody": true
    }
   },
   "id": "56c1c05e-11e4-5923-bd66-4b6143411379",
   "name": "Webhook",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2,
   "position": [
    0,
    0
   ],
   "webhookId": "099de0dd-4f01-5287-ae96-b3d3839c062d"
  },
  {
   "parameters": {
    "jsCode": "// بدنهٔ خام و سرآیند امضا را برای بررسی آماده می‌کند\nconst item = $input.first();\nconst buf = await this.helpers.getBinaryDataBuffer(0, 'data');\nconst raw = buf.toString('utf8');\nconst header = String(item.json.headers['x-monshiva-signature'] || '');\nconst parts = Object.fromEntries(header.split(',').map(x => x.split('=')));\nreturn [{ json: { t: parts.t || '', v1: parts.v1 || '', signed: (parts.t || '') + '.' + raw, event: JSON.parse(raw) } }];"
   },
   "id": "2b8b3c3a-f107-5030-ab70-7e00cc310444",
   "name": "آماده‌سازی",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    220,
    0
   ]
  },
  {
   "parameters": {
    "action": "hmac",
    "type": "SHA256",
    "value": "={{ $json.signed }}",
    "dataPropertyName": "expected",
    "secret": "whsec_PASTE_YOUR_SECRET",
    "encoding": "hex"
   },
   "id": "7bdc14ae-ea2f-5b89-b598-50fd70850c33",
   "name": "محاسبهٔ امضا",
   "type": "n8n-nodes-base.crypto",
   "typeVersion": 1,
   "position": [
    440,
    0
   ]
  },
  {
   "parameters": {
    "jsCode": "// امضا باید برابر باشد و زمان رویداد کمتر از ۵ دقیقه با اکنون فاصله داشته باشد\nconst j = $input.first().json;\nconst fresh = Math.abs(Date.now() / 1000 - Number(j.t)) < 300;\nconst valid = fresh && j.v1.length === 64 && j.expected === j.v1;\nconst e = j.event, d = e.data || {};\nlet text = '';\nif (e.type === 'call.completed') { const c = d.call || {}; text = `تماس تازه از ${c.customer?.name || c.customer?.phone || 'مشتری'}: ${c.summary || 'بدون خلاصه'}` + (c.callback_requested ? ' — درخواست تماس دارد' : ''); }\nelse if (e.type === 'booking.created') { const b = d.booking || {}; text = `نوبت تازه: ${b.name || b.customer || ''} ${b.date || ''} ساعت ${b.time || ''}`; }\nelse if (e.type === 'callback.created') text = 'درخواست تماس تازه ثبت شد.';\nelse if (e.type === 'lead.created') text = `سرنخ تازه: ${d.name || d.mobile || ''} (${d.source || ''})`;\nelse text = `رویداد ${e.type}`;\nreturn [{ json: { valid, id: e.id, type: e.type, text, event: e } }];"
   },
   "id": "5580a546-8a09-5bb0-9851-24fec92de457",
   "name": "بررسی",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    660,
    0
   ]
  },
  {
   "parameters": {
    "conditions": {
     "options": {
      "caseSensitive": true,
      "leftValue": "",
      "typeValidation": "loose",
      "version": 2
     },
     "conditions": [
      {
       "id": "9fb7f8a0-09e8-5ac4-a10a-d0319cf7a283",
       "leftValue": "={{ $json.valid }}",
       "rightValue": "",
       "operator": {
        "type": "boolean",
        "operation": "true",
        "singleValue": true
       }
      }
     ],
     "combinator": "and"
    },
    "options": {}
   },
   "id": "5ebf450c-a88b-5602-a1b3-0616d5de4b2b",
   "name": "معتبر است؟",
   "type": "n8n-nodes-base.if",
   "typeVersion": 2.2,
   "position": [
    880,
    0
   ]
  },
  {
   "parameters": {
    "assignments": {
     "assignments": [
      {
       "id": "fd00e761-4ab2-5172-92b6-5e1771cdfee9",
       "name": "text",
       "value": "={{ $json.text }}",
       "type": "string"
      },
      {
       "id": "7fad70a1-06d6-55c4-a519-72d0e5c82391",
       "name": "type",
       "value": "={{ $json.type }}",
       "type": "string"
      },
      {
       "id": "288ba79c-4fbf-5cf3-b0dd-f9dee0ce9ed1",
       "name": "event_id",
       "value": "={{ $json.id }}",
       "type": "string"
      }
     ]
    },
    "options": {}
   },
   "id": "89ab4e84-a147-5992-ab0c-214e19b87e75",
   "name": "پیام آماده",
   "type": "n8n-nodes-base.set",
   "typeVersion": 3.4,
   "position": [
    1100,
    -80
   ]
  }
 ],
 "connections": {
  "Webhook": {
   "main": [
    [
     {
      "node": "آماده‌سازی",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "آماده‌سازی": {
   "main": [
    [
     {
      "node": "محاسبهٔ امضا",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "محاسبهٔ امضا": {
   "main": [
    [
     {
      "node": "بررسی",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "بررسی": {
   "main": [
    [
     {
      "node": "معتبر است؟",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "معتبر است؟": {
   "main": [
    [
     {
      "node": "پیام آماده",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 },
 "active": false,
 "settings": {
  "executionOrder": "v1"
 },
 "pinData": {},
 "meta": {
  "templateCredsSetupCompleted": false
 }
}