113 lines
2.7 KiB
Markdown
113 lines
2.7 KiB
Markdown
# راهنمای تنظیم n8n Workflow برای چت
|
|||
|
|
|
||
|
|
## گزینه 1: پاسخ ساده بدون GPT
|
||
|
|
|
||
|
|
### Node 1: Webhook
|
||
|
|
- **HTTP Method**: POST
|
||
|
|
- **Path**: `/chat` (یا هر مسیری که میخواهی)
|
||
|
|
- **Response Mode**: Using 'Respond to Webhook' Node
|
||
|
|
|
||
|
|
### Node 2: Code (JavaScript)
|
||
|
|
```javascript
|
||
|
|
const inputData = $input.item.json;
|
||
|
|
const userMessage = inputData.message || '';
|
||
|
|
|
||
|
|
// یک پاسخ ساده برمیگردانیم
|
||
|
|
return {
|
||
|
|
json: {
|
||
|
|
response: `شما گفتید: "${userMessage}". این یک پاسخ تست از n8n است!`
|
||
|
|
}
|
||
|
|
};
|
||
|
|
```
|
||
|
|
|
||
|
|
### Node 3: Respond to Webhook
|
||
|
|
- **Response Body**: `{{ $json.response }}`
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## گزینه 2: با استفاده از GPT (Message a model)
|
||
|
|
|
||
|
|
### Node 1: Webhook
|
||
|
|
- **HTTP Method**: POST
|
||
|
|
- **Path**: `/chat`
|
||
|
|
- **Response Mode**: Using 'Respond to Webhook' Node
|
||
|
|
|
||
|
|
### Node 2: Code (JavaScript)
|
||
|
|
```javascript
|
||
|
|
const inputData = $input.item.json;
|
||
|
|
const userMessage = inputData.message || '';
|
||
|
|
const conversationHistory = inputData.conversationHistory || [];
|
||
|
|
|
||
|
|
// ساخت messages array برای GPT
|
||
|
|
const messages = [
|
||
|
|
{
|
||
|
|
role: 'system',
|
||
|
|
content: 'شما یک دستیار هوشمند فارسیزبان هستید. پاسخهای کوتاه و مفید بدهید.'
|
||
|
|
}
|
||
|
|
];
|
||
|
|
|
||
|
|
// اضافه کردن تاریخچه
|
||
|
|
conversationHistory.forEach(msg => {
|
||
|
|
messages.push({
|
||
|
|
role: msg.role,
|
||
|
|
content: msg.content
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
// اضافه کردن پیام فعلی
|
||
|
|
messages.push({
|
||
|
|
role: 'user',
|
||
|
|
content: userMessage
|
||
|
|
});
|
||
|
|
|
||
|
|
return {
|
||
|
|
json: {
|
||
|
|
messages: messages,
|
||
|
|
model: 'gpt-4o-mini',
|
||
|
|
temperature: 0.7
|
||
|
|
}
|
||
|
|
};
|
||
|
|
```
|
||
|
|
|
||
|
|
### Node 3: Message a model
|
||
|
|
- **Model**: `gpt-4o-mini`
|
||
|
|
- **Messages**: `{{ $json.messages }}`
|
||
|
|
- **Temperature**: `{{ $json.temperature }}`
|
||
|
|
- **Credentials**: باید OpenAI API Key را تنظیم کنی
|
||
|
|
|
||
|
|
### Node 4: Code (برای فرمت کردن پاسخ)
|
||
|
|
```javascript
|
||
|
|
const gptResponse = $input.item.json;
|
||
|
|
const responseText = gptResponse.choices?.[0]?.message?.content ||
|
||
|
|
gptResponse.response ||
|
||
|
|
'پاسخی دریافت نشد';
|
||
|
|
|
||
|
|
return {
|
||
|
|
json: {
|
||
|
|
response: responseText
|
||
|
|
}
|
||
|
|
};
|
||
|
|
```
|
||
|
|
|
||
|
|
### Node 5: Respond to Webhook
|
||
|
|
- **Response Body**: `{{ $json.response }}`
|
||
|
|
|
||
|
|
---
|
||
|
|
|
||
|
|
## نکات مهم:
|
||
|
|
|
||
|
|
1. **URL Webhook**: بعد از فعال کردن Webhook، URL را کپی کن و در `.env` بذار:
|
||
|
|
```
|
||
|
|
VITE_N8N_WEBHOOK_URL=https://your-n8n.com/webhook/chat
|
||
|
|
```
|
||
|
|
|
||
|
|
2. **فعال کردن Workflow**: حتماً Workflow را فعال (Activate) کن
|
||
|
|
|
||
|
|
3. **تست**: میتوانی با Postman یا curl تست کنی:
|
||
|
|
```bash
|
||
|
|
curl -X POST https://your-n8n.com/webhook/chat \
|
||
|
|
-H "Content-Type: application/json" \
|
||
|
|
-d '{"message":"سلام","userId":"test-user"}'
|
||
|
|
```
|
||
|
|
|