Explorer
project
images
env.js
fetchAI.js
index.css
index.html
index.js
Dependencies
openai@3.2.1
const chatbotConversation = document.getElementById('chatbot-conversation')
let conversationStr = ''
document.addEventListener('submit', (e) => {
e.preventDefault()
const userInput = document.getElementById('user-input')
conversationStr += ` ${userInput.value} ->`
fetchReply()
const newSpeechBubble = document.createElement('div')
newSpeechBubble.classList.add('speech', 'speech-human')
chatbotConversation.appendChild(newSpeechBubble)
newSpeechBubble.textContent = userInput.value
userInput.value = ''
chatbotConversation.scrollTop = chatbotConversation.scrollHeight
})
async function fetchReply(){
const url = 'https://deft-smakager-03b2a5.netlify.app/.netlify/functions/fetchAI'
const response = await fetch(url, {
method: 'POST',
headers: {
'content-type': 'text/plain',
},
body: conversationStr
})
const data = await response.json()
/*
Challenge:
1. Update the two commented lines of code to get this working.
2. Push to GitHub to redeploy, then test.
*/
// conversationStr+=` ${response.data.choices[0].text} ->`
// renderTypewriterText(response.data.choices[0].text)
console.log(data)
}
function renderTypewriterText(text) {
const newSpeechBubble = document.createElement('div')
newSpeechBubble.classList.add('speech', 'speech-ai', 'blinking-cursor')
chatbotConversation.appendChild(newSpeechBubble)
let i = 0
const interval = setInterval(() => {
newSpeechBubble.textContent += text.slice(i-1, i)
if (text.length === i) {
clearInterval(interval)
newSpeechBubble.classList.remove('blinking-cursor')
}
i++
chatbotConversation.scrollTop = chatbotConversation.scrollHeight
}, 50)
}