angular-ai-chat-bot
Version:
Angular 6 AI Chat Bot module with Google Api
59 lines (49 loc) • 1.9 kB
text/typescript
import {
Component, ContentChild, ElementRef, EventEmitter, Input, OnInit, Output, TemplateRef,
ViewChild
} from '@angular/core';
import { Observable, Subject } from 'rxjs';
import {DataService, ESendBy, Message} from '../services/data.service';
import {scan} from 'rxjs/internal/operators';
export class ChatWindowComponent implements OnInit {
template: TemplateRef<any>;
msgTemplate: TemplateRef<any>;
inputTemplate: TemplateRef<any>;
msg: Subject<any>;
token: string;
onMsgReceive = new EventEmitter();
msgArea: ElementRef;
defaultMsgTemplate: TemplateRef<any>;
defaultInputTemplate: TemplateRef<any>;
allMessages: Observable<Message[]>;
constructor(public dataService: DataService) { }
ngOnInit() {
this.msgTemplate = this.msgTemplate ? this.msgTemplate : this.defaultMsgTemplate;
this.inputTemplate = this.inputTemplate ? this.inputTemplate : this.defaultInputTemplate;
this.dataService.init(this.token);
this.allMessages = this.dataService.conversation.asObservable()
.pipe(
scan((acc, val) => {
setTimeout(() => {
this.msgArea.nativeElement.scrollTop = this.msgArea.nativeElement.scrollHeight;
});
if (ESendBy.bot === val[0].sendBy) {
this.onMsgReceive.emit(val[0].content);
}
return acc.concat(val);
} )
)
this.msg.subscribe((msg) => {
this.dataService.converse(msg);
})
}
public onChange(target: any) {
this.msg.next(target.value);
target.value = '';
}
}