@ujet/websdk-headless 3.0.3

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 ADDED
@@ -0,0 +1,757 @@
1
+ # Headless WebSDK
2
+
3
+
4
+ ## Install
5
+
6
+ ```
7
+ npm install @ujet/websdk-headless --save
8
+ ```
9
+
10
+ ## Usage
11
+
12
+ ```js
13
+ import { Client } from "@ujet/websdk-headless"
14
+
15
+ async function authenticate() {
16
+ const resp = await fetch("/your-auth-endpoint")
17
+ const data = await resp.json()
18
+ return { token: data.token }
19
+ }
20
+
21
+ const client = new Client({
22
+ companyId: "YOUR-COMPANY-ID",
23
+ tenant: "YOUR-TENANT-NAME",
24
+ authenticate: authenticate,
25
+ })
26
+
27
+ // const company = await client.getCompany()
28
+ // const menus = await client.getMenus()
29
+ ```
30
+
31
+ Available options for `Client` class:
32
+
33
+ ```ts
34
+ interface ClientOption {
35
+ companyId: string;
36
+ authenticate: () => Promise<TokenResponse>;
37
+ tenant?: string;
38
+ host?: string;
39
+ lang?: string;
40
+ bridge?: string;
41
+ cobrowse?: {
42
+ enabled: boolean;
43
+ messages?: CobrowseMessages;
44
+ api?: string;
45
+ license?: string;
46
+ trustedOrigins?: string[];
47
+ capabilities?: string[];
48
+ registration?: boolean;
49
+ redactedViews?: string[];
50
+ unredactedViews?: string[];
51
+ };
52
+ }
53
+ ```
54
+
55
+
56
+ ## Methods
57
+
58
+ All methods available for `client` instance.
59
+
60
+ ### `getTrigger`
61
+
62
+ This method is used to detect current page proactive triggers. It will
63
+ return current matched trigger or `null`.
64
+
65
+ ```ts
66
+ const trigger = await client.getTrigger()
67
+ ```
68
+
69
+ ### `getCompany`
70
+
71
+ This method is used to get the company information.
72
+
73
+ ```ts
74
+ const company = await client.getCompany()
75
+ ```
76
+
77
+ ```ts
78
+ interface CompanyResponse {
79
+ name: string;
80
+ subdomain: string;
81
+ support_email: string;
82
+ languages: LanguageOption[];
83
+ action_tracking: boolean;
84
+ email_transcripts: boolean;
85
+ message_preview: boolean;
86
+ email_enhancement: boolean;
87
+ }
88
+ ```
89
+
90
+ ### `getMenus`
91
+
92
+ This method is used to list all the menu items available for the tenant.
93
+
94
+ Request parameter:
95
+
96
+ ```js
97
+ (key?: string, lang?: string)
98
+ ```
99
+
100
+ Example:
101
+
102
+ ```ts
103
+ const data = await client.getMenus("direct_menu_key")
104
+ console.log(data.menus)
105
+ console.log(data.direct)
106
+ ```
107
+
108
+ Response data:
109
+
110
+ ```ts
111
+ interface MenuItem {
112
+ id: number;
113
+ name?: string;
114
+ enabled: boolean;
115
+ redirection?: {
116
+ option: string;
117
+ data: string;
118
+ };
119
+ children?: MenuItem[];
120
+ channels: MenuChannel[];
121
+ deflection?: {
122
+ enabled: boolean;
123
+ type: string;
124
+ };
125
+ }
126
+
127
+ interface MenuResponse {
128
+ menus: MenuItem[];
129
+ direct: {
130
+ key: boolean;
131
+ user: boolean;
132
+ };
133
+ }
134
+ ```
135
+
136
+ ### `getTimeSlots`
137
+
138
+ This method is used to get available time slots for scheduled calls.
139
+
140
+ Request parameter:
141
+
142
+ ```js
143
+ (menuId: number | string, lang?: string)
144
+ ```
145
+
146
+ Example:
147
+
148
+ ```ts
149
+ const slots = await client.getTimeSlots(123)
150
+ ```
151
+
152
+ Response data example:
153
+
154
+ ```json
155
+ [
156
+ "2023-05-31 11:45 +0000",
157
+ "2023-05-31 12:00 +0000",
158
+ "2023-05-31 12:15 +0000",
159
+ "2023-05-31 12:30 +0000",
160
+ "2023-05-31 12:45 +0000",
161
+ "2023-05-31 13:00 +0000",
162
+ "2023-05-31 13:15 +0000",
163
+ "2023-05-31 13:30 +0000",
164
+ "2023-05-31 13:45 +0000",
165
+ "2023-05-31 14:00 +0000"
166
+ ]
167
+ ```
168
+
169
+
170
+ ### `createCall`
171
+
172
+ This method is used to create an instant/scheduled call.
173
+
174
+ Request parameter:
175
+
176
+ ```ts
177
+ interface CallRequest {
178
+ phone_number: string;
179
+ lang?: string;
180
+ scheduled_at?: string;
181
+ ticket_id?: string;
182
+ email?: string;
183
+ recording_permission?: boolean;
184
+ }
185
+
186
+ (menuId: number | string, data: CallRequest)
187
+ ```
188
+
189
+ When `scheduled_at` is set, it will create a scheduled call.
190
+
191
+ 1. create an instant call:
192
+
193
+ ```ts
194
+ const call = await client.createCall(123, {
195
+ lang: 'en',
196
+ phone_number: '+12345678',
197
+ })
198
+ ```
199
+
200
+ 2. create a scheduled call:
201
+
202
+ ```ts
203
+ const call = await client.createCall(123, {
204
+ lang: 'en',
205
+ phone_number: '+12345678',
206
+ scheduled_at: '2023-05-31T11:45+0000',
207
+ })
208
+ ```
209
+
210
+ ### `loadCall`
211
+
212
+ This method is used to get the call information of the given call ID.
213
+
214
+ Request parameter:
215
+
216
+ ```ts
217
+ (callId: number | string)
218
+ ```
219
+
220
+ Example:
221
+
222
+ ```ts
223
+ const call = await client.loadCall(1234)
224
+ ```
225
+
226
+ ### `cancelCall`
227
+
228
+ This method is used to cancel a call, usually used for cancelling a scheduled call.
229
+
230
+ Request parameter:
231
+
232
+ ```ts
233
+ (callId: number | string)
234
+ ```
235
+
236
+ Example:
237
+
238
+ ```ts
239
+ client.cancelCall(1234)
240
+ ```
241
+
242
+ ### `createChat`
243
+
244
+ This method is used to create a new chat.
245
+
246
+ Request parameter:
247
+
248
+ ```ts
249
+ interface ChatRequest {
250
+ lang?: string;
251
+ trigger_id?: string;
252
+ ticket_id?: string;
253
+ email?: string;
254
+ greeting?: string;
255
+ cobrowsable?: boolean;
256
+ }
257
+
258
+ (menuId: number | string, data: ChatRequest, customData?: string | object)
259
+ ```
260
+
261
+ An example:
262
+
263
+ ```ts
264
+ client.createChat(123, { lang: 'en' })
265
+ ```
266
+
267
+ ### `loadChat`
268
+
269
+ This method is used to get the chat information of the given chat ID.
270
+
271
+ Request parameter:
272
+
273
+ ```ts
274
+ (chatId: number | string)
275
+
276
+ // client.loadChat(1234)
277
+ ```
278
+
279
+ ### `resumeChat`
280
+
281
+ This method is used to resume the chat when chat is "dismissed".
282
+
283
+ Request parameter:
284
+
285
+ ```ts
286
+ (chatId: number | string)
287
+
288
+ // client.resumeChat(1234)
289
+ ```
290
+
291
+ ### `loadOngoingChat`
292
+
293
+ This method is used to get the ongoing chat information. The client
294
+ will save current ongoing chat ID in browser's localStorage, this
295
+ method will try to load the ongoing chat information.
296
+
297
+ ```ts
298
+ const chat = await client.loadOngoingChat()
299
+ ```
300
+
301
+ ### `escalateChat`
302
+
303
+ This method will transfer the chat from virtual agent to human agent.
304
+
305
+ ```ts
306
+ client.escalateChat()
307
+ ```
308
+
309
+ ### `finishChat`
310
+
311
+ This method will change the chat status to `finished`.
312
+
313
+ ```ts
314
+ client.finishChat()
315
+ ```
316
+
317
+ Some methods are called after `finishChat`, e.g. `sendChatSurvey`, `sendChatRate`.
318
+
319
+ ### `destroyChat`
320
+
321
+ This method will destroy current ongoing chat.
322
+
323
+ ```ts
324
+ client.destroyChat()
325
+ ```
326
+
327
+ ### `sendTextMessage`
328
+
329
+ Send a text message:
330
+
331
+ ```ts
332
+ client.sendTextMessage("hello world")
333
+ ```
334
+
335
+ ### `sendFileMessage`
336
+
337
+ Send a file message:
338
+
339
+ ```ts
340
+ const input = document.querySelector('input[type="file"]')
341
+ const file = input.files[0]
342
+ client.sendFileMessage(file)
343
+ ```
344
+
345
+ ### `startCobrowse`
346
+
347
+ Request starging cobrowse:
348
+
349
+ ```ts
350
+ client.startCobrowse()
351
+ ```
352
+
353
+ ### `createCobrowseCode`
354
+
355
+ Create cobrowse code digits:
356
+
357
+ ```ts
358
+ client.createCobrowseCode()
359
+ ```
360
+
361
+ ### `sendChatTranscript`
362
+
363
+ Send current chat transcript to the given emails.
364
+
365
+ ```ts
366
+ client.sendChatTranscript([
367
+ "name1@example.com",
368
+ "name2@example.com",
369
+ ])
370
+ ```
371
+
372
+ ### `sendChatRate`
373
+
374
+ This method will send user feedback for current chat, when chat is finished.
375
+
376
+ Request parameter:
377
+
378
+ ```ts
379
+ client.sendChatRate({
380
+ rating: 5,
381
+ feedback: "Very good service",
382
+ })
383
+ ```
384
+
385
+ ### `sendChatSurvey`
386
+
387
+ This method will also send user feedbacks, but powerful.
388
+
389
+ Request parameter:
390
+
391
+ ```ts
392
+ interface {
393
+ [question_id: string]: number | string;
394
+ }
395
+ ```
396
+
397
+ 1. you will need to get the survey questions:
398
+
399
+ ```ts
400
+ const questions = await client.getChatSurvey()
401
+ /* response:
402
+ {
403
+ sign_off_display_text: "some text",
404
+ questions: [
405
+ {
406
+ id: number,
407
+ type: string,
408
+ display_text: string,
409
+ valid_answers?
410
+ }
411
+ ]
412
+ }
413
+ */
414
+ ```
415
+
416
+ 2. answer each questions, and call `sendChatSurvey`:
417
+
418
+ ```ts
419
+ client.sendChatSurvey({
420
+ 123: "a",
421
+ 231: "b",
422
+ })
423
+ ```
424
+
425
+ ### `createEmail`
426
+
427
+ This method will send an email:
428
+
429
+ Request parameter:
430
+
431
+ ```ts
432
+ interface EmailRequest {
433
+ name?: string;
434
+ email: string;
435
+ content: string;
436
+ lang?: string;
437
+ files?: File[];
438
+ recaptcha?: string;
439
+ }
440
+
441
+ (menuId: number | string, data: EmailRequest)
442
+ ```
443
+
444
+ ```ts
445
+ client.createEmail(123, {
446
+ lang: "en",
447
+ name: "User name",
448
+ email: "name@example.com",
449
+ content: "description of the question",
450
+ files: input.files,
451
+ })
452
+ ```
453
+
454
+ ## Events
455
+
456
+ It is possible to add and remove event listners via `.on` and `.off`
457
+ methods:
458
+
459
+ ```js
460
+ const handleReady = () => {
461
+ console.log("**** client is ready")
462
+ }
463
+
464
+ client.on("ready", handleReady)
465
+
466
+ // client.off("ready", handleReady)
467
+ ```
468
+
469
+ Here lists all the available events:
470
+
471
+ ### ready
472
+
473
+ Emit when client is ready for communication.
474
+
475
+ ```js
476
+ client.on("ready", () => {
477
+ })
478
+ ```
479
+
480
+ ### authenticated
481
+
482
+ Emit when client has authenticated with the user's token.
483
+
484
+ ```js
485
+ client.on("authenticated", () => {
486
+ })
487
+ ```
488
+
489
+ ### chat.updated
490
+
491
+ Emit when `chat` instance has updated.
492
+
493
+ ```js
494
+ client.on("chat.updated", (chat) => {
495
+ // the `chat` property on `client` has updated
496
+ console.log(chat)
497
+ })
498
+ ```
499
+
500
+ ### chat.message
501
+
502
+ Emit when there is a new message.
503
+
504
+ ```js
505
+ client.on("chat.message", message => {
506
+ console.log(message)
507
+ })
508
+ ```
509
+
510
+ The `message` type:
511
+
512
+ ```ts
513
+ interface MessageResponse {
514
+ $index: number;
515
+ $sid: string;
516
+ $timestamp: Date;
517
+ $userType: string;
518
+ $userId: number;
519
+ type: string;
520
+ content?: string;
521
+ event?: string;
522
+ file?: File;
523
+
524
+ // extra parameters
525
+ [key: string]: unknown;
526
+ }
527
+ ```
528
+
529
+ ### chat.memberJoined
530
+
531
+ Emit when a new member joined the conversation.
532
+
533
+ ```js
534
+ client.on("chat.memberJoined", (identity) => {
535
+ console.log(identity)
536
+ })
537
+ ```
538
+
539
+ ### chat.memberLeft
540
+
541
+ Emit when a member left the conversation.
542
+
543
+ ```js
544
+ client.on("chat.memberLeft", (identity) => {
545
+ console.log(identity)
546
+ })
547
+ ```
548
+
549
+ ### chat.typingStarted
550
+
551
+ Emit when a member started typing.
552
+
553
+ ```js
554
+ client.on("chat.typingStarted", (identity) => {
555
+ console.log(identity)
556
+ })
557
+ ```
558
+
559
+ ### chat.typingEnded
560
+
561
+ Emit when a member ended typing.
562
+
563
+ ```js
564
+ client.on("chat.typingEnded", (identity) => {
565
+ console.log(identity)
566
+ })
567
+ ```
568
+
569
+ ### chat.connected
570
+
571
+ Emit when the chat is connected with conversation provider.
572
+
573
+ ```js
574
+ client.on("chat.connected", () => {
575
+ console.log("connected")
576
+ })
577
+ ```
578
+
579
+ ### chat.disconnected
580
+
581
+ Emit when the chat is disconnected with conversation provider.
582
+
583
+ ```js
584
+ client.on("chat.disconnected", () => {
585
+ console.log("disconnected")
586
+ })
587
+ ```
588
+
589
+ ### chat.dismissed
590
+
591
+ Emit when the chat's status is changed to `dismissed`.
592
+
593
+ ```js
594
+ client.on("chat.dismissed", () => {
595
+ console.log("dismissed")
596
+ })
597
+ ```
598
+
599
+ ### chat.timeout
600
+
601
+ Emit when the chat is ended, and the reason is "timeout".
602
+
603
+ ```js
604
+ client.on("chat.timeout", () => {
605
+ console.log("timeout")
606
+ })
607
+ ```
608
+
609
+ ### chat.ended
610
+
611
+ Emit when the chat is ended.
612
+
613
+ ```js
614
+ client.on("chat.ended", () => {
615
+ console.log("ended")
616
+ })
617
+ ```
618
+
619
+ ### chat.destroyed
620
+
621
+ Emit when `destroyChat` is called.
622
+
623
+ ```js
624
+ client.on("chat.destroyed", () => {
625
+ console.log("destroyed")
626
+ })
627
+ ```
628
+
629
+ ### cobrowse.request
630
+
631
+ Emit when end user or agent request to start cobrowse.
632
+
633
+ ```js
634
+ client.on("cobrowse.request", { from } => {
635
+ console.log("request by", from)
636
+ })
637
+ ```
638
+
639
+ ### cobrowse.loaded
640
+
641
+ When cobrowse session is loaded.
642
+
643
+ ```js
644
+ client.on("cobrowse.loaded", session => {
645
+ console.log("cobrowse session", session)
646
+ })
647
+ ```
648
+
649
+ ### cobrowse.updated
650
+
651
+ When cobrowse session is updated.
652
+
653
+ ```js
654
+ client.on("cobrowse.updated", session => {
655
+ console.log("cobrowse session", session)
656
+ })
657
+ ```
658
+
659
+ ### cobrowse.ended
660
+
661
+ When cobrowse session is ended.
662
+
663
+ ```js
664
+ client.on("cobrowse.ended", session => {
665
+ console.log("cobrowse session", session)
666
+ })
667
+ ```
668
+
669
+ ## Cobrowse
670
+
671
+ Headless WebSDK has built-in integration with co-browse. It has a simple UI without
672
+ CSS, developers SHOULD add style for it.
673
+
674
+ The default template is:
675
+
676
+ ```html
677
+ <dialog open class="cobrowse-dialog">
678
+ <h1>$title</h1>
679
+ <div class="cobrowse-dialog_content">$content</div>
680
+ <div class="cobrowse-dialog_footer">
681
+ <button class="cobrowse-dialog_allow js-cobrowse-allow">$allow</button>
682
+ <button class="cobrowse-dialog_deny js-cobrowse-deny">$deny</button>
683
+ </div>
684
+ </dialog>
685
+ ```
686
+
687
+ The template will be wrapped by a `<div>` tag:
688
+
689
+ ```html
690
+ <div class="cobrowse-wrapper">${template}</div>
691
+ ```
692
+
693
+ You can add CSS style according to the above template's class names.
694
+
695
+ ### Custom template
696
+
697
+ Developers can also provide a custom template with `class="cobrowse-template"`, e.g.
698
+
699
+ ```html
700
+ <script class="cobrowse-template" type="text/template">
701
+ <div class="cobrowse">
702
+ <div class="cobrowse-title">$title</div>
703
+ <div class="cobrowse-content">$content</div>
704
+ <div class="cobrowse-footer">
705
+ <button class="cobrowse-deny js-cobrowse-deny">$deny</button>
706
+ <button class="cobrowse-allow js-cobrowse-allow">$allow</button>
707
+ </div>
708
+ </div>
709
+ </script>
710
+ ```
711
+
712
+ Variables `$title`, `$content`, `$deny`, and `$allow` will be replaced automatically.
713
+
714
+ **Note**: `js-cobrowse-deny` and `js-cobrowse-allow` class names are required.
715
+
716
+ ### Custom messages
717
+
718
+ We know `$title`, `$content`, `$deny`, and `$allow` are variables, the default (en) messages
719
+ for these variables are:
720
+
721
+ ```
722
+ {
723
+ "confirmSessionTitle": "Co-browse Session Request",
724
+ "confirmSessionContent": "Do you want to share your current screen with the agent?",
725
+ "endSessionText": "End Co-browse Session",
726
+ "confirmRemoteControlTitle": "Remote Access Request",
727
+ "confirmRemoteControlContent": "The agent would like to have access to your currently shared screen to further assist you. Do you want to allow this?",
728
+ "confirmFullDeviceTitle": "Screen Share Request",
729
+ "confirmFullDeviceContent": "Do you want to share your full screen with the agent? The agent will not be able to control anything on the screen.",
730
+ "allowText": "Allow",
731
+ "denyText": "Deny"
732
+ }
733
+ ```
734
+
735
+ You can customize the message through option `cobrowse.messages`:
736
+
737
+ ```ts
738
+ const client = new Client({
739
+ companyId: "YOUR-COMPANY-ID",
740
+ tenant: "YOUR-TENANT-NAME",
741
+ authenticate: authenticate,
742
+ cobrowse: {
743
+ enabled: true,
744
+ messages: {
745
+ confirmSessionTitle: "...",
746
+ confirmSessionContent: "...",
747
+ endSessionText: "...",
748
+ confirmRemoteControlTitle: "...",
749
+ confirmRemoteControlContent: "...",
750
+ confirmFullDeviceTitle: "...",
751
+ confirmFullDeviceContent: "...",
752
+ allowText: "...",
753
+ denyText: "...",
754
+ }
755
+ }
756
+ })
757
+ ```