{
“nodes”: [
{
“parameters”: {
“updates”: [“message”],
“additionalFields”: {}
},
“id”: “telegram-trigger-001”,
“name”: “Telegram Trigger”,
“type”: “n8n-nodes-base.telegramTrigger”,
“typeVersion”: 1,
“position”: [480, 80],
“webhookId”: “8ecd0432-93a9-4a02-86fc-95f17c118884”,
“credentials”: {
“telegramApi”: {
“id”: “ZXvFPh8Fpfd2tq2p”,
“name”: “Telegram account 2”
}
}
},
{
“parameters”: {
“assignments”: {
“assignments”: [
{
“id”: “assign-chat-id”,
“name”: “chat_id”,
“value”: “={{ $json.message.chat.id }}”,
“type”: “string”
},
{
“id”: “assign-message”,
“name”: “message”,
“value”: “={{ $json.message.text }}”,
“type”: “string”
},
{
“id”: “assign-firstname”,
“name”: “first_name”,
“value”: “={{ $json.message.from.first_name }}”,
“type”: “string”
}
]
},
“options”: {}
},
“id”: “edit-fields-001”,
“name”: “Edit Fields”,
“type”: “n8n-nodes-base.set”,
“typeVersion”: 3.4,
“position”: [680, 80]
},
{
“parameters”: {
“conditions”: {
“options”: {
“caseSensitive”: false,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 3
},
“conditions”: [
{
“id”: “start-check”,
“leftValue”: “={{ $json.message }}”,
“rightValue”: “/start”,
“operator”: {
“type”: “string”,
“operation”: “equals”
}
}
],
“combinator”: “and”
},
“options”: {}
},
“id”: “if-start-001”,
“name”: “Is /start?”,
“type”: “n8n-nodes-base.if”,
“typeVersion”: 2.3,
“position”: [880, 80]
},
{
“parameters”: {
“chatId”: “={{ $(‘Edit Fields’).item.json.chat_id }}”,
“text”: “=:waving_hand: مرحباً {{ $(‘Edit Fields’).item.json.first_name }}!\n\n:convenience_store: أهلاً بك في متجرنا للهواتف\n\nيمكنك:\n:mobile_phone: السعر — اكتب: سعر iphone 13\n:shopping_cart: الطلب — اكتب: نحب نطلب samsung s23\n:hammer_and_wrench: الدعم — اكتب: عندي مشكلة\n\nاكتب ما تريد وسنرد عليك فوراً
”,
“additionalFields”: {
“appendAttribution”: false
}
},
“id”: “welcome-001”,
“name”: “Welcome”,
“type”: “n8n-nodes-base.telegram”,
“typeVersion”: 1.2,
“position”: [1080, -80],
“credentials”: {
“telegramApi”: {
“id”: “ZXvFPh8Fpfd2tq2p”,
“name”: “Telegram account 2”
}
}
},
{
“parameters”: {
“documentId”: {
“__rl”: true,
“value”: “1OiHeQ0W9pwgcD9-1jLECu5eDudsPSZfn9lQcwaerK34”,
“mode”: “list”,
“cachedResultName”: “our storage”
},
“sheetName”: {
“__rl”: true,
“value”: “gid=0”,
“mode”: “list”,
“cachedResultName”: “Sheet1”
},
“options”: {}
},
“id”: “get-sheet-001”,
“name”: “Get Inventory”,
“type”: “n8n-nodes-base.googleSheets”,
“typeVersion”: 4.7,
“position”: [1080, 80],
“credentials”: {
“googleSheetsOAuth2Api”: {
“id”: “q5eYcQcjfRRs7qEf”,
“name”: “Google Sheets account 2”
}
}
},
{
“parameters”: {
“jsCode”: “const rows = $input.all();\n\nconst catalog = rows.map(r =>\n - ${r.json.model_name} | Price: ${r.json.price} DZD | Stock: ${r.json.stock} units\n).join(‘\n’);\n\nreturn {\n json: {\n …($(‘Edit Fields’).first().json),\n catalog\n }\n};”
},
“id”: “build-catalog-001”,
“name”: “Build Catalog”,
“type”: “n8n-nodes-base.code”,
“typeVersion”: 2,
“position”: [1280, 80]
},
{
“parameters”: {
“promptType”: “define”,
“text”: “={{ $json.message }}”,
“options”: {
“systemMessage”: “You are a professional mobile phone store assistant.\n\nCRITICAL RULES:\n1. ALWAYS reply in the same language as the customer.\n2. When a phone EXISTS in the database AND stock > 0, you MUST:\n * Clearly confirm availability.\n * Display the phone name EXACTLY as stored in database.\n * Show the FULL price.\n * Show remaining stock.\n * End the message with: To continue the purchase, press YES\n3. When a phone EXISTS but stock = 0:\n * Inform the customer it is out of stock.\n * Apologize politely.\n * Append: {“valable”: “false”, “model”: “MODEL NAME”, “reason”: “out_of_stock”}\n4. When a phone does NOT exist in database:\n * Inform the customer it is not available.\n * Append: {“valable”: “false”, “model”: “unknown”, “reason”: “not_found”}\n5. If greeting:\n * Reply warmly and list all available phones with prices.\n * Append: {“valable”: “false”, “model”: “none”, “reason”: “greeting”}\n6. If customer sends YES after phone confirmation:\n * Reply: ask for their name and phone number (تيليفون).\n * Append: {“valable”: “true”, “model”: “MODEL NAME”, “reason”: “order”}\n7. If customer sends only a number → ask them what phone they are looking for.\n8. You are the SELLER not the buyer. The customer is the one messaging you.\n9. Do NOT give short lazy answers.\n10. Do NOT skip price or stock.\n11. Use 2-3 emojis maximum.\n12. Do NOT invent any information not in the database.\n13. Do NOT use markdown stars or formatting symbols.\n\nFORMAT WHEN PHONE EXISTS AND IN STOCK:\nPhone: MODEL NAME\nPrice: FULL PRICE DZD\nStock: X units remaining\nTo continue the purchase, press YES\n\nThen append:\n{“valable”: “true”, “model”: “MODEL NAME EXACTLY AS IN DATABASE”, “reason”: “available”}\n\nDatabase:\n{{ $json.catalog }}\n\nDO NOT IGNORE THE DATABASE.\nDO NOT CREATE PERSONAL INFORMATION.”
}
},
“id”: “ai-agent-001”,
“name”: “AI Agent”,
“type”: “@n8n/n8n-nodes-langchain.agent”,
“typeVersion”: 1.7,
“position”: [1480, 80]
},
{
“parameters”: {
“jsCode”: “const output = $json.output || “”;\n\n// استخراج JSON\nlet aiData = {};\nconst jsonMatch = output.match(/\{[\s\S]?\}/);\nif (jsonMatch) {\n try {\n aiData = JSON.parse(jsonMatch[0]);\n } catch(e) {\n aiData = {};\n }\n}\n\n// حذف JSON من النص\nconst cleanText = output.replace(/\{[\s\S]?\}/, “”).trim();\n\nreturn {\n json: {\n …($json),\n clean_response: cleanText,\n ai_data: aiData,\n is_order: aiData.reason === “order”,\n valable: aiData.valable === “true”,\n detected_model: aiData.model || “”,\n reason: aiData.reason || “unknown”\n }\n};”
},
“id”: “parse-response-001”,
“name”: “Parse Response”,
“type”: “n8n-nodes-base.code”,
“typeVersion”: 2,
“position”: [1680, 80]
},
{
“parameters”: {
“chatId”: “={{ $(‘Edit Fields’).item.json.chat_id }}”,
“text”: “={{ $json.clean_response }}”,
“additionalFields”: {
“appendAttribution”: false
}
},
“id”: “send-response-001”,
“name”: “Send Response”,
“type”: “n8n-nodes-base.telegram”,
“typeVersion”: 1.2,
“position”: [1880, 80],
“credentials”: {
“telegramApi”: {
“id”: “ZXvFPh8Fpfd2tq2p”,
“name”: “Telegram account 2”
}
}
},
{
“parameters”: {
“conditions”: {
“options”: {
“caseSensitive”: true,
“leftValue”: “”,
“typeValidation”: “strict”,
“version”: 3
},
“conditions”: [
{
“id”: “is-order-check”,
“leftValue”: “={{ $json.is_order }}”,
“rightValue”: true,
“operator”: {
“type”: “boolean”,
“operation”: “equal”
}
}
],
“combinator”: “and”
},
“options”: {}
},
“id”: “if-order-001”,
“name”: “Is Order?”,
“type”: “n8n-nodes-base.if”,
“typeVersion”: 2.3,
“position”: [2080, 80]
},
{
“parameters”: {
“operation”: “append”,
“documentId”: {
“__rl”: true,
“value”: “1OiHeQ0W9pwgcD9-1jLECu5eDudsPSZfn9lQcwaerK34”,
“mode”: “list”,
“cachedResultName”: “our storage”
},
“sheetName”: {
“__rl”: true,
“value”: 1070965526,
“mode”: “list”,
“cachedResultName”: “orders”
},
“columns”: {
“mappingMode”: “defineBelow”,
“value”: {
“chat_id”: “={{ $(‘Edit Fields’).item.json.chat_id }}”,
“model_name”: “={{ $json.detected_model }}”,
“status”: “pending”,
“timestamp”: “={{ new Date().toISOString() }}”
},
“matchingColumns”: ,
“schema”: [
{“id”: “chat_id”, “displayName”: “chat_id”, “required”: false, “defaultMatch”: false, “display”: true, “type”: “string”, “canBeUsedToMatch”: false},
{“id”: “model_name”, “displayName”: “model_name”, “required”: false, “defaultMatch”: false, “display”: true, “type”: “string”, “canBeUsedToMatch”: false},
{“id”: “status”, “displayName”: “status”, “required”: false, “defaultMatch”: false, “display”: true, “type”: “string”, “canBeUsedToMatch”: false},
{“id”: “timestamp”, “displayName”: “timestamp”, “required”: false, “defaultMatch”: false, “display”: true, “type”: “string”, “canBeUsedToMatch”: false}
]
},
“options”: {}
},
“id”: “save-order-001”,
“name”: “Save Order”,
“type”: “n8n-nodes-base.googleSheets”,
“typeVersion”: 4.7,
“position”: [2280, -48],
“credentials”: {
“googleSheetsOAuth2Api”: {
“id”: “q5eYcQcjfRRs7qEf”,
“name”: “Google Sheets account 2”
}
}
},
{
“parameters”: {
“chatId”: “7654671409”,
“text”: “=🛒 طلب جديد!\n───────────────\n👤 العميل: {{ $(‘Edit Fields’).item.json.first_name }}\n💬 Chat ID: {{ $(‘Edit Fields’).item.json.chat_id }}\n📱 الهاتف: {{ $json.detected_model }}\n🕐 الوقت: {{ new Date().toLocaleString(‘ar-DZ’) }}\n───────────────”,
“additionalFields”: {
“appendAttribution”: false
}
},
“id”: “notify-admin-001”,
“name”: “Notify Admin”,
“type”: “n8n-nodes-base.telegram”,
“typeVersion”: 1.2,
“position”: [2480, -48],
“credentials”: {
“telegramApi”: {
“id”: “ZXvFPh8Fpfd2tq2p”,
“name”: “Telegram account 2”
}
}
},
{
“parameters”: {
“operation”: “append”,
“documentId”: {
“__rl”: true,
“value”: “1OiHeQ0W9pwgcD9-1jLECu5eDudsPSZfn9lQcwaerK34”,
“mode”: “list”,
“cachedResultName”: “our storage”
},
“sheetName”: {
“__rl”: true,
“value”: 222045363,
“mode”: “list”,
“cachedResultName”: “logs”
},
“columns”: {
“mappingMode”: “defineBelow”,
“value”: {
“chat_id”: “={{ $(‘Edit Fields’).item.json.chat_id }}”,
“username”: “={{ $(‘Edit Fields’).item.json.first_name }}”,
“message”: “={{ $(‘Edit Fields’).item.json.message }}”,
“category”: “={{ $json.reason }}”,
“timestamp”: “={{ new Date().toISOString() }}”
},
“matchingColumns”: ,
“schema”: [
{“id”: “chat_id”, “displayName”: “chat_id”, “required”: false, “defaultMatch”: false, “display”: true, “type”: “string”, “canBeUsedToMatch”: false},
{“id”: “username”, “displayName”: “username”, “required”: false, “defaultMatch”: false, “display”: true, “type”: “string”, “canBeUsedToMatch”: false},
{“id”: “message”, “displayName”: “message”, “required”: false, “defaultMatch”: false, “display”: true, “type”: “string”, “canBeUsedToMatch”: false},
{“id”: “category”, “displayName”: “category”, “required”: false, “defaultMatch”: false, “display”: true, “type”: “string”, “canBeUsedToMatch”: false},
{“id”: “timestamp”, “displayName”: “timestamp”, “required”: false, “defaultMatch”: false, “display”: true, “type”: “string”, “canBeUsedToMatch”: false}
]
},
“options”: {}
},
“id”: “logs-001”,
“name”: “Logs”,
“type”: “n8n-nodes-base.googleSheets”,
“typeVersion”: 4.7,
“position”: [2280, 240],
“credentials”: {
“googleSheetsOAuth2Api”: {
“id”: “q5eYcQcjfRRs7qEf”,
“name”: “Google Sheets account 2”
}
}
},
{
“parameters”: {},
“id”: “error-trigger-001”,
“name”: “Error Trigger”,
“type”: “n8n-nodes-base.errorTrigger”,
“typeVersion”: 1,
“position”: [480, -240]
},
{
“parameters”: {
“chatId”: “7654671409”,
“text”: “=⚠️ خطأ في الـ workflow!\n───────────────\n❌ Node: {{ $json.execution.lastNodeExecuted }}\n📝 الخطأ: {{ $json.execution.error.message }}\n🕐 الوقت: {{ new Date().toLocaleString(‘ar-DZ’) }}\n───────────────”,
“additionalFields”: {
“appendAttribution”: false
}
},
“id”: “error-notify-001”,
“name”: “Error Notify”,
“type”: “n8n-nodes-base.telegram”,
“typeVersion”: 1.2,
“position”: [680, -240],
“credentials”: {
“telegramApi”: {
“id”: “ZXvFPh8Fpfd2tq2p”,
“name”: “Telegram account 2”
}
}
}
],
“connections”: {
“Telegram Trigger”: {“main”: [[{“node”: “Edit Fields”, “type”: “main”, “index”: 0}]]},
“Edit Fields”: {“main”: [[{“node”: “Is /start?”, “type”: “main”, “index”: 0}]]},
“Is /start?”: {
“main”: [
[{“node”: “Welcome”, “type”: “main”, “index”: 0}],
[{“node”: “Get Inventory”, “type”: “main”, “index”: 0}]
]
},
“Get Inventory”: {“main”: [[{“node”: “Build Catalog”, “type”: “main”, “index”: 0}]]},
“Build Catalog”: {“main”: [[{“node”: “AI Agent”, “type”: “main”, “index”: 0}]]},
“AI Agent”: {“main”: [[{“node”: “Parse Response”, “type”: “main”, “index”: 0}]]},
“Parse Response”: {“main”: [[{“node”: “Send Response”, “type”: “main”, “index”: 0}]]},
“Send Response”: {“main”: [[{“node”: “Is Order?”, “type”: “main”, “index”: 0}]]},
“Is Order?”: {
“main”: [
[{“node”: “Save Order”, “type”: “main”, “index”: 0}],
[{“node”: “Logs”, “type”: “main”, “index”: 0}]
]
},
“Save Order”: {“main”: [[{“node”: “Notify Admin”, “type”: “main”, “index”: 0}]]},
“Error Trigger”: {“main”: [[{“node”: “Error Notify”, “type”: “main”, “index”: 0}]]}
},
“pinData”: {},
“meta”: {
“templateCredsSetupCompleted”: true,
“instanceId”: “7fbeb0e44c9259464bb788da40ee857ec56581d474c5c5e31f1c6b34bab08022”
}
}