forked from apache/hertzbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.component.ts
46 lines (39 loc) · 1.22 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
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-chatbot',
templateUrl: './chatbot.component.html',
styleUrls: ['./chatbot.component.css'],
})
export class ChatbotComponent {
isPopupVisible = false;
userInput = '';
messages: { sender: string; text: string }[] = [];
constructor(private http: HttpClient) { }
togglePopup() {
this.isPopupVisible = !this.isPopupVisible;
}
sendMessage() {
if (this.userInput.trim()) {
// Add user's message to the chat window
this.messages.push({ sender: 'You', text: this.userInput });
// Send the message to the AI backend
this.http.post('/api/ai', { text: this.userInput }).subscribe(
(response: any) => {
// Add AI's response to the chat window
this.messages.push({ sender: 'AI', text: "sample answer" });
},
(error) => {
// Handle error response
this.messages.push({ sender: 'AI', text: 'Error communicating with AI' });
console.error('Error communicating with AI:', error);
}
);
// Clear the input field
this.userInput = '';
}
}
resetChat() {
this.messages = [];
}
}