@buni.ai/chatbot-angular 1.0.0 → 1.0.2

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,556 @@
1
+ # @buni.ai/chatbot-angular
2
+
3
+ Angular components and services for seamless BuniAI chat widget integration.
4
+
5
+ [![npm version](https://badge.fury.io/js/@buni.ai%2Fchatbot-angular.svg)](https://badge.fury.io/js/@buni.ai%2Fchatbot-angular)
6
+ [![Angular](https://img.shields.io/badge/Angular-15%2B-DD0031.svg)](https://angular.io/)
7
+ [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-3178C6.svg)](https://www.typescriptlang.org/)
8
+
9
+ ## 🚀 Quick Start
10
+
11
+ ### Installation
12
+
13
+ ```bash
14
+ npm install @buni.ai/chatbot-angular
15
+ ```
16
+
17
+ ### Basic Usage
18
+
19
+ #### Option 1: Using the Service (Recommended)
20
+
21
+ ```typescript
22
+ // app.component.ts
23
+ import { Component, OnInit } from '@angular/core';
24
+ import { BuniChatService } from '@buni.ai/chatbot-angular';
25
+ import { Observable } from 'rxjs';
26
+
27
+ @Component({
28
+ selector: 'app-root',
29
+ template: `
30
+ <div>
31
+ <h1>My Angular App</h1>
32
+
33
+ <button (click)="toggle()">
34
+ {{ (isOpen$ | async) ? 'Close Chat' : 'Open Chat' }}
35
+ </button>
36
+
37
+ <button (click)="setCustomer()">
38
+ Set Customer Data
39
+ </button>
40
+
41
+ <p>Unread messages: {{ unreadCount$ | async }}</p>
42
+ <p>Widget ready: {{ (isReady$ | async) ? 'Yes' : 'No' }}</p>
43
+ </div>
44
+ `
45
+ })
46
+ export class AppComponent implements OnInit {
47
+ isOpen$: Observable<boolean>;
48
+ isReady$: Observable<boolean>;
49
+ unreadCount$: Observable<number>;
50
+
51
+ constructor(private buniChatService: BuniChatService) {
52
+ this.isOpen$ = this.buniChatService.isOpen$;
53
+ this.isReady$ = this.buniChatService.isReady$;
54
+ this.unreadCount$ = this.buniChatService.unreadCount$;
55
+ }
56
+
57
+ async ngOnInit() {
58
+ await this.buniChatService.initialize({
59
+ token: 'your-widget-token',
60
+ config: {
61
+ position: 'bottom-right',
62
+ theme: 'light',
63
+ primaryColor: '#007bff'
64
+ },
65
+ onReady: () => {
66
+ console.log('✅ BuniAI widget loaded!');
67
+ },
68
+ onNewMessage: (data) => {
69
+ console.log('💬 New message:', data);
70
+ }
71
+ });
72
+ }
73
+
74
+ toggle() {
75
+ this.buniChatService.toggle();
76
+ }
77
+
78
+ setCustomer() {
79
+ this.buniChatService.setCustomerData({
80
+ name: 'John Doe',
81
+ email: 'john@example.com',
82
+ userId: '12345'
83
+ });
84
+ }
85
+ }
86
+ ```
87
+
88
+ #### Option 2: Using the Component
89
+
90
+ ```typescript
91
+ // app.component.ts
92
+ import { Component } from '@angular/core';
93
+
94
+ @Component({
95
+ selector: 'app-root',
96
+ template: `
97
+ <div>
98
+ <h1>My Angular App</h1>
99
+ <buni-chat-widget
100
+ token="your-widget-token"
101
+ [config]="{
102
+ position: 'bottom-right',
103
+ theme: 'light'
104
+ }"
105
+ [onReady]="onReady"
106
+ [onNewMessage]="onNewMessage">
107
+ </buni-chat-widget>
108
+ </div>
109
+ `
110
+ })
111
+ export class AppComponent {
112
+ onReady = () => {
113
+ console.log('Widget is ready!');
114
+ };
115
+
116
+ onNewMessage = (data: any) => {
117
+ console.log('New message received:', data);
118
+ };
119
+ }
120
+ ```
121
+
122
+ #### Option 3: Using the Module
123
+
124
+ ```typescript
125
+ // app.module.ts
126
+ import { NgModule } from '@angular/core';
127
+ import { BrowserModule } from '@angular/platform-browser';
128
+ import { BuniChatModule } from '@buni.ai/chatbot-angular';
129
+
130
+ import { AppComponent } from './app.component';
131
+
132
+ @NgModule({
133
+ declarations: [AppComponent],
134
+ imports: [
135
+ BrowserModule,
136
+ BuniChatModule
137
+ ],
138
+ providers: [],
139
+ bootstrap: [AppComponent]
140
+ })
141
+ export class AppModule { }
142
+ ```
143
+
144
+ ```html
145
+ <!-- app.component.html -->
146
+ <div>
147
+ <h1>My Angular App</h1>
148
+ <buni-chat-widget token="your-widget-token"></buni-chat-widget>
149
+ <buni-chat-button
150
+ [showText]="true"
151
+ openText="Chat with us"
152
+ closeText="Close chat"
153
+ (clicked)="onChatToggled($event)">
154
+ </buni-chat-button>
155
+ </div>
156
+ ```
157
+
158
+ ## 📖 API Reference
159
+
160
+ ### `BuniChatService`
161
+
162
+ Injectable service for managing the BuniAI chat widget.
163
+
164
+ #### Properties
165
+
166
+ ```typescript
167
+ class BuniChatService {
168
+ // Observable state
169
+ state$: Observable<BuniChatState>;
170
+ isOpen$: Observable<boolean>;
171
+ isReady$: Observable<boolean>;
172
+ isMinimized$: Observable<boolean>;
173
+ unreadCount$: Observable<number>;
174
+ }
175
+ ```
176
+
177
+ #### Methods
178
+
179
+ | Method | Parameters | Returns | Description |
180
+ |--------|------------|---------|-------------|
181
+ | `initialize` | `options: BuniChatOptions` | `Promise<void>` | Initialize the widget |
182
+ | `show` | - | `void` | Show the chat widget |
183
+ | `hide` | - | `void` | Hide the chat widget |
184
+ | `toggle` | - | `void` | Toggle widget visibility |
185
+ | `minimize` | - | `void` | Minimize the widget |
186
+ | `maximize` | - | `void` | Maximize the widget |
187
+ | `destroy` | - | `void` | Destroy the widget instance |
188
+ | `setCustomerData` | `data: CustomerData` | `void` | Set customer information |
189
+ | `getCustomerData` | - | `CustomerData \| null` | Get current customer data |
190
+ | `setSessionVariables` | `variables: SessionVariables` | `void` | Set session variables |
191
+ | `sendMessage` | `message: string` | `void` | Send a message |
192
+ | `clearChat` | - | `void` | Clear chat history |
193
+
194
+ ### `<buni-chat-widget>`
195
+
196
+ Angular component for rendering the chat widget.
197
+
198
+ #### Inputs
199
+
200
+ | Input | Type | Default | Description |
201
+ |-------|------|---------|-------------|
202
+ | `token` | `string` | - | Your BuniAI widget token (required) |
203
+ | `config` | `object` | `{}` | Widget configuration options |
204
+ | `onReady` | `function` | - | Callback when widget is ready |
205
+ | `onNewMessage` | `function` | - | Callback for new messages |
206
+ | `onVisibilityChanged` | `function` | - | Callback for visibility changes |
207
+ | `onError` | `function` | - | Callback for errors |
208
+
209
+ ### `<buni-chat-button>`
210
+
211
+ Floating chat button component.
212
+
213
+ #### Inputs
214
+
215
+ | Input | Type | Default | Description |
216
+ |-------|------|---------|-------------|
217
+ | `openText` | `string` | `'Chat with us'` | Text when chat is closed |
218
+ | `closeText` | `string` | `'Close chat'` | Text when chat is open |
219
+ | `showText` | `boolean` | `true` | Whether to show text |
220
+
221
+ #### Outputs
222
+
223
+ | Output | Type | Description |
224
+ |--------|------|-------------|
225
+ | `clicked` | `EventEmitter<boolean>` | Emitted when button is clicked |
226
+
227
+ ### Configuration Options
228
+
229
+ ```typescript
230
+ interface BuniChatOptions {
231
+ token: string;
232
+ config?: {
233
+ position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
234
+ theme?: 'light' | 'dark' | 'auto';
235
+ primaryColor?: string;
236
+ welcomeMessage?: string;
237
+ placeholder?: string;
238
+ showBranding?: boolean;
239
+ };
240
+ onReady?: (data: any) => void;
241
+ onNewMessage?: (data: any) => void;
242
+ onVisibilityChanged?: (data: any) => void;
243
+ onError?: (error: Error) => void;
244
+ }
245
+ ```
246
+
247
+ ## 🎯 Advanced Examples
248
+
249
+ ### Reactive State Management
250
+
251
+ ```typescript
252
+ import { Component, OnInit, OnDestroy } from '@angular/core';
253
+ import { BuniChatService } from '@buni.ai/chatbot-angular';
254
+ import { Subject, takeUntil, combineLatest } from 'rxjs';
255
+
256
+ @Component({
257
+ selector: 'app-chat-manager',
258
+ template: `
259
+ <div class="chat-status">
260
+ <div *ngIf="chatState$ | async as state">
261
+ <p>Status: {{ state.isLoaded ? 'Ready' : 'Loading...' }}</p>
262
+ <p>Visibility: {{ state.isOpen ? 'Open' : 'Closed' }}</p>
263
+ <p>Unread: {{ state.unreadCount }}</p>
264
+ <p>Minimized: {{ state.isMinimized ? 'Yes' : 'No' }}</p>
265
+ </div>
266
+
267
+ <button (click)="toggleChat()" [disabled]="!(isReady$ | async)">
268
+ Toggle Chat
269
+ </button>
270
+ </div>
271
+ `
272
+ })
273
+ export class ChatManagerComponent implements OnInit, OnDestroy {
274
+ private destroy$ = new Subject<void>();
275
+
276
+ chatState$ = this.buniChatService.state$;
277
+ isReady$ = this.buniChatService.isReady$;
278
+
279
+ constructor(private buniChatService: BuniChatService) {}
280
+
281
+ async ngOnInit() {
282
+ await this.buniChatService.initialize({
283
+ token: 'your-token',
284
+ config: { position: 'bottom-right' }
285
+ });
286
+
287
+ // Listen to state changes
288
+ this.chatState$
289
+ .pipe(takeUntil(this.destroy$))
290
+ .subscribe(state => {
291
+ console.log('Chat state changed:', state);
292
+ });
293
+ }
294
+
295
+ ngOnDestroy() {
296
+ this.destroy$.next();
297
+ this.destroy$.complete();
298
+ }
299
+
300
+ toggleChat() {
301
+ this.buniChatService.toggle();
302
+ }
303
+ }
304
+ ```
305
+
306
+ ### Custom Customer Data Management
307
+
308
+ ```typescript
309
+ import { Component, OnInit } from '@angular/core';
310
+ import { FormBuilder, FormGroup } from '@angular/forms';
311
+ import { BuniChatService } from '@buni.ai/chatbot-angular';
312
+
313
+ @Component({
314
+ selector: 'app-customer-form',
315
+ template: `
316
+ <form [formGroup]="customerForm" (ngSubmit)="updateCustomer()">
317
+ <input formControlName="name" placeholder="Name" />
318
+ <input formControlName="email" placeholder="Email" />
319
+ <select formControlName="plan">
320
+ <option value="free">Free</option>
321
+ <option value="pro">Pro</option>
322
+ <option value="enterprise">Enterprise</option>
323
+ </select>
324
+ <button type="submit">Update Customer Data</button>
325
+ </form>
326
+ `
327
+ })
328
+ export class CustomerFormComponent implements OnInit {
329
+ customerForm: FormGroup;
330
+
331
+ constructor(
332
+ private fb: FormBuilder,
333
+ private buniChatService: BuniChatService
334
+ ) {
335
+ this.customerForm = this.fb.group({
336
+ name: [''],
337
+ email: [''],
338
+ plan: ['free']
339
+ });
340
+ }
341
+
342
+ async ngOnInit() {
343
+ await this.buniChatService.initialize({
344
+ token: 'your-token'
345
+ });
346
+ }
347
+
348
+ updateCustomer() {
349
+ const formValue = this.customerForm.value;
350
+
351
+ this.buniChatService.setCustomerData({
352
+ name: formValue.name,
353
+ email: formValue.email,
354
+ userId: `user_${Date.now()}`,
355
+ customFields: {
356
+ plan: formValue.plan,
357
+ registrationDate: new Date().toISOString()
358
+ }
359
+ });
360
+ }
361
+ }
362
+ ```
363
+
364
+ ### Integration with NgRx Store
365
+
366
+ ```typescript
367
+ // chat.effects.ts
368
+ import { Injectable } from '@angular/core';
369
+ import { Actions, createEffect, ofType } from '@ngrx/effects';
370
+ import { BuniChatService } from '@buni.ai/chatbot-angular';
371
+ import { switchMap, map, catchError } from 'rxjs/operators';
372
+ import { of } from 'rxjs';
373
+
374
+ @Injectable()
375
+ export class ChatEffects {
376
+ initializeChat$ = createEffect(() =>
377
+ this.actions$.pipe(
378
+ ofType(ChatActions.initializeChat),
379
+ switchMap(action =>
380
+ this.buniChatService.initialize(action.options).then(() =>
381
+ ChatActions.initializeChatSuccess()
382
+ ).catch(error =>
383
+ ChatActions.initializeChatFailure({ error })
384
+ )
385
+ )
386
+ )
387
+ );
388
+
389
+ constructor(
390
+ private actions$: Actions,
391
+ private buniChatService: BuniChatService
392
+ ) {}
393
+ }
394
+
395
+ // chat.component.ts
396
+ import { Component, OnInit } from '@angular/core';
397
+ import { Store } from '@ngrx/store';
398
+ import { BuniChatService } from '@buni.ai/chatbot-angular';
399
+ import { ChatActions } from './store/chat.actions';
400
+
401
+ @Component({
402
+ selector: 'app-chat',
403
+ template: `
404
+ <div>
405
+ <button (click)="initializeFromStore()">Initialize via Store</button>
406
+ <button (click)="toggle()">Toggle Chat</button>
407
+ </div>
408
+ `
409
+ })
410
+ export class ChatComponent {
411
+ constructor(
412
+ private store: Store,
413
+ private buniChatService: BuniChatService
414
+ ) {}
415
+
416
+ initializeFromStore() {
417
+ this.store.dispatch(ChatActions.initializeChat({
418
+ options: {
419
+ token: 'your-token',
420
+ config: { position: 'bottom-right' }
421
+ }
422
+ }));
423
+ }
424
+
425
+ toggle() {
426
+ this.buniChatService.toggle();
427
+ }
428
+ }
429
+ ```
430
+
431
+ ### Event Handling with RxJS
432
+
433
+ ```typescript
434
+ import { Component, OnInit, OnDestroy } from '@angular/core';
435
+ import { BuniChatService } from '@buni.ai/chatbot-angular';
436
+ import { Subject, takeUntil, filter, debounceTime } from 'rxjs';
437
+
438
+ @Component({
439
+ selector: 'app-chat-analytics',
440
+ template: `
441
+ <div>
442
+ <p>Messages received: {{ messageCount }}</p>
443
+ <p>Chat opened: {{ openCount }} times</p>
444
+ <p>Last activity: {{ lastActivity | date:'medium' }}</p>
445
+ </div>
446
+ `
447
+ })
448
+ export class ChatAnalyticsComponent implements OnInit, OnDestroy {
449
+ private destroy$ = new Subject<void>();
450
+
451
+ messageCount = 0;
452
+ openCount = 0;
453
+ lastActivity: Date | null = null;
454
+
455
+ constructor(private buniChatService: BuniChatService) {}
456
+
457
+ async ngOnInit() {
458
+ await this.buniChatService.initialize({
459
+ token: 'your-token',
460
+ onNewMessage: (data) => {
461
+ this.messageCount++;
462
+ this.lastActivity = new Date();
463
+ },
464
+ onVisibilityChanged: (data) => {
465
+ if (data.visibility === 'visible') {
466
+ this.openCount++;
467
+ }
468
+ this.lastActivity = new Date();
469
+ }
470
+ });
471
+
472
+ // Track state changes with RxJS
473
+ this.buniChatService.state$
474
+ .pipe(
475
+ takeUntil(this.destroy$),
476
+ filter(state => state.isLoaded),
477
+ debounceTime(1000)
478
+ )
479
+ .subscribe(state => {
480
+ console.log('Chat analytics update:', {
481
+ messageCount: this.messageCount,
482
+ openCount: this.openCount,
483
+ unreadCount: state.unreadCount
484
+ });
485
+ });
486
+ }
487
+
488
+ ngOnDestroy() {
489
+ this.destroy$.next();
490
+ this.destroy$.complete();
491
+ }
492
+ }
493
+ ```
494
+
495
+ ## 🔧 TypeScript Support
496
+
497
+ Full TypeScript support is included:
498
+
499
+ ```typescript
500
+ import {
501
+ BuniChatOptions,
502
+ CustomerData,
503
+ SessionVariables,
504
+ BuniChatState
505
+ } from '@buni.ai/chatbot-angular';
506
+
507
+ const options: BuniChatOptions = {
508
+ token: 'your-token',
509
+ config: {
510
+ position: 'bottom-right',
511
+ theme: 'light'
512
+ }
513
+ };
514
+
515
+ const customerData: CustomerData = {
516
+ name: 'John Doe',
517
+ email: 'john@example.com',
518
+ userId: '12345'
519
+ };
520
+ ```
521
+
522
+ ## 🤝 Requirements
523
+
524
+ - Angular 15+
525
+ - RxJS 7+
526
+ - Modern browser with ES2020 support
527
+
528
+ ## 📦 Module Import
529
+
530
+ ```typescript
531
+ import { NgModule } from '@angular/core';
532
+ import { BuniChatModule } from '@buni.ai/chatbot-angular';
533
+
534
+ @NgModule({
535
+ imports: [BuniChatModule],
536
+ // ...
537
+ })
538
+ export class AppModule { }
539
+ ```
540
+
541
+ ## 📝 License
542
+
543
+ MIT © BuniAI
544
+
545
+ ## 🔗 Links
546
+
547
+ - [Documentation](https://docs.buni.ai/widgets/angular)
548
+ - [GitHub Repository](https://github.com/buni-ai/chatbot-angular)
549
+ - [NPM Package](https://www.npmjs.com/package/@buni.ai/chatbot-angular)
550
+ - [BuniAI Platform](https://buni.ai)
551
+
552
+ ## 🆘 Support
553
+
554
+ - [GitHub Issues](https://github.com/buni-ai/chatbot-angular/issues)
555
+ - [Discord Community](https://discord.gg/buni-ai)
556
+ - [Documentation](https://docs.buni.ai)