DialogFlow conditional response based on opening hours

Louis-Philippe Savard
3 min readDec 24, 2020

You want your Dialogflow chatbot to answer differently when your business is open? This pretty useful feature should be easy to implement but the documentation is not easy to find, so I created an easy tutorial.

One easy way to accomplish that would be to use integromat workflow, but in this tutorial we will avoid third party and create a Fulfillment in JavaScript in the inline editor.

In this example we have a chatbot for a telecom company that give the new Shaw Direct channel lineup. When the business is open we have a typical greeting, but if the business is closed we add a message with an image of the business hours.

First step: Create the intent

Let’s start by creating a welcome intent for the Get Started default Facebook greeting.

Now be sure to delete the default response and enable the fulfillment response.

Save your intent and go to your inline editor in the Fulfillment section of your chatbot. Now let’s add this code.

//Establish Day of Week
var date = new Date();
function getWeekDay(date) {
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
return days[date.getDay()];
}
var day = date.getDate();
var weekdate = (getWeekDay(date));
var hours = date.getHours() - 5; // set your offset from UTC Time here
//variables for business
var days = {
"Sunday": {
"openTime": 9,
"closeTime": 15
},
"Monday": {
"openTime": 7,
"closeTime": 17
},
"Tuesday": {
"openTime": 5,
"closeTime": 17
},
"Wednesday": {
"openTime": 7,
"closeTime": 17
},
"Thursday": {
"openTime": 7,
"closeTime": 21
},
"Friday": {
"openTime": 7,
"closeTime": 21
},
"Saturday": {
"openTime": 7,
"closeTime": 21
}
};
var theDay = days[weekdate];
var theTime = date.getHours();
//function statement
function openhours(agent) {
if (hours >= theDay.openTime && hours < theDay.closeTime) {
agent.add(`We are open`);
} else {
agent.add(`We are close`);
}
}

*Change your timezone (DialogFlow uses UTC) and your business hours.

Last step is to map your intent.

intentMap.set(‘Default Welcome Intent’, openhours);

You can now change your agent response however you want.

--

--

Louis-Philippe Savard

I am a Full Stack Digital Marketer passionate about growth hacking and machine learning.