Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
360 lines (307 loc) · 11.2 KB
/
Copy pathserver.js
File metadata and controls
360 lines (307 loc) · 11.2 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
const express = require('express');
const path = require('path');
const { WebSocketServer } = require('ws');
const http = require('http');
const fs = require('fs');
const app = express();
app.use(express.static(__dirname));
const server = http.createServer(app);
const wss = new WebSocketServer({ server });
let chatbotData;
try {
chatbotData = JSON.parse(fs.readFileSync('./smartlife-chatbot.json', 'utf8'));
console.log('✅ JSON loaded! Nodes:', Object.keys(chatbotData.nodes).length);
} catch (e) {
console.error('❌ smartlife-chatbot.json missing!');
process.exit(1);
}
const clients = new Map();
wss.on('connection', (ws) => {
console.log('✅ Client connected');
const clientId = Date.now();
clients.set(clientId, {
ws,
currentNode: 'L.1',
language: 'en',
history: [],
formData: {},
formState: null
});
sendMessage(clientId);
ws.on('message', (data) => {
const message = data.toString().trim();
console.log(`📨 [${clientId}] ${message}`);
handleMessage(clientId, message);
});
ws.on('close', () => {
clients.delete(clientId);
console.log('❌ Client disconnected');
});
});
function sendMessage(clientId) {
const client = clients.get(clientId);
if (!client) return;
const node = chatbotData.nodes[client.currentNode];
if (!node) {
client.ws.send(JSON.stringify({ message: "Node not found", options: [] }));
return;
}
let response;
if (node.type === 'form') {
response = handleFormNode(client, node);
} else if (node.type === 'input') {
// Handle simple input nodes (like newsletter subscription)
client.inputState = { nodeId: client.currentNode, next: node.next };
response = {
message: node.message[client.language] || node.message.en || 'Please enter:',
options: [],
expectInput: true
};
} else {
response = {
message: node.message[client.language] || node.message.en || 'Welcome!',
options: getNodeOptions(client, node)
};
}
// Include action if node has one (for URL redirections)
if (node.action) {
response.action = node.action;
}
client.ws.send(JSON.stringify(response));
console.log(`📤 [${clientId}] ${client.currentNode}: ${response.message.substring(0, 50)}`);
}
function getNodeOptions(client, node) {
let options = [];
// Language selection node: always show English labels for language options
if (client.currentNode === 'L.1' && node.options) {
options = node.options.map((opt, index) => ({
index: index + 1,
text: opt.text.en
}));
return options;
}
// Build node's own options from the JSON
if (node.options) {
options = node.options.map((opt, index) => ({
index: index + 1,
text: opt.text[client.language] || opt.text.en || Object.values(opt.text)[0]
}));
}
// Only add auto-generated "Go Back" if node has `back` property
// AND the node's own options don't already contain a "Back" option
// to prevent duplicate Back buttons
if (node.back && client.currentNode !== 'L.1') {
const hasBackInOptions = node.options && node.options.some(opt => {
const text = (opt.text[client.language] || opt.text.en || '').toLowerCase().trim();
return text === 'back' || text === 'go back' || text === '⬅️ go back'
|| text === 'رجوع' || text === 'वापस';
});
if (!hasBackInOptions) {
options.push({
index: options.length + 1,
text: getBackText(client.language),
action: 'back'
});
}
}
// Add global options (Main Menu, Change Language, End Chat) if node has show_global
if (node.show_global && client.currentNode !== 'L.1' && chatbotData.global_options) {
chatbotData.global_options.forEach(globalOpt => {
options.push({
index: options.length + 1,
text: globalOpt.text[client.language] || globalOpt.text.en,
action: globalOpt.action || 'global'
});
});
}
return options;
}
function getBackText(language) {
return {
en: "⬅️ Go Back",
ar: "⬅️ رجوع",
hi: "⬅️ वापस"
}[language] || "⬅️ Go Back";
}
function handleMessage(clientId, message) {
const client = clients.get(clientId);
if (!client) return;
const node = chatbotData.nodes[client.currentNode];
// Handle form input first
if (client.formState) {
handleFormInput(clientId, message);
return;
}
// Handle simple input nodes (like newsletter name/email)
if (client.inputState) {
console.log(`📝 [${clientId}] Input received: ${message}`);
if (!client.collectedData) client.collectedData = {};
client.collectedData[client.inputState.nodeId] = message;
client.currentNode = client.inputState.next;
client.inputState = null;
sendMessage(clientId);
return;
}
const optionIndex = parseInt(message);
// Language selection node: special handling
if (client.currentNode === 'L.1' && !isNaN(optionIndex) && node.options[optionIndex - 1]) {
const option = node.options[optionIndex - 1];
client.language = option.action.split(':')[1];
client.currentNode = option.next;
console.log(`🌐 Language: ${client.language} → ${client.currentNode}`);
sendMessage(clientId);
return;
}
if (!isNaN(optionIndex)) {
// Build the same flat option list that was displayed to the user
// so we can correctly map the chosen number to the right action
const displayedOptions = getDisplayedOptionsMeta(client, node);
if (optionIndex >= 1 && optionIndex <= displayedOptions.length) {
const chosen = displayedOptions[optionIndex - 1];
if (chosen.source === 'node') {
// This is a regular node option from the JSON
const option = node.options[chosen.originalIndex];
if (option.action && option.action.startsWith('set_language:')) {
client.language = option.action.split(':')[1];
}
// Navigate to next node if it has one
if (option.next) {
client.currentNode = option.next;
}
// If action is call: or link:, send action to client and stay or navigate
if (option.action && (option.action.startsWith('call:') || option.action.startsWith('link:'))) {
const actionResponse = {
message: node.message[client.language] || node.message.en,
options: getNodeOptions(client, node),
action: option.action
};
client.ws.send(JSON.stringify(actionResponse));
return;
}
} else if (chosen.source === 'back') {
// Auto-generated "Go Back" button
client.currentNode = node.back;
} else if (chosen.source === 'global') {
// Global option
const globalOpt = chatbotData.global_options[chosen.originalIndex];
if (globalOpt.action === 'end') {
const endMsg = {
en: "Thank you for using SmartLife! Goodbye! 🙏",
ar: "شكراً لاستخدامك سمارت لايف! مع السلامة! 🙏",
hi: "स्मार्टलाइफ उपयोग करने के लिए धन्यवाद! अलविदा! 🙏"
};
client.ws.send(JSON.stringify({
message: endMsg[client.language] || endMsg.en,
options: []
}));
return;
} else if (globalOpt.action === 'goto_role_menu') {
// "Main Menu" / "Home" → go to M.1
client.currentNode = 'M.1';
} else if (globalOpt.next) {
client.currentNode = globalOpt.next;
}
}
sendMessage(clientId);
} else {
// Invalid option number, re-display current node
sendMessage(clientId);
}
} else {
// Non-numeric input, re-display current node
sendMessage(clientId);
}
}
/**
* Build metadata list matching exactly what getNodeOptions() displays.
* Each entry tells us the source (node / back / global) and the original index
* so handleMessage() can correctly resolve the user's selection.
*/
function getDisplayedOptionsMeta(client, node) {
const meta = [];
if (client.currentNode === 'L.1') {
// Language node — handled separately
return node.options ? node.options.map((_, i) => ({ source: 'node', originalIndex: i })) : [];
}
// 1. Node's own options
if (node.options) {
node.options.forEach((opt, i) => {
meta.push({ source: 'node', originalIndex: i });
});
}
// 2. Auto-generated Back button (only if node has `back` AND options don't already have one)
if (node.back && client.currentNode !== 'L.1') {
const hasBackInOptions = node.options && node.options.some(opt => {
const text = (opt.text[client.language] || opt.text.en || '').toLowerCase().trim();
return text === 'back' || text === 'go back' || text === '⬅️ go back'
|| text === 'رجوع' || text === 'वापس';
});
if (!hasBackInOptions) {
meta.push({ source: 'back', originalIndex: -1 });
}
}
// 3. Global options
if (node.show_global && client.currentNode !== 'L.1' && chatbotData.global_options) {
chatbotData.global_options.forEach((_, i) => {
meta.push({ source: 'global', originalIndex: i });
});
}
return meta;
}
function handleFormNode(client, node) {
if (!client.formState) {
client.formState = {
nodeId: node.id,
step: 0,
data: {},
completionOptions: node.completion_options || []
};
}
return {
message: `${node.message[client.language] || node.message.en}\n\n(Simplified form - type anything to continue)`,
options: node.completion_options?.map((opt, i) => ({
index: i + 1,
text: opt.text[client.language] || opt.text.en
})) || []
};
}
function handleFormInput(clientId, input) {
const client = clients.get(clientId);
const formNode = chatbotData.nodes[client.formState.nodeId];
// Check if user picked a completion option by number
const optionIndex = parseInt(input);
const completionOptions = formNode?.completion_options || client.formState.completionOptions || [];
if (!isNaN(optionIndex) && optionIndex >= 1 && optionIndex <= completionOptions.length) {
const chosen = completionOptions[optionIndex - 1];
client.formState = null;
if (chosen.next) {
client.currentNode = chosen.next;
} else {
// Fallback: go to role-appropriate menu
client.currentNode = getRoleMenu(client);
}
} else {
// Not a valid selection, clear form and go to role menu
client.formState = null;
client.currentNode = getRoleMenu(client);
}
sendMessage(clientId);
}
/**
* Determine the correct menu node based on the current role context.
* Falls back to M.1 if role is unknown.
*/
function getRoleMenu(client) {
const node = chatbotData.nodes[client.currentNode];
const role = node?.role;
switch (role) {
case 'worker': return 'W.1';
case 'volunteer': return 'V.1';
case 'company': return 'C.1';
case 'donor': return 'D.1';
default: return 'M.1';
}
}
server.listen(3000, () => {
console.log('SmartLife Chatbot: http://localhost:3000');
});