Building Chatbots with Artificial Intelligence and Angular

Bhuvan Chawla | 22nd January 2020

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. 

AI-powered Chatbots Using Angular

 

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

 

Follow Some Steps to get AI ChatBot in Angular 2+ Version

 

  1. Add Chatbot module to the main module or anywhere you want to use this library. We suggest adding it to another module, so you can import the entire setup later. Here are the commands-

 

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();

 

}

 

  1. You need to add the module into typescript compilation configs

tsconfig.app.json

{

    …

  “include”: [

   …

  “../node_modules/angular-ai-chat-bot/*.ts”,

   “../node_modules/angular-ai-chat-bot/**/*.ts”

  ],

}

 

Here is the fully stuffed chatbot tag that you can use in your templates by following these steps:

   

<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>

 

  &lt;input (change)=” ‘YOUR_ACCESS_TOKEN’;

 

  public shareable_message: Subject&lt;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.

About Author

Bhuvan Chawla

Bhuvan Chawla is working as Front-End developer, having good knowledge of Angular 7, Typescript, HTML5, CSS3, Bootstrap.JavaScript

No Comments Yet.


Leave a Comment

Name is required

Comment is required




Request For Proposal

Sending message..

Ready to innovate ? Let's get in touch

Chat With Us