0

I am using the inline editor for Dialogflow to manipulate user data and query my database in.js, with that, I also want the chatbot to answer questions like:

"When is my next Art test."

As the admin I know Art tests are every 3 weeks on Wednesday throughout the year, but how do I translate that into a chatbot response, something like;

"Your next art test is Wednesday 8th August", Or even, "Your next test is Today!"

I have looked at this which is different ways to initiate js times, but none seem to fit what I'm looking for.

I have created a function for time as below, which is where I assume I'd have to make the calculation:

  function getDate(agent){
      var today = new Date();
  }

And the response would be something like:

function getTestDate(agent){
        agent.add('Your next test date is' + today); #etc   
}

Lastly I have seen this other post on stack calculating days between two dates, whilst this could work I'm not too sure how I can implement this into a Dialogflow environment.

I don't think moment.js can work within the inline editor, but if it did, It looks like it would be easy, per the example here:

moment([2007, 0, 29]).fromNow();     // 4 years ago
moment([2007, 0, 29]).fromNow(true); // 4 years

Then I could simply do the fromNow() command in each function for different dates, and idea how something like this could be interpreted to normal js?

UPDATE

Not got anywhere with this yet but had another idea, is there anyway to identify the month and from there trigger a response? For example can I create a function that identifies the date right now, then i'll be able to write the 3/4 test dates as a text response:

function (getTestDate(agent){
        if(date) == May
} agent.add('Tests for May are....etc')
Tipping44
  • 281
  • 4
  • 16

1 Answers1

0

Okay, not exactly what I wanted but the closest I can get to what I want is declaring a test date, then creating if statements to suggest IF today is before a certain date, then i know to respond with the next closest date, and so on.

function getTestDate(agent){
        var today = new Date();
        var testDate= new Date("08/08/2018");
        var timeDiff = Math.abs(testDate.getTime() - today.getTime());
        var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));

        if (today < testDate){
            agent.add('Your next test date is in ' + diffDays + 'days, at 9pm on Wednesday');
        }else if (today > testDate) {
            agent.add('Your test date was on Wednesday 8th August 2018');
        }else if (today == testDate){
            agent.add('Your test is today, Wednesday 8th August 2018');
        }

Again, not ideal as I know have to create a variety of functions for different times, but get's the job done. If you have any other suggestions please feel free to suggest.

Tipping44
  • 281
  • 4
  • 16