-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchatbot.component.ts
199 lines (182 loc) · 5.08 KB
/
chatbot.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { MessageService } from "./../services/message.service";
import {
Component,
OnInit,
ElementRef,
ViewChild,
Renderer2,
ViewEncapsulation,
} from "@angular/core";
import {
trigger,
state,
style,
animate,
transition,
} from "@angular/animations";
import { Router } from "@angular/router";
import { BsModalService } from "ngx-bootstrap/modal";
import { BsModalRef } from "ngx-bootstrap/modal";
@Component({
selector: "app-chatbot",
templateUrl: "./chatbot.component.html",
styleUrls: ["./chatbot.component.css"],
encapsulation: ViewEncapsulation.None,
animations: [
trigger("openClose", [
// ...
state(
"open",
style({
opacity: 1,
bottom: "60px",
right: "40px",
})
),
state(
"closed",
style({
opacity: 1,
bottom: "10px",
right: "10px",
})
),
transition("open => closed", [animate("1s")]),
transition("closed => open", [animate("1s")]),
]),
],
})
export class ChatbotComponent implements OnInit {
public showChatBox: boolean;
public showChatIcon: boolean;
public message: string;
public msgCount: number = 0;
public botResponse: any;
public initialMsgShown: boolean = false;
public showTyping: boolean = false;
isOpen = true;
public modalRef: BsModalRef;
public interval: any;
public navigateToPage: Boolean = false;
@ViewChild("displayMsg") div: ElementRef;
@ViewChild("inputMsg") input: ElementRef;
@ViewChild("confirm") confirmMessage: ElementRef;
constructor(
private renderer: Renderer2,
private msgService: MessageService,
private router: Router,
private modalService: BsModalService
) {
this.showChatIcon = true;
this.showChatBox = false;
}
ngOnInit() {
this.startInterval();
}
toggle() {
this.isOpen = !this.isOpen;
}
startInterval() {
this.interval = setInterval(() => {
this.toggle();
}, 1000);
}
stopInterval() {
clearInterval(this.interval);
this.isOpen = true;
}
openConfirmDialog() {
this.navigateToPage = !this.navigateToPage;
this.modalRef = this.modalService.show(this.confirmMessage);
}
public onConfirm(): void {
this.navigateToPage = true;
this.modalRef.hide();
this.router.navigate([]).then((result) => {
window.open(this.botResponse["url"], "_blank");
});
}
public onCancel(): void {
this.navigateToPage = false;
this.modalRef.hide();
}
ngAfterViewChecked() {
if (this.showChatBox && !this.initialMsgShown) {
this.initialMsgShown = true;
const message =
"Hiiii.. I am your Roo's Bot and here to help you..Common get started";
this.displayMessage(message, "self");
}
}
scrollToBottom(): void {
try {
this.div.nativeElement.scrollTop = this.div.nativeElement.scrollHeight;
} catch (err) {}
}
toggleChatBox() {
this.showChatBox = !this.showChatBox;
this.showChatIcon = !this.showChatIcon;
this.initialMsgShown = this.showChatBox ? false : true;
}
sendMessage(message: string) {
console.log(message);
if (message.trim() !== "") {
this.displayMessage(message, "user");
}
this.showTyping = true;
setTimeout(() => {
this.getBotResponse(message);
}, 2000);
}
speak() {
const messages = document.getElementsByClassName("cm-msg-text");
console.log(this.botResponse);
const lastMessage = this.botResponse["text"];
let msg = new SpeechSynthesisUtterance(lastMessage);
window.speechSynthesis.speak(msg);
}
displayMessage(message: string, type: string) {
this.msgCount++;
var str = ` <span class="msg-avatar">
<img src="assets/images/${type}-icon.png">
</span>
<div class="cm-msg-text">
${message}
</div>
</div>`;
const p: HTMLDivElement = this.renderer.createElement("div");
p.innerHTML = str;
p.className = "chat-msg " + type;
p.id = "cm-msg-" + this.msgCount;
this.renderer.appendChild(this.div.nativeElement, p);
// this.renderer.setStyle(p, 'style', this.style);
if (type === "user") {
this.input.nativeElement.value = "";
}
this.scrollToBottom();
}
getBotResponse(message) {
this.msgService.getBotResponse().subscribe((result) => {
this.botResponse = result.find((e) => e.user === message).bot;
const answer = this.botResponse
? this.botResponse["text"]
: "No messgage";
this.displayMessage(answer, "self");
this.showTyping = false;
if (this.botResponse["url"]) {
const link = this.botResponse["url"];
console.log("1111", link);
this.msgService.setItem("url", this.botResponse["url"]);
this.displayMessage(this.botResponse["url"], "self");
// this.router.navigate(["about"]);
this.openConfirmDialog();
if (this.navigateToPage) {
}
}
if (this.botResponse["directions"]) {
this.msgService.setItem("maps", JSON.stringify(this.botResponse));
this.router.navigate(["maps"]);
}
});
}
}