node.jsdialogflow-es-fulfillment

DialogFlow ES - Get the current time without user_input


What can I do, this code keeps showing Good Morning, but I was working on it in night. I also try what ChatGPT said used Intl.DateTimeFormat, but it's said undefined.

I want to get what is the problem of my code and the solution that I can used....

function welcome(agent) {
    const currentTime = new Date();
    const hour = currentTime.getHours(); 

    let greeting = "hello";
    if (hour >= 5 && hour < 12) {
        greeting = "Good morning.";
    } else if (hour >= 12 && hour < 18) {
        greeting = "Good afternoon.";
    } else if (hour >= 18 && hour < 22) {
    greeting = "Good evening.";
    } else {
        greeting = "Good night.";
    }
 
    agent.add(`šŸŽµ ${greeting} my friend? I'm Si Kompang`);
    agent.add("May I know what is your name?");
    }

Solution

  • function welcome(agent) {

    var date = new Date();
    var hour = date.getHours() + 8; 
    
    let greeting = "hello";
    if (hour >= 5 && hour < 12) {
        greeting = "Good morning";
    } else if (hour >= 12 && hour < 18) {
        greeting = "Good afternoon";
    } else if (hour >= 18 && hour < 22) {
    greeting = "Good evening";
    } else {
        greeting = "Good night";
    }
    
    agent.add(`${greeting}, welcome to the world of musical instruments! Iā€™m Pak Bung, the village head of kompang ethnicity. `);
    agent.add("But before we start, may I know your name?šŸ˜Š");
    }
    

    This is the solution