AWS Builder Center

Chat bot simples

aqui está um código de chat bot simples feito totalmente em html,css e js

<!DOCTYPE html> <html lang="pt-BR"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Chatbot</title>
\<style\>
body {
font-family: Arial, sans-serif;
background: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.chat {
width: 350px;
background: white;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 0 10px #aaa;
}
.topo {
background: #222;
color: white;
padding: 15px;
text-align: center;
}
.mensagens {
height: 400px;
padding: 10px;
overflow-y: auto;
}
.mensagem {
padding: 8px 12px;
margin: 5px 0;
border-radius: 8px;
max-width: 80%;
}
.bot {
background: #eee;
}
.usuario {
background: #222;
color: white;
margin-left: auto;
}
.entrada {
display: flex;
padding: 10px;
border-top: 1px solid #ddd;
}
input {
flex: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
margin-left: 5px;
padding: 10px 15px;
background: #222;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
\</style\>
</head>
<body>
\<div class="chat"\>
\<div class="topo"\>
Chatbot
\</div\>
\<div class="mensagens" id="mensagens"\>
\<div class="mensagem bot"\>
Olá! Como posso ajudar?
\</div\>
\</div\>
\<div class="entrada"\>
\<input type="text" id="texto" placeholder="Digite uma mensagem..."\>
\<button onclick="enviar()"\>Enviar\</button\>
\</div\>
\</div\>
\<script\>
function enviar() {
const input = document.getElementById("texto");
const mensagens = document.getElementById("mensagens");
const texto = input.value.trim();
if (texto === "") return;
// Mensagem do usuário
mensagens.innerHTML += `
\<div class="mensagem usuario"\>
${texto}
\</div\>
`;
input.value = "";
// Resposta do bot
setTimeout(() =\> {
mensagens.innerHTML += `
\<div class="mensagem bot"\>
Você disse: ${texto}
\</div\>
`;
mensagens.scrollTop = mensagens.scrollHeight;
}, 500);
mensagens.scrollTop = mensagens.scrollHeight;
}
// Enviar pressionando Enter
document.getElementById("texto").addEventListener("keydown", function(event) {
if (event.key === "Enter") {
enviar();
}
});
\</script\>
</body> </html>
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article