{
 "name": "تعليق ← رسالة خاصة بالدليل — اسأل أبو خليفة",
 "nodes": [
  {
   "id": "1596de35-155e-4004-8a1e-f867563e78ee",
   "name": "تحقق Meta (GET)",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2,
   "position": [
    0,
    0
   ],
   "parameters": {
    "httpMethod": "GET",
    "path": "instagram",
    "responseMode": "responseNode",
    "options": {}
   },
   "webhookId": "2717fc17-1ef7-4d98-b35b-4b8fca26a8be"
  },
  {
   "id": "cc6cfd4a-e903-42eb-bfca-168804426541",
   "name": "كلمة التحقق",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    220,
    0
   ],
   "parameters": {
    "jsCode": "// ===== عدّل هنا =====\nconst VERIFY_TOKEN = 'abukhalifa2026';   // غيّرها: حروف وأرقام إنجليزية بس، ونفسها تكتبها في Meta (Verify token)\n// ====================\n\nconst q = $input.first().json.query || {};\nreturn [{ json: { reply: q['hub.verify_token'] === VERIFY_TOKEN ? (q['hub.challenge'] || '') : 'forbidden' } }];\n"
   }
  },
  {
   "id": "21c3436e-81ef-4d8e-844d-caa1f39ae5f7",
   "name": "رد التحقق",
   "type": "n8n-nodes-base.respondToWebhook",
   "typeVersion": 1.1,
   "position": [
    440,
    0
   ],
   "parameters": {
    "respondWith": "text",
    "responseBody": "={{ $json.reply }}",
    "options": {}
   }
  },
  {
   "id": "cfc43915-bb9e-42fd-a063-2de6eab220a0",
   "name": "أحداث انستقرام (POST)",
   "type": "n8n-nodes-base.webhook",
   "typeVersion": 2,
   "position": [
    0,
    240
   ],
   "parameters": {
    "httpMethod": "POST",
    "path": "instagram",
    "options": {}
   },
   "webhookId": "ba7ecd23-c03d-45bd-ac54-ac3cbaa9f7f0"
  },
  {
   "id": "6f0fb9d6-79f9-45c0-84c3-1c76a0963c13",
   "name": "الإعدادات وفهم الحدث",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    220,
    240
   ],
   "parameters": {
    "jsCode": "// ===== الإعدادات: عدّل هنا =====\nconst GUIDES = [\n  // key: اسم قصير بالإنجليزي، keywords: الكلمات بدون «ال»، link: رابط الدليل، title: اسم الدليل في الرسالة\n  { key: 'guide1', keywords: ['دليل'], link: 'https://example.com/guide', title: 'الدليل' },\n];\n// ===================================\n\nconst API = 'https://graph.instagram.com/v26.0';\nconst norm = s => (s || '').toLowerCase()\n  .replace(/[ً-ْـ]/g, '')\n  .replace(/[أإآ]/g, 'ا').replace(/ة/g, 'ه').replace(/ى/g, 'ي').trim();\nconst said = (g, t) => (g.keywords || []).some(k => t.includes(norm(k)));\nconst out = [];\nfor (const it of $input.all()) {\n  const body = it.json.body || {};\n  for (const e of body.entry || []) {\n    for (const c of e.changes || []) {\n      if (c.field !== 'comments') continue;\n      const v = c.value || {};\n      if (!v.from || v.from.id === e.id) continue;   // تعليقات حسابك أنت ما يرد عليها\n      const g = GUIDES.find(g => said(g, norm(v.text)));\n      if (!g) continue;\n      out.push({ json: { type: 'comment', api: API, comment_id: v.id, user_id: v.from.id, key: g.key, link: g.link, title: g.title } });\n    }\n    for (const m of e.messaging || []) {\n      if (m.message && m.message.is_echo) continue;\n      const payload = (m.postback && m.postback.payload) || '';\n      if (!payload.includes(':')) continue;\n      const g = GUIDES.find(g => g.key === payload.split(':')[1]);\n      if (!g) continue;\n      out.push({ json: { type: 'button', api: API, user_id: m.sender.id, payload, key: g.key, link: g.link, title: g.title } });\n    }\n  }\n}\nreturn out;\n"
   }
  },
  {
   "id": "c5b29a62-1b52-4216-903a-be11f01c4df7",
   "name": "ضغط الزر؟",
   "type": "n8n-nodes-base.if",
   "typeVersion": 2.2,
   "position": [
    440,
    240
   ],
   "parameters": {
    "conditions": {
     "options": {
      "caseSensitive": true,
      "typeValidation": "strict",
      "version": 2
     },
     "conditions": [
      {
       "id": "f2bea435-adee-46e5-b0e8-f9c3abd45f4c",
       "leftValue": "={{ $json.type }}",
       "rightValue": "button",
       "operator": {
        "type": "string",
        "operation": "equals"
       }
      }
     ],
     "combinator": "and"
    },
    "options": {}
   }
  },
  {
   "id": "06936edb-e1ed-4ab2-84f2-774816d44946",
   "name": "هل يتابعك؟",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    660,
    160
   ],
   "parameters": {
    "url": "={{ $json.api }}/{{ $json.user_id }}",
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth",
    "sendQuery": true,
    "queryParameters": {
     "parameters": [
      {
       "name": "fields",
       "value": "is_user_follow_business"
      }
     ]
    },
    "options": {
     "response": {
      "response": {
       "neverError": true
      }
     }
    }
   },
   "credentials": {
    "httpHeaderAuth": {
     "id": "",
     "name": "توكن انستقرام"
    }
   }
  },
  {
   "id": "6fe658ba-3a2e-4a02-b024-c98a7048baab",
   "name": "رسالة الرابط أو المتابعة",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    880,
    160
   ],
   "parameters": {
    "jsCode": "const btn = (text, buttons) => ({ attachment: { type: 'template', payload: { template_type: 'button', text, buttons } } });\nconst checks = $input.all();\nconst out = [];\nchecks.forEach((c, i) => {\n  const d = $('ضغط الزر؟').itemMatching(i).json;\n  if (c.json.error) throw new Error('فشل سؤال انستقرام عن المتابعة (غالباً التوكن خلص): ' + JSON.stringify(c.json.error).slice(0, 200));\n  const dm = message => ({ json: { url: `${d.api}/me/messages`, body: { recipient: { id: d.user_id }, message } } });\n  const follows = c.json.is_user_follow_business === true;\n  if (follows) {\n    out.push(dm(btn(`تفضّل ${d.title} 👇`, [{ type: 'web_url', url: d.link, title: 'افتح الدليل' }])));\n  } else {\n    const again = d.payload.startsWith('FOLLOWED');\n    const text = again ? 'للحين ما لقيت المتابعة 🤔 تابع الحساب وبعدها اضغط «تابعت»'\n                       : 'شكلك ما تتابعني للحين 🙏 تابع الحساب وبعدها اضغط «تابعت» ويوصلك الدليل';\n    out.push(dm(btn(text, [{ type: 'postback', title: 'تابعت ✅', payload: 'FOLLOWED:' + d.key }])));\n  }\n});\nreturn out;\n"
   }
  },
  {
   "id": "6aa0ddb9-a1af-4637-8fa5-b3d98258a85c",
   "name": "أرسل (الزر)",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    1100,
    160
   ],
   "parameters": {
    "method": "POST",
    "url": "={{ $json.url }}",
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth",
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify($json.body) }}",
    "options": {}
   },
   "credentials": {
    "httpHeaderAuth": {
     "id": "",
     "name": "توكن انستقرام"
    }
   }
  },
  {
   "id": "e1e6687b-654a-43f0-a0d2-bb8f34ac31b4",
   "name": "رسالة التعليق",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    660,
    340
   ],
   "parameters": {
    "jsCode": "const btn = (text, buttons) => ({ attachment: { type: 'template', payload: { template_type: 'button', text, buttons } } });\nconst out = [];\nfor (const it of $input.all()) {\n  const d = it.json;\n  out.push({ json: { url: `${d.api}/me/messages`, body: { recipient: { comment_id: d.comment_id },\n    message: btn(`هلا 👋 اضغط الزر تحت ويوصلك ${d.title}`, [{ type: 'postback', title: 'أرسل لي الرابط', payload: 'LINK:' + d.key }]) } } });\n  out.push({ json: { url: `${d.api}/${d.comment_id}/replies`, body: { message: 'أرسلتها لك بالخاص ✅ شيّك الرسائل' } } });\n}\nreturn out;\n"
   }
  },
  {
   "id": "4104e3d6-e904-4a77-895f-d03298fb1be1",
   "name": "أرسل (التعليق)",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    880,
    340
   ],
   "parameters": {
    "method": "POST",
    "url": "={{ $json.url }}",
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth",
    "sendBody": true,
    "specifyBody": "json",
    "jsonBody": "={{ JSON.stringify($json.body) }}",
    "options": {}
   },
   "credentials": {
    "httpHeaderAuth": {
     "id": "",
     "name": "توكن انستقرام"
    }
   }
  },
  {
   "id": "e2c688fe-f80a-4734-8618-e08b13b6f068",
   "name": "ملاحظات",
   "type": "n8n-nodes-base.stickyNote",
   "typeVersion": 1,
   "position": [
    -40,
    -260
   ],
   "parameters": {
    "width": 560,
    "height": 220,
    "content": "## تعليق بكلمة ← رسالة خاصة بالدليل\n1. افتح «كلمة التحقق» وغيّر الكلمة (إنجليزي بس، ونفسها في Meta).\n2. افتح «الإعدادات وفهم الحدث» واكتب أدلتك وكلماتها (بدون «ال»).\n3. سوّ Credential من نوع Header Auth: Name = Authorization، Value = Bearer ثم التوكن.\n4. اختره في «هل يتابعك؟» و«أرسل (الزر)» و«أرسل (التعليق)».\n5. Publish.\n— اسأل أبو خليفة"
   }
  }
 ],
 "connections": {
  "تحقق Meta (GET)": {
   "main": [
    [
     {
      "node": "كلمة التحقق",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "كلمة التحقق": {
   "main": [
    [
     {
      "node": "رد التحقق",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "أحداث انستقرام (POST)": {
   "main": [
    [
     {
      "node": "الإعدادات وفهم الحدث",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "الإعدادات وفهم الحدث": {
   "main": [
    [
     {
      "node": "ضغط الزر؟",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "ضغط الزر؟": {
   "main": [
    [
     {
      "node": "هل يتابعك؟",
      "type": "main",
      "index": 0
     }
    ],
    [
     {
      "node": "رسالة التعليق",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "هل يتابعك؟": {
   "main": [
    [
     {
      "node": "رسالة الرابط أو المتابعة",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "رسالة الرابط أو المتابعة": {
   "main": [
    [
     {
      "node": "أرسل (الزر)",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "رسالة التعليق": {
   "main": [
    [
     {
      "node": "أرسل (التعليق)",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 },
 "settings": {
  "executionOrder": "v1"
 }
}