1
votes


I'm doing a service chat and I'd like to extract days that correspond to Sunday. For exemple:

Watson: Choose a date
user: day 30
Watson: We don't open this day, because it's a Sunday.

The problem is.. the Sundays of this month are not the same as next month.. I tried with an array inside the context with Sundays days of this month(ex: "2","9","16","23","30"), but the Conversation didn't understand that day 30 is a Sunday.. Could anyone help me please?

Tks! :)

1

1 Answers

1
votes

In this case, unfortunally, @sys-date does not work equal @sys-number. In this case with @sys-number, if user type 1, @sys-number recognize and can use @sys-number:1 with condition inside the flow.

Unfortunally, @sys-date doesn't.

In this case, for get the date with javascript you can use:

new Date() //get the date NOW
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

You can see the now() is the same format to get date: new Date(year, month, day, hours, minutes, seconds, milliseconds) And you can convert the @sys-date to the same format, and use this to verify the name of the date.

Get the day with context variable and use code in your application. For example:

  var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  var dateObject = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
  var dayName = days[dateObject.getDay()];
  console.log(dayName);

And make some condition... for example:

 if(dayName === 'Sunday'){
    data.output.text[0] = "We don't open this day, because it's a Sunday."    
}

I code this and works fine, see:

function dayRequest(data, req, res){
  console.log('works the true condition context')

  var dateString = data.context.day; //context variable with <? @sys-date ?>
  var dateParts = dateString.split("/");
  var dateObject = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);

  var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

  var dateObject = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
  var dayName = days[dateObject.getDay()];
  console.log(dayName);

  if(dayName === 'Sunday'){
    data.output.text[0] = "We don't open this day, because it's a Sunday."; 
    return res.json(data);
  }
}

Check my app:

enter image description here

In this case, you can use now to get the date and in your application you'll end a message if the day is Sunday... You can use context variable for this too, but, you need set all dates.. And with this code, you'll check the date and your application will send a message.

Summary: convert your @sys-date to the same format now() and use my code to send a message for your user if the day is Sunday. I created one context variable with to save the date after request and in the next flow I create one context with dayTrue: true inside conversation flow. In my application, I created one condition in my updateMessage() if data.context.date === true, my function dayRequest() will execute.