What is chatbot
Chatbots typically imitate or replace human user behavior. Because they are automated, they operate much faster than human users.
Where we use this
Every application we have help or support pages has some application-related content. So better we can go for chatbot for the customer related query. If we go to a chatbot, can't wait for any customer-related query for a long time, we can get the answer within no time.
Simple and easy implementation and faster. No need for any backend or API service. We can handle the frontend only. In case you need any backend services you can able to call API service as well.
Once I create the application, I have added the codes below.
app.component.css
.button {
margin-right: 20px
}
.button-active {
background-color: #9b4dca;
color: white;
}
.button-active:hover,
.button-active:focus {
background-color: #9b4dca;
border-color: #9b4dca;
color: #fff;
}
.top-bar {
margin: 25px 0;
}
app.component.html
<div class="row">
<div class="column"></div>
<div class="column column-50">
<router-outlet></router-outlet>
</div>
<div class="column"></div>
</div>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule} from './app-router.module'
import {AngularBotModule} from './angular-bot/angular-bot.module'
import { AppComponent } from './app.component';
@NgModule({
imports:[
BrowserModule,
FormsModule,
AppRoutingModule,
AngularBotModule
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
I have to add the chat service(chat.service.ts), chat(chat.component.ts, CSS and HTML) & chat modules(angular-bot.module.ts) and chat.
Just copy and paste the code below changes.
chat.service.ts
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
export class Message {
constructor(public author: string, public content: string) {}
}
@Injectable()
export class ChatService {
constructor() {}
conversation = new Subject<Message[]>();
messageMap = {
"Hi": "Hello",
"Who are you": "My name is Test Sat Bot",
"What is your role": "Just guide for the user",
"defaultmsg": "I can't understand your text. Can you please repeat"
}
getBotAnswer(msg: string) {
const userMessage = new Message('user', msg);
this.conversation.next([userMessage]);
const botMessage = new Message('bot', this.getBotMessage(msg));
setTimeout(()=>{
this.conversation.next([botMessage]);
}, 1500);
}
getBotMessage(question: string){
let answer = this.messageMap[question];
return answer || this.messageMap['defaultmsg'];
}
}
angular-bot.module.ts





abhay singhPosted Jun 15, 2023, 10:38 AM
I want to initially show some questions and user click one of them and then see answer
do doPosted May 26, 2023, 10:52 PM
Not working. shows blank page. can you post full source?
chris jenPosted Apr 27, 2022, 6:15 AM
Can you pls post an article on how to integrate python with angular, I mean ui can be in angular and the business logic could be in python...Thanks! Nice post
Dhanush KPosted Mar 24, 2022, 2:54 AM
Thank you!
Dhanush KPosted Mar 22, 2022, 3:36 PM
Can you post one article on lazy loading concept in Angular? It would be very helpful for me as I'm not clear with that concept.