|
| 1 | +# Integration Steps for Native Web Chatbot UI in Angular |
| 2 | + |
| 3 | +## 1. Install the Library |
| 4 | + |
| 5 | +First, you need to install the native-web-chatbot-ui or any external JavaScript library in your Angular project. |
| 6 | + |
| 7 | +If the library is available via npm: |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install native-web-chatbot-ui --save |
| 11 | +``` |
| 12 | + |
| 13 | +If not available through npm, manually add it by downloading the .js file and placing it into your src/assets folder. |
| 14 | + |
| 15 | +## 2. Add External JS Library to Angular's angular.json |
| 16 | + |
| 17 | +Add the library to the angular.json file so Angular includes it in the build process: |
| 18 | + |
| 19 | +```json |
| 20 | +"scripts": [ |
| 21 | + "node_modules/native-web-chatbot-ui/dist/main.js |
| 22 | +" |
| 23 | +] |
| 24 | +``` |
| 25 | + |
| 26 | +## 3. Declare the Module and Its Types |
| 27 | + |
| 28 | +Create a declaration file to make TypeScript aware of the library: |
| 29 | + |
| 30 | +```typescript |
| 31 | +// src/app/native-web-chatbot-ui.d.ts |
| 32 | +declare module 'native-web-chatbot-ui/dist/main.js' { |
| 33 | + const content: any; |
| 34 | + export default content; |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## 4. Import and Use the Library in Angular Component |
| 39 | + |
| 40 | +```typescript |
| 41 | +import { Component, OnInit, OnDestroy, Inject, PLATFORM_ID } from '@angular/core'; |
| 42 | +import { isPlatformBrowser } from '@angular/common'; |
| 43 | + |
| 44 | +declare global { |
| 45 | + interface HTMLElement { |
| 46 | + config: any; |
| 47 | + getBotResponse: (message: string) => Promise<string>; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +@Component({ |
| 52 | + selector: 'app-chatbot', |
| 53 | + standalone: true, |
| 54 | + imports: [], |
| 55 | + templateUrl: './chatbot.component.html', |
| 56 | + styleUrls: ['./chatbot.component.css'] |
| 57 | +}) |
| 58 | +export class ChatbotComponent implements OnInit, OnDestroy { |
| 59 | + chatbotElement: HTMLElement | null = null; |
| 60 | + |
| 61 | + constructor(@Inject(PLATFORM_ID) private platformId: any) {} |
| 62 | + |
| 63 | + ngOnInit(): void { |
| 64 | + if (isPlatformBrowser(this.platformId)) { |
| 65 | + // Only run client-side code |
| 66 | + this.loadChatbot(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + ngOnDestroy(): void { |
| 71 | + // Clean up any dynamic elements if needed |
| 72 | + if (this.chatbotElement) { |
| 73 | + document.body.removeChild(this.chatbotElement); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + loadChatbot() { |
| 78 | + // Dynamically load the chatbot library in the browser |
| 79 | + import('native-web-chatbot-ui/dist/main.js').then(() => { |
| 80 | + // Dynamically create the chatbot element |
| 81 | + this.chatbotElement = document.createElement('chat-bot'); |
| 82 | + document.body.appendChild(this.chatbotElement); |
| 83 | + |
| 84 | + // Configure chatbot settings |
| 85 | + if (this.chatbotElement) { |
| 86 | + this.chatbotElement.config = { |
| 87 | + apiKey: 'sk-your-openai-key-here', |
| 88 | + theme: 'dark', |
| 89 | + css: '@org/.css', |
| 90 | + initialWidth: 400, |
| 91 | + position: 'bottom-right', |
| 92 | + bubbleStyle: { |
| 93 | + userBackground: '#4CAF50', |
| 94 | + botBackground: '#F5F5F5', |
| 95 | + borderRadius: '20px' |
| 96 | + }, |
| 97 | + streamingData: true, |
| 98 | + delayTime: 50 |
| 99 | + }; |
| 100 | + |
| 101 | + // Define the bot's response behavior |
| 102 | + this.chatbotElement.getBotResponse = async (message: string) => { |
| 103 | + const longStarWarsResponse = `A long time ago in a galaxy far, far away... The Force will be with you. Always.`; |
| 104 | + return new Promise(resolve => { |
| 105 | + setTimeout(() => { |
| 106 | + resolve(longStarWarsResponse); |
| 107 | + }, 1000); |
| 108 | + }); |
| 109 | + }; |
| 110 | + } |
| 111 | + }).catch(error => { |
| 112 | + console.error('Error loading the chatbot library:', error); |
| 113 | + }); |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +## 5. Add the Chatbot Component to Your App |
| 119 | + |
| 120 | +Add the component to your Angular app's template: |
| 121 | + |
| 122 | +```html |
| 123 | +<app-chatbot></app-chatbot> |
| 124 | +``` |
| 125 | + |
| 126 | +## 6. Style and Customize the Chatbot |
| 127 | + |
| 128 | +Customize the chatbot's appearance through its configuration: |
| 129 | + |
| 130 | +```typescript |
| 131 | +this.chatbotElement.config = { |
| 132 | + // other config properties... |
| 133 | + bubbleStyle: { |
| 134 | + userBackground: '#4CAF50', |
| 135 | + botBackground: '#F5F5F5', |
| 136 | + borderRadius: '20px' |
| 137 | + }, |
| 138 | + // more config properties... |
| 139 | +}; |
| 140 | +``` |
| 141 | + |
| 142 | +## 7. Test and Debug |
| 143 | + |
| 144 | +- Check for errors in the console when loading the script and creating the chatbot |
| 145 | +- Verify performance to ensure the script doesn't block the main thread |
| 146 | +- Test mobile responsiveness across various screen sizes |
| 147 | + |
| 148 | +## 8. Clean Up After Component Destruction |
| 149 | + |
| 150 | +Implement proper cleanup in ngOnDestroy: |
| 151 | + |
| 152 | +```typescript |
| 153 | +ngOnDestroy(): void { |
| 154 | + if (this.chatbotElement) { |
| 155 | + document.body.removeChild(this.chatbotElement); |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +## 9. Handle API Keys Securely |
| 161 | + |
| 162 | +- Don't hardcode API keys in frontend code |
| 163 | +- Use environment variables or backend services to manage sensitive data |
| 164 | + |
| 165 | +<!-- ## Summary of Integration Steps |
| 166 | +
|
| 167 | +1. **Install the Library**: Via npm or manual download |
| 168 | +2. **Configure angular.json**: Include external scripts |
| 169 | +3. **Declare Types**: Create type definitions for TypeScript |
| 170 | +4. **Create Component**: Implement dynamic loading and initialization |
| 171 | +5. **Add to Template**: Include the component in your app |
| 172 | +6. **Style**: Customize appearance through configuration |
| 173 | +7. **Cleanup**: Handle proper removal when component is destroyed |
| 174 | +8. **Test**: Ensure smooth integration and responsive behavior |
| 175 | +9. **Security**: Protect sensitive information like API keys --> |
0 commit comments