Artificial intelligence (AI) is becoming an essential business requirement to power conversational and intelligent chatbot development. Angular is a scalable web framework that supports providers of chatbot development services to integrate apps with complex artificial intelligence services. Here is a comprehensive guide for developers to build intuitive and cross-device chatbots.
Let’s get started with Ai-powered chatbot development using the Angular application.
Install this library using the below-mentioned Command
npm install –save angular-ai-chat-bot
import { NgModule } from ‘@angular/core’;
import { AppComponent } from ‘./app.component’;
import { BrowserModule } from ‘@angular/platform-browser’;
import {ChatBot} from ‘angular-ai-chat-bot’;
@NgModule({
declarations: [MyComponent, ChatBot],
imports: [BrowserModule],
bootstrap: [MyComponent]
})
export class MyChatbotModule {}
1 import classes and interfaces In Component file
import { ChatBot } from ‘angular-ai-chat-bot’;
import { Subject } from ‘rxjs’;
@Component({
selector: ‘myComp’,
2 – Set [token] attribute to chatbot object in Component Files, for example-
template: `
<Chat-bot class=”chat-window” [token]=”accToken” [msg]=”shareable_message” >
<ng-template>
</ng-template>
</Chat-bot>`
})
class MyComponent {
public accToken = ‘YOUR_ACCESS_TOKEN’;
public shareable_message: Subject<any> = new Subject();
}
tsconfig.app.json
{
…
“include”: [
…
“../node_modules/angular-ai-chat-bot/*.ts”,
“../node_modules/angular-ai-chat-bot/**/*.ts”
],
}
<Chat-bot class=”chat-window” [token]=”accToken” [msg]=”msg” [msgTemplate]=’message’ [inputTemplate]=’input’ (onMsgReceive)=”onMsgReceive($event)”>
<ng-template #window>
</ng-template>
</Chat-bot>
Let’s go through every element of this structure one by one in a respective manner.
Chat-bot
Chat-bot is the selector for Chatbot which is bundled into ChatBot Application:
[token] :Required in attribute
Chat-bot has an [token] attribute which needs to connect to Google API:
[msg] :Required
Chat-bot has an [msg] attribute which should be RX Subject object
1 – Import required classes and interfaces
import { ChatBot } from ‘angular-ai-chat-bot’;
import { Subject } from ‘rxjs’;
@Component({
selector: ‘ChatBotComponent’,
template: `<app-chat-window class=”chat-window”
[token]=”accToken”
[msg]=”shareable_message”
>
<ng-template>
</ng-template>
</app-chat-window>`
})
class MyComponent {
public accToken = ‘YOUR_ACCESS_TOKEN’;
public shareable_message: Subject<any> = new Subject();
private sendMessage(msgText: string):void {
this.shareable_message.next(msgText);
}
Or you can import chat-msg component from the angular-ai-chat-bot module and use it in your angular app.
<Chat-bot class=”chat-window”
[token]=”accToken”
[msg]=”msg”
[messageTemplate]=’message’
[inputTemplate]=’input’>
<ng-template #window>
</ng-template>
</Chat-bot>
<ng-template #message let-text=”text” let-object=”object” let-sendBy=”sendBy”>
<chat-msg [msg]=”{text: text,sendBy: sendBy}” ></chat-msg>
</ng-template>
Add the ChatMsg to your application’s module declarations in the section for using chat-bot in application
import { NgModule } from ‘@angular/core’;
import { AppComponent } from ‘./app.component’;
import { BrowserModule } from ‘@angular/platform-browser’;
import {ChatBot} from ‘angular-ai-chat-bot’;
Add the following component
import { ChatMsg } from ‘angular-ai-chat-bot’;
@NgModule({
declarations: [MyComponent, ChatBot, ChatMsg],
imports: [BrowserModule],
bootstrap: [MyComponent]
})
export class MyModule {}
Chat-bot has an [inputTemplate] attribute which is a template for message input
<Chat-bot class=”chat-window”
[token]=”accToken”
[msg]=”msg”
[msgTemplate]=’message’
[inputTemplate]=’input’>
<ng-template #window>
</ng-template>
</Chat-bot>
<ng-template #input>
<input (change)=” ‘YOUR_ACCESS_TOKEN’;
public shareable_message: Subject<any> = new Subject();
public “chat-window”
[token]=”accessToken”
[msg]=”msg”
[msgTemplate]=’message’
[inputTemplate]=’input’>
<ng-template #window>
</ng-template>
</Chat-bot>
<ng-template #input>
<chat-input (change)=” ‘YOUR_ACCESS_TOKEN’;
public shareable_message: Subject<any> = new Subject();
public “chat-window” (onMsgReceive)=”MsgReceive($event)”>
<ng-template #window>
</ng-template>
</Chat-bot>
MsgReceive has just one property: received message context
With this, we have successfully integrated the chatbot in Angular 2+ Version.