Skip to content

Instantly share code, notes, and snippets.

@HugoAlvidrez
Created January 8, 2026 20:54
Show Gist options
  • Select an option

  • Save HugoAlvidrez/1a099b73b745bdd6dd3cd1e09c2c1208 to your computer and use it in GitHub Desktop.

Select an option

Save HugoAlvidrez/1a099b73b745bdd6dd3cd1e09c2c1208 to your computer and use it in GitHub Desktop.
Proyecto chatbot en Javascript
class Chatbot {
constructor() {
this.rules = {
hola: () => "Hola, pideme lo que quieras :D",
adios: () => "Bye bye, espero hablemos de nuevo pronto <3",
ayuda: () =>
"Puedo responder a saludos, despedidas y mostrar ayuda básica"
};
}
getResponse(message) {
if (!message || typeof message !== "string") {
return "Entrada inválida";
}
const normalizedMessage = message.trim().toLowerCase();
for (const keyword in this.rules) {
if (normalizedMessage.includes(keyword)) {
return this.rules[keyword]();
}
}
return this.defaultResponse();
}
defaultResponse() {
return "Lo siento, no entendí tu mensaje. Escribe 'ayuda' para ver opciones XC";
}
}
module.exports = Chatbot;
const Chatbot = require("./chatbot");
describe("Chatbot responses", () => {
let chatbot;
beforeEach(() => {
chatbot = new Chatbot();
});
test("responde a un saludo", () => {
expect(chatbot.getResponse("hola")).toBe("Hola, pideme lo que quieras :D");
});
test("responde a despedida", () => {
expect(chatbot.getResponse("adios")).toBe(
"Bye bye, espero hablemos de nuevo pronto <3"
);
});
test("responde a ayuda", () => {
expect(chatbot.getResponse("ayuda")).toContain("Puedo responder");
});
test("maneja mensajes desconocidos", () => {
expect(chatbot.getResponse("xyz")).toContain("no entendí");
});
test("maneja entradas inválidas", () => {
expect(chatbot.getResponse(null)).toBe("Entrada inválida");
});
});
const readline = require("readline");
const Chatbot = require("./chatbot");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const chatbot = new Chatbot();
console.log("Chatbot iniciado. Escribe 'adios' para salir.");
function ask() {
rl.question("Tú: ", (input) => {
const response = chatbot.getResponse(input);
console.log(`Bot: ${response}`);
if (input.toLowerCase().includes("adios")) {
rl.close();
return;
}
ask();
});
}
ask();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment