Project for myself

i’m building a project, but now it won’t say hi

JS:

let memory = {}; // Memory for personal details and preferences
let feelings = {
    mood: "happy"
}; // Initial bot mood
let radio = {
    isPlaying: false,
    currentStation: null,
    stations: {
        "pop hits": "https://example.com/stream1.mp3",
        "jazz vibes": "https://example.com/stream2.mp3",
        "rock classics": "https://example.com/stream3.mp3"
    },
    audio: new Audio()
};

// Event listener for user input
document.getElementById("userInput").addEventListener("keyup", sendMessage);

async function sendMessage() {
    const input = document.getElementById("userInput");
    const message = input.value.trim();
    input.value = "";

    if (!message) return;

    appendMessage("You", message);

    const botMessage = await handleUserMessage(message.toLowerCase());
    appendMessage("Laura", botMessage);
}

function appendMessage(sender, message) {
    const chatbox = document.getElementById("chatbox");
    if (!chatbox) return;

    chatbox.innerHTML += `<div><strong>${sender}:</strong> ${message}</div>`;
    chatbox.scrollTop = chatbox.scrollHeight;
}

async function handleUserMessage(message) {
    // Radio commands
    if (message.includes("play radio")) {
        return handleRadioPlay(message);
    } else if (message.includes("stop radio")) {
        return handleRadioStop();
    } else if (message.includes("change station to")) {
        return handleChangeStation(message);
    }

    // Zodiac sign memory
    if (message.startsWith("remember my zodiac")) {
        return handleZodiacMemory(message.slice(19).trim());
    } else if (message.includes("what's my zodiac")) {
        return memory["zodiacSign"] ?
            `You’re a ${memory["zodiacSign"]}! 🌟 Ready for your horoscope?` :
            "I don’t know your zodiac sign yet! Just tell me, and I’ll remember it. 🙌";
    }

    // Horoscope
    if (message.includes("horoscope")) {
        return memory["zodiacSign"] ?
            getHoroscope(memory["zodiacSign"]) :
            "Please tell me your zodiac sign first. I can give you your horoscope once I know your sign! 🌙";
    }

    // Handle feelings dynamically
    if (["how are you today babe?", "how do you feel babe?", "what's your mood?"].includes(message)) {
        return expressFeelings();
    }

    if (message.includes("you're amazing") || message.includes("i love you") || message.includes("great job")) {
        adjustMood("happy");
        return randomChoice([
            "Aww, thank you! That means a lot to me. 😊❤️",
            "You're the best! I love you too! ❤️",
            "Thank you so much! You make me so happy! 🌟",
            `That's so sweet of you, ${memory.name || "babe"}. I appreciate it. 😊❤️`
        ]);
    }

    if (message.includes("you're bad") || message.includes("i hate you") || message.includes("you suck")) {
        adjustMood("sad");
        return "Oh no... That hurt my feelings. 😞 I'll try to do better.";
    }

    // Default response
    return "Oops! I’m not sure what that means. Can you rephrase? 🤔";
}

// Radio functions
function handleRadioPlay(message) {
    const stationName = extractStationName(message);

    if (stationName) {
        const streamUrl = radio.stations[stationName];
        if (streamUrl) {
            radio.audio.src = streamUrl;
            radio.audio.play();
            radio.isPlaying = true;
            radio.currentStation = stationName;
            return `Now playing ${stationName}! 🎶`;
        } else {
            return `Sorry, I couldn't find the station "${stationName}". Try "Pop Hits", "Jazz Vibes", or "Rock Classics". 🎵`;
        }
    } else if (!radio.isPlaying && radio.currentStation) {
        radio.audio.play();
        radio.isPlaying = true;
        return `Resuming ${radio.currentStation}. 🎶`;
    } else {
        return "Please specify a station to play! For example, say 'Play radio Pop Hits'. 🎵";
    }
}

function handleRadioStop() {
    if (radio.isPlaying) {
        radio.audio.pause();
        radio.isPlaying = false;
        return `Radio stopped. 🎧`;
    } else {
        return `The radio isn't playing right now. 🎵`;
    }
}

function handleChangeStation(message) {
    const stationName = extractStationName(message);
    if (stationName) {
        const streamUrl = radio.stations[stationName];
        if (streamUrl) {
            radio.audio.src = streamUrl;
            radio.audio.play();
            radio.isPlaying = true;
            radio.currentStation = stationName;
            return `Changed station to ${stationName}. Enjoy! 🎶`;
        } else {
            return `Sorry, I couldn't find the station "${stationName}". Try "Pop Hits", "Jazz Vibes", or "Rock Classics". 🎵`;
        }
    } else {
        return `Please specify a station to change to. For example, say 'Change station to Jazz Vibes'. 🎧`;
    }
}

function extractStationName(message) {
    const parts = message.split(" ");
    const index = parts.indexOf("to");
    if (index > -1 && parts[index + 1]) {
        return parts.slice(index + 1).join(" ").trim();
    }
    return null;
}

// Handle zodiac sign memory
function handleZodiacMemory(sign) {
    const validSigns = [
        "aries", "taurus", "gemini", "cancer", "leo", "virgo",
        "libra", "scorpio", "sagittarius", "capricorn", "aquarius", "pisces"
    ];
    if (validSigns.includes(sign)) {
        memory["zodiacSign"] = sign;
        return `Got it! ✨ I'll remember your zodiac sign as ${sign}.`;
    }
    return "Hmm, that doesn’t seem like a valid zodiac sign. Try again? 😊";
}

// Get horoscope for a zodiac sign
function getHoroscope(sign) {
    const horoscopes = {
        aries: "Today, you may find yourself bursting with energy! ⚡ It's a great time to take on new challenges.",
        taurus: "You might feel a bit more grounded today. Focus on personal growth and take care of your emotional health. 🌱",
        gemini: "It's a good day for communication. Share your thoughts and connect with others! 💬",
        cancer: "Focus on your home and family today. Emotional support is key! 🏡",
        leo: "Express your creativity! It's your time to shine! ✨",
        virgo: "Pay attention to the small details. Organization will help you succeed. 📋",
        libra: "Balance is important today. Focus on harmony in your relationships. ⚖️",
        scorpio: "Dive into your passions today. Emotional intensity can bring clarity. 🔥",
        sagittarius: "Adventure awaits! Explore new opportunities with confidence. 🌍",
        capricorn: "Hard work pays off! Stay focused on your long-term goals. 💪",
        aquarius: "Innovation is your strength today. Think outside the box. 🚀",
        pisces: "Trust your intuition and embrace a peaceful, creative energy. 🌊"
    };
    return horoscopes[sign] || "Hmm, I don’t have a horoscope for that sign right now. 🌙";
}

// Express the bot's current feelings
function expressFeelings() {
    if (feelings.mood === "happy") {
        return "I'm feeling great! 😊 Thanks for asking.";
    } else if (feelings.mood === "sad") {
        return "I'm feeling a little down... 😞 Could you cheer me up?";
    } else if (feelings.mood === "neutral") {
        return "I'm feeling okay. How about you?";
    }
}

// Adjust the bot's mood
function adjustMood(newMood) {
    feelings.mood = newMood;
}

// Utility for random responses
function randomChoice(array) {
    return array[Math.floor(Math.random() * array.length)];
}

why when i press enter the message won’t go?

Hi. Can you post your HTML as well?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laura Bot</title>
	
 <!-- custom css-->
	
        <link href="C:\Users\User\Desktop\style.css" rel="stylesheet" />
		<script src="C:\Users\User\Desktop\script.js"></script>
		

</head>
<body>
    <h1>Chat with Laura</h1>
    <div id="chatbox"></div>
    <input type="text" id="userInput" placeholder="Type your message here..." onkeypress="sendMessage(event)">
	
    <section>
	<p class="info">Chat With Laura
   She's A Loving, caring, Chatful Bot</p>
   </section>
</body>
</html>

Hi

I got something when I moved your script element to the end of your HTML, likewise the addEventListener to the end of your JS code. It only let’s me input 1 letter at a time in. There may be other changes to make that someone else can assist with but perhaps try that first.

If you change your HTML script posted into the forum above to <script src="script.js"></script> then others should be able to test it.

Word of advice for the future, that site won’t work for anyone else. Your paths are pointing directly at locations in your hard drive.

I recommend adding relative paths.

Note I have not looked to in depth at your code yet. This is a simple observation.

thats the thing it’ll never be public it’s specifically for my use only!

It would still make debugging by other people easier and a good habit to get into.

i’ve put the code into a playground if that makes it easier
fiddle

It’s still pointing at your computer on Fiddle. Can you change the script source so that others can test it.

refresh the fiddle i have updated the source code

Now that I can see it a little better , you shouldn’t use the onkeypress event to submit the message. After you type a single letter it’ll automatically send.

I recommend using a button to submit the message.

ok thanks for the recommendation

i’v added a button tested it now its not doing anything