-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
106 lines (85 loc) · 3.18 KB
/
main.js
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
const chatBox = document.getElementById('chat-box');
const userInput = document.getElementById('user-input');
const sendBtn = document.getElementById('send-btn');
const GEMINI_API_KEY = 'AIzaSyC0Cjd5U_kIM9tvqxfjjvQ_MlhabjtxA30';
function addMessage(role, text) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message', role);
const logo = document.createElement('img');
logo.src = role === 'user' ? './img/user.png' : './img/ai.png';
logo.alt = role === 'user' ? 'User Logo' : 'Tarz Bots Logo';
const contentDiv = document.createElement('div');
contentDiv.classList.add('message-content');
contentDiv.innerHTML = role === 'ai' ? marked.parse(text) : text;
const actionButtons = document.createElement('div');
actionButtons.classList.add('action-buttons');
if (role === 'ai') {
const copyButton = document.createElement('button');
copyButton.innerText = 'Salin';
copyButton.onclick = () => copyText(text);
actionButtons.appendChild(copyButton);
const likeButton = document.createElement('button');
likeButton.innerText = '👍';
likeButton.onclick = () => alert('Anda menyukai respons ini!');
actionButtons.appendChild(likeButton);
const dislikeButton = document.createElement('button');
dislikeButton.innerText = '👎';
dislikeButton.onclick = () => alert('Anda tidak menyukai respons ini!');
actionButtons.appendChild(dislikeButton);
}
messageDiv.appendChild(logo);
messageDiv.appendChild(contentDiv);
messageDiv.appendChild(actionButtons);
chatBox.appendChild(messageDiv);
chatBox.scrollTop = chatBox.scrollHeight;
}
function copyText(text) {
navigator.clipboard.writeText(text).then(() => {
alert('Teks berhasil disalin!');
}).catch(err => {
console.error('Gagal menyalin teks: ', err);
});
}
async function sendMessageToGemini(message) {
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${GEMINI_API_KEY}`;
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
contents: [{
parts: [{ text: message }]
}]
})
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
if (!data.candidates || !data.candidates[0] || !data.candidates[0].content.parts[0].text) {
throw new Error('Respons dari API tidak valid.');
}
return data.candidates[0].content.parts[0].text;
} catch (error) {
console.error('Error:', error);
throw error;
}
}
sendBtn.addEventListener('click', async () => {
const userMessage = userInput.value.trim();
if (!userMessage) {
alert('Silakan ketik pesan Anda terlebih dahulu.');
return;
}
addMessage('user', userMessage);
userInput.value = '';
try {
const aiResponse = await sendMessageToGemini(userMessage);
addMessage('ai', aiResponse);
} catch (error) {
addMessage('ai', '⚠️ Maaf, terjadi kesalahan. Silakan coba lagi.');
}
});
addMessage('ai', '👋 Halo! Saya Tarz Bots, asisten AI Anda. Silakan ketik pesan Anda di sini.');