-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn8n-workflow-v2.json
More file actions
171 lines (171 loc) · 5.35 KB
/
n8n-workflow-v2.json
File metadata and controls
171 lines (171 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
{
"name": "Chatbot Workflow - Working Version",
"nodes": [
{
"parameters": {
"httpMethod": "POST",
"path": "chatbot-v2",
"responseMode": "responseNode",
"options": {}
},
"id": "webhook-trigger",
"name": "Webhook Trigger",
"type": "n8n-nodes-base.webhook",
"typeVersion": 1,
"position": [240, 300],
"webhookId": "your-webhook-id-v2"
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"id": "condition1",
"leftValue": "={{ $json.body.session_variables['x-hasura-user-id'] }}",
"rightValue": "",
"operator": {
"type": "string",
"operation": "notEmpty"
}
}
],
"combinator": "and"
}
},
"id": "validate-user",
"name": "Validate User",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [460, 300]
},
{
"parameters": {
"method": "POST",
"url": "https://openrouter.ai/api/v1/chat/completions",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Authorization",
"value": "Bearer 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"
},
{
"name": "Content-Type",
"value": "application/json"
},
{
"name": "HTTP-Referer",
"value": "https://your-app-domain.com"
},
{
"name": "X-Title",
"value": "Chatbot App"
}
]
},
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={\n \"model\": \"openai/gpt-3.5-turbo\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"You are a helpful AI assistant. Be concise and friendly in your responses.\"\n },\n {\n \"role\": \"user\",\n \"content\": $('Webhook Trigger').first().json.body.input.message\n }\n ],\n \"max_tokens\": 150,\n \"temperature\": 0.7\n}",
"options": {}
},
"id": "call-openrouter",
"name": "Call OpenRouter AI",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [680, 200]
},
{
"parameters": {
"jsCode": "// Extract the AI response from OpenRouter API response\nconst openRouterResponse = $input.first().json;\nconsole.log('OpenRouter Response:', JSON.stringify(openRouterResponse, null, 2));\n\n// Handle both array and object responses\nlet aiContent;\nif (Array.isArray(openRouterResponse)) {\n aiContent = openRouterResponse[0]?.choices?.[0]?.message?.content;\n} else {\n aiContent = openRouterResponse?.choices?.[0]?.message?.content;\n}\n\n// Get webhook input data\nconst webhookData = $('Webhook Trigger').first().json;\nconst chatId = webhookData.body.input.chat_id;\nconst userMessage = webhookData.body.input.message;\n\nconsole.log('Extracted AI Content:', aiContent);\nconsole.log('Chat ID:', chatId);\nconsole.log('User Message:', userMessage);\n\nreturn {\n success: true,\n message: 'Response generated successfully',\n response: aiContent || 'Sorry, I could not generate a response.',\n chatId: chatId,\n debug: {\n openRouterResponse: openRouterResponse,\n isArray: Array.isArray(openRouterResponse)\n }\n};"
},
"id": "process-response",
"name": "Process Response",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [900, 200]
},
{
"parameters": {
"respondWith": "json",
"responseBody": "={{ $json }}"
},
"id": "success-response",
"name": "Success Response",
"type": "n8n-nodes-base.respondToWebhook",
"typeVersion": 1,
"position": [1120, 200]
},
{
"parameters": {
"respondWith": "json",
"responseBody": "{\n \"success\": false,\n \"message\": \"Authentication required: No valid user session\",\n \"response\": null\n}",
"options": {
"responseCode": 401
}
},
"id": "auth-required-response",
"name": "Auth Required Response",
"type": "n8n-nodes-base.respondToWebhook",
"typeVersion": 1,
"position": [460, 400]
}
],
"connections": {
"Webhook Trigger": {
"main": [
[
{
"node": "Validate User",
"type": "main",
"index": 0
}
]
]
},
"Validate User": {
"main": [
[
{
"node": "Call OpenRouter AI",
"type": "main",
"index": 0
}
],
[
{
"node": "Auth Required Response",
"type": "main",
"index": 0
}
]
]
},
"Call OpenRouter AI": {
"main": [
[
{
"node": "Process Response",
"type": "main",
"index": 0
}
]
]
},
"Process Response": {
"main": [
[
{
"node": "Success Response",
"type": "main",
"index": 0
}
]
]
}
}
}