@data-netmonk/mona-chat-widget 2.1.37 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +238 -91
- package/dist/index.cjs +56 -56
- package/dist/index.js +9501 -9470
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ Chat widget package developed by Netmonk data & solution team to be imported in
|
|
|
10
10
|
|
|
11
11
|
1. Install dependencies
|
|
12
12
|
```
|
|
13
|
-
npm install
|
|
13
|
+
npm install --legacy-peer-deps
|
|
14
14
|
```
|
|
15
15
|
2. Copy .env.example
|
|
16
16
|
```
|
|
@@ -99,6 +99,45 @@ For responses with buttons:
|
|
|
99
99
|
|
|
100
100
|
---
|
|
101
101
|
|
|
102
|
+
### Recent Updates & Breaking Changes
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
**Latest Version Changes:**
|
|
107
|
+
|
|
108
|
+
⚠️ **Breaking Changes:**
|
|
109
|
+
1. **Removed `type` and `agentType` props** - These parameters are no longer used and have been removed from all components
|
|
110
|
+
2. **Renamed `botServerUrl` to `webhookUrl`** - For better clarity and consistency
|
|
111
|
+
3. **`webhookUrl` is now required** - Must be provided as a prop (falls back to env vars if not provided)
|
|
112
|
+
|
|
113
|
+
✨ **New Features:**
|
|
114
|
+
1. **Authentication support** - Automatic token management with refresh on 401 errors
|
|
115
|
+
- Pass `authUrl` in the `data` prop
|
|
116
|
+
- Widget handles token lifecycle automatically
|
|
117
|
+
2. **Enhanced data prop** - Support for custom variables passed to backend via `data` prop
|
|
118
|
+
3. **Improved error handling** - Better fallback mechanisms and error messages
|
|
119
|
+
|
|
120
|
+
📝 **Migration Guide:**
|
|
121
|
+
```jsx
|
|
122
|
+
// Old usage (deprecated)
|
|
123
|
+
<ChatWidget
|
|
124
|
+
userId="user123"
|
|
125
|
+
sourceId="source456"
|
|
126
|
+
type="prime"
|
|
127
|
+
botServerUrl="https://api.example.com"
|
|
128
|
+
/>
|
|
129
|
+
|
|
130
|
+
// New usage (current)
|
|
131
|
+
<ChatWidget
|
|
132
|
+
userId="user123"
|
|
133
|
+
sourceId="source456"
|
|
134
|
+
webhookUrl="https://api.example.com/webhook"
|
|
135
|
+
data="authUrl=https://api.example.com/login/chatwidget~username=John"
|
|
136
|
+
/>
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
102
141
|
### Library (how to update and publish)
|
|
103
142
|
|
|
104
143
|
---
|
|
@@ -165,10 +204,9 @@ For responses with buttons:
|
|
|
165
204
|
function App() {
|
|
166
205
|
return (
|
|
167
206
|
<ChatWidget
|
|
168
|
-
type="prime"
|
|
169
207
|
userId="user123"
|
|
170
208
|
sourceId="691e1b5952068ff7aaeccffc9"
|
|
171
|
-
|
|
209
|
+
webhookUrl="https://your-backend-url.com"
|
|
172
210
|
/>
|
|
173
211
|
);
|
|
174
212
|
}
|
|
@@ -186,20 +224,13 @@ For responses with buttons:
|
|
|
186
224
|
|------|------|-------------|
|
|
187
225
|
| `userId` | `string` | **Required.** Unique identifier for the user |
|
|
188
226
|
| `sourceId` | `string` | **Required.** Source/channel identifier for the chat |
|
|
189
|
-
|
|
190
|
-
#### Dynamic Data Prop (NEW!)
|
|
191
|
-
| Prop | Type | Description |
|
|
192
|
-
|------|------|-------------|
|
|
193
|
-
| `data` | `string` | URL-encoded config string (see advanced usage below) |
|
|
194
|
-
|
|
227
|
+
| `webhookUrl` | `string` | **Required.** Backend webhook URL (falls back to env vars if not provided) |
|
|
195
228
|
|
|
196
229
|
#### Optional Props
|
|
197
230
|
|
|
198
231
|
| Prop | Type | Default | Description |
|
|
199
232
|
|------|------|---------|-------------|
|
|
200
|
-
| `
|
|
201
|
-
| `botServerUrl` | `string` | - | Backend API URL (falls back to env vars if not provided) |
|
|
202
|
-
| `data` | `string` | - | URL-encoded config string (see advanced usage below) |
|
|
233
|
+
| `data` | `string` | - | Custom variables and auth URL in format `key1=value1~key2=value2~authUrl=https://...` |
|
|
203
234
|
| `width` | `string` | `"25vw"` | Widget width (CSS value) |
|
|
204
235
|
| `height` | `string` | `"90vh"` | Widget height (CSS value) |
|
|
205
236
|
| `right` | `string` | `"1.25rem"` | Distance from right edge |
|
|
@@ -225,29 +256,22 @@ function App() {
|
|
|
225
256
|
<ChatWidget
|
|
226
257
|
userId="user123"
|
|
227
258
|
sourceId="691e1b5952068ff7aaeccffc9"
|
|
259
|
+
webhookUrl="https://api.example.com/webhook"
|
|
228
260
|
/>
|
|
229
261
|
);
|
|
230
262
|
}
|
|
231
263
|
```
|
|
232
264
|
|
|
233
|
-
#### With
|
|
234
|
-
|
|
235
|
-
```jsx
|
|
236
|
-
<ChatWidget
|
|
237
|
-
userId="user123"
|
|
238
|
-
sourceId="691e1b5952068ff7aaeccffc9"
|
|
239
|
-
botServerUrl="https://api.example.com"
|
|
240
|
-
type="prime"
|
|
241
|
-
/>
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
#### With Custom Variables (NEW!)
|
|
265
|
+
#### With Custom Variables
|
|
245
266
|
|
|
246
267
|
Pass custom user data like username, phone number, etc. to the backend:
|
|
247
268
|
|
|
248
269
|
```jsx
|
|
249
270
|
<ChatWidget
|
|
250
|
-
|
|
271
|
+
userId="user123"
|
|
272
|
+
sourceId="691e1b5952068ff7aaeccffc9"
|
|
273
|
+
webhookUrl="https://api.example.com/webhook"
|
|
274
|
+
data="username=John Doe~telephone_number=+628123456789~email=john@example.com"
|
|
251
275
|
/>
|
|
252
276
|
```
|
|
253
277
|
|
|
@@ -267,13 +291,58 @@ Pass custom user data like username, phone number, etc. to the backend:
|
|
|
267
291
|
}
|
|
268
292
|
```
|
|
269
293
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
294
|
+
#### With Authentication (NEW!)
|
|
295
|
+
|
|
296
|
+
The widget supports automatic authentication with token refresh on expiry:
|
|
297
|
+
|
|
298
|
+
```jsx
|
|
299
|
+
<ChatWidget
|
|
300
|
+
userId="user123"
|
|
301
|
+
sourceId="691e1b5952068ff7aaeccffc9"
|
|
302
|
+
webhookUrl="https://api.example.com/webhook"
|
|
303
|
+
data="authUrl=https://api.example.com/login/chatwidget~username=John Doe"
|
|
304
|
+
/>
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
**How authentication works:**
|
|
308
|
+
|
|
309
|
+
1. **Initial Authentication**: When the widget loads (on Launcher mount), if `authUrl` is provided in the `data` prop, it automatically calls the auth API:
|
|
310
|
+
```
|
|
311
|
+
POST {authUrl}
|
|
312
|
+
Body: { "user_id": "user123" }
|
|
313
|
+
Response: { "token": "eyJ..." }
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
2. **Token Usage**: The received token is automatically included in all webhook API calls as `authToken` in the `variables` object:
|
|
317
|
+
```json
|
|
318
|
+
{
|
|
319
|
+
"chat_id": "...",
|
|
320
|
+
"user_id": "user123",
|
|
321
|
+
"message": "Hello",
|
|
322
|
+
"variables": {
|
|
323
|
+
"authToken": "eyJ...",
|
|
324
|
+
"username": "John Doe"
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
```
|
|
275
328
|
|
|
276
|
-
**
|
|
329
|
+
3. **Automatic Token Refresh**: If the webhook returns **401 Unauthorized** (token expired/revoked), the widget automatically:
|
|
330
|
+
- Calls the `authUrl` again to get a fresh token
|
|
331
|
+
- Updates the `authToken` in variables
|
|
332
|
+
- Retries the failed request with the new token
|
|
333
|
+
- This happens transparently without user interaction
|
|
334
|
+
|
|
335
|
+
**Combined example with auth and custom variables:**
|
|
336
|
+
```jsx
|
|
337
|
+
<ChatWidget
|
|
338
|
+
userId="user123"
|
|
339
|
+
sourceId="691e1b5952068ff7aaeccffc9"
|
|
340
|
+
webhookUrl="https://api.example.com/webhook"
|
|
341
|
+
data="authUrl=https://api.example.com/login/chatwidget~username=John~email=john@example.com~phone=+628123456789"
|
|
342
|
+
/>
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
**Note:** The `data` prop is for optional parameters only (custom variables and `authUrl`). Required parameters (`userId`, `sourceId`, `webhookUrl`) must be passed as direct props.
|
|
277
346
|
|
|
278
347
|
#### Custom Styling
|
|
279
348
|
|
|
@@ -342,92 +411,170 @@ function App() {
|
|
|
342
411
|
|
|
343
412
|
---
|
|
344
413
|
|
|
345
|
-
The `data` prop allows you to pass
|
|
414
|
+
The `data` prop allows you to pass custom variables and authentication URL via a single string. This is especially useful when you need to dynamically pass user-specific data or pass it through URL parameters.
|
|
346
415
|
|
|
347
|
-
**Format:** `key
|
|
416
|
+
**Format:** `key=value` pairs separated by `~`
|
|
348
417
|
|
|
349
418
|
**Example data string:**
|
|
350
419
|
```
|
|
351
|
-
|
|
420
|
+
username=John Doe~phone=+1234567890~email=john@example.com
|
|
352
421
|
```
|
|
353
422
|
|
|
423
|
+
**With authentication:**
|
|
424
|
+
```
|
|
425
|
+
authUrl=https://api.example.com/login/chatwidget~username=John Doe~email=john@example.com
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
**Special keys:**
|
|
429
|
+
- `authUrl`: Authentication endpoint URL (see "With Authentication" section above)
|
|
430
|
+
- All other keys: Custom variables passed to webhook in `variables` object
|
|
431
|
+
|
|
354
432
|
#### Building Data String Programmatically
|
|
355
433
|
|
|
356
|
-
Instead of manually constructing the data string, you can create a helper function to build it dynamically:
|
|
434
|
+
Instead of manually constructing the data string, you can create a helper function to build it dynamically. This approach is recommended for production applications as it:
|
|
435
|
+
- Prevents syntax errors in the data string format
|
|
436
|
+
- Makes the code more maintainable and readable
|
|
437
|
+
- Allows for conditional inclusion of parameters
|
|
438
|
+
- Handles URL encoding automatically if needed
|
|
439
|
+
|
|
440
|
+
**Create a helper function** (`src/helpers/chatWidget.js` or similar):
|
|
441
|
+
|
|
442
|
+
```jsx
|
|
443
|
+
/**
|
|
444
|
+
* Builds the data string for ChatWidget component
|
|
445
|
+
* @param {Object} params - Object containing all parameters
|
|
446
|
+
* @param {string} params.authUrl - Optional: Authentication endpoint URL
|
|
447
|
+
* @param {string} params.username - Optional: User's display name
|
|
448
|
+
* @param {string} params.email - Optional: User's email address
|
|
449
|
+
* @param {string} params.phone - Optional: User's phone number
|
|
450
|
+
* @param {Object} params.customFields - Optional: Any additional custom fields as key-value pairs
|
|
451
|
+
* @returns {string} Formatted data string for ChatWidget (always returns a string)
|
|
452
|
+
*
|
|
453
|
+
* @example
|
|
454
|
+
* // Returns: "authUrl=https://api.com~username=John~email=john@example.com"
|
|
455
|
+
* buildChatWidgetData({
|
|
456
|
+
* authUrl: "https://api.com",
|
|
457
|
+
* username: "John",
|
|
458
|
+
* email: "john@example.com"
|
|
459
|
+
* });
|
|
460
|
+
*/
|
|
461
|
+
export const buildChatWidgetData = ({
|
|
462
|
+
authUrl,
|
|
463
|
+
username,
|
|
464
|
+
email,
|
|
465
|
+
phone,
|
|
466
|
+
customFields = {}
|
|
467
|
+
}) => {
|
|
468
|
+
const parts = [];
|
|
469
|
+
|
|
470
|
+
// Add authUrl first if provided (for authentication)
|
|
471
|
+
if (authUrl) {
|
|
472
|
+
parts.push(`authUrl=${authUrl}`);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// Add standard fields
|
|
476
|
+
if (username) {
|
|
477
|
+
parts.push(`username=${encodeURIComponent(username)}`);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if (email) {
|
|
481
|
+
parts.push(`email=${encodeURIComponent(email)}`);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
if (phone) {
|
|
485
|
+
parts.push(`phone=${encodeURIComponent(phone)}`);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// Add any custom fields dynamically
|
|
489
|
+
// Note: customFields is just a convenience parameter for the helper function
|
|
490
|
+
// Each field will be flattened into the string format: key=value~key2=value2
|
|
491
|
+
Object.entries(customFields).forEach(([key, value]) => {
|
|
492
|
+
if (value) {
|
|
493
|
+
parts.push(`${key}=${encodeURIComponent(String(value))}`);
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
// Returns a string like: "authUrl=...~username=John~email=john@example.com~department=Engineering"
|
|
498
|
+
return parts.join("~");
|
|
499
|
+
};
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
**Important:** The `customFields` parameter is **NOT** passed as an object to the widget. It's just a convenient way to pass multiple additional fields to the helper function. The function flattens everything into a single string format.
|
|
503
|
+
|
|
504
|
+
**Usage in your application:**
|
|
357
505
|
|
|
358
|
-
**Usage:**
|
|
359
506
|
```jsx
|
|
360
507
|
import { ChatWidget } from "@data-netmonk/mona-chat-widget";
|
|
508
|
+
import "@data-netmonk/mona-chat-widget/dist/style.css";
|
|
361
509
|
import { buildChatWidgetData } from "./helpers/chatWidget";
|
|
362
510
|
|
|
363
511
|
function App() {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
username: "John Doe",
|
|
512
|
+
// Get user data from your auth system, state, or props
|
|
513
|
+
const user = {
|
|
514
|
+
id: "user123",
|
|
515
|
+
name: "John Doe",
|
|
369
516
|
email: "john@example.com",
|
|
370
517
|
phone: "+628123456789"
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
// Build the data string
|
|
521
|
+
const customData = buildChatWidgetData({
|
|
522
|
+
authUrl: "https://api.example.com/login/chatwidget",
|
|
523
|
+
username: user.name,
|
|
524
|
+
email: user.email,
|
|
525
|
+
phone: user.phone,
|
|
526
|
+
customFields: {
|
|
527
|
+
department: "Engineering",
|
|
528
|
+
role: "Developer",
|
|
529
|
+
company: "Acme Corp"
|
|
530
|
+
}
|
|
371
531
|
});
|
|
532
|
+
|
|
533
|
+
// customData is now a STRING like:
|
|
534
|
+
// "authUrl=https://api.example.com/login/chatwidget~username=John%20Doe~email=john@example.com~phone=%2B628123456789~department=Engineering~role=Developer~company=Acme%20Corp"
|
|
372
535
|
|
|
373
|
-
return
|
|
536
|
+
return (
|
|
537
|
+
<ChatWidget
|
|
538
|
+
userId={user.id}
|
|
539
|
+
sourceId="691e1b5952068ff7aaeccffc9"
|
|
540
|
+
webhookUrl="https://api.example.com/webhook"
|
|
541
|
+
data={customData} // Pass the STRING to data prop
|
|
542
|
+
/>
|
|
543
|
+
);
|
|
374
544
|
}
|
|
375
545
|
```
|
|
376
546
|
|
|
377
|
-
|
|
378
|
-
- Accept configuration parameters (userId, sourceId, botServerUrl, and any custom variables)
|
|
379
|
-
- Build the data string in format: `key=value` pairs separated by `~`
|
|
380
|
-
- Use `url:` prefix (not `url=`) for the backend URL
|
|
381
|
-
- Return the formatted string
|
|
382
|
-
|
|
383
|
-
#### Using with URL Parameters (Optional)
|
|
384
|
-
|
|
385
|
-
If you need to pass the data through URL parameters:
|
|
547
|
+
**With conditional authentication:**
|
|
386
548
|
|
|
387
549
|
```jsx
|
|
388
|
-
|
|
389
|
-
const
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
<ChatWidget data={dataParam} />
|
|
393
|
-
```
|
|
394
|
-
|
|
395
|
-
**URL example:**
|
|
396
|
-
```
|
|
397
|
-
https://yourapp.com/chat?data=url:https://api.example.com~user_id=user123~source_id=abc~username=John~phone=%2B628123456789
|
|
398
|
-
```
|
|
399
|
-
|
|
400
|
-
#### Real-World Example from ui-mona-web
|
|
401
|
-
|
|
402
|
-
See the complete helper implementation in `ui-mona-web/src/helpers/chatWidget.js`:
|
|
403
|
-
|
|
404
|
-
```jsx
|
|
405
|
-
import { getUserId } from './cookie';
|
|
406
|
-
|
|
407
|
-
export const buildChatWidgetData = ({ loginType, sourceId, userId, botServerUrl }) => {
|
|
408
|
-
// Get userId from cookies if not provided
|
|
409
|
-
const finalUserId = userId || getUserId();
|
|
410
|
-
|
|
411
|
-
// Get sourceId based on loginType
|
|
412
|
-
const finalSourceId = sourceId || getSourceIdByLoginType(loginType);
|
|
413
|
-
|
|
414
|
-
// Get bot server URL from env
|
|
415
|
-
const finalBotServerUrl = import.meta.env.VITE_CHAT_WIDGET_WEBHOOK_URL || botServerUrl;
|
|
416
|
-
|
|
417
|
-
const parts = [
|
|
418
|
-
`user_id=${String(finalUserId)}`,
|
|
419
|
-
`source_id=${finalSourceId}`,
|
|
420
|
-
];
|
|
421
|
-
|
|
422
|
-
if (finalBotServerUrl) {
|
|
423
|
-
parts.unshift(`url:${finalBotServerUrl}`);
|
|
424
|
-
}
|
|
550
|
+
function App() {
|
|
551
|
+
const authUrl = import.meta.env.VITE_CHAT_AUTH_ENABLED === "true"
|
|
552
|
+
? import.meta.env.VITE_CHAT_AUTH_URL
|
|
553
|
+
: null;
|
|
425
554
|
|
|
426
|
-
|
|
427
|
-
|
|
555
|
+
const customData = buildChatWidgetData({
|
|
556
|
+
authUrl: authUrl, // Only included if authentication is enabled
|
|
557
|
+
username: getCurrentUser().name,
|
|
558
|
+
email: getCurrentUser().email
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
return (
|
|
562
|
+
<ChatWidget
|
|
563
|
+
userId={getCurrentUser().id}
|
|
564
|
+
sourceId="691e1b5952068ff7aaeccffc9"
|
|
565
|
+
webhookUrl={import.meta.env.VITE_WEBHOOK_URL}
|
|
566
|
+
data={customData}
|
|
567
|
+
/>
|
|
568
|
+
);
|
|
569
|
+
}
|
|
428
570
|
```
|
|
429
571
|
|
|
430
|
-
|
|
572
|
+
**The helper function handles:**
|
|
573
|
+
- ✅ Proper formatting with `~` separators
|
|
574
|
+
- ✅ URL encoding for special characters
|
|
575
|
+
- ✅ Conditional parameter inclusion (only adds if value exists)
|
|
576
|
+
- ✅ Support for dynamic custom fields
|
|
577
|
+
- ✅ Type safety and documentation via JSDoc comments
|
|
431
578
|
|
|
432
579
|
### Standalone app (for demonstration)
|
|
433
580
|
|