0

I am building an Alexa skill and using AMAZON.DATE slot type to get a date.

My problem is that when a user says a date without a year, Alexa processes it and returns the date string with a future date.

Example - Today is 2017-09-20, User asks Alexa about date 'Sixth June', Alexa returns 2018-06-06.

I want to use the closest past date instead of closest future dates, for the case when the user doesn't specify a year in the utterance. If the user specifies a year, I don't want to change the date year.

I can't handle this on AWS Lambda using Python, as Alexa sends the complete date string, no matter if the user provides the year or not, in the JSON body.

I don't know if it is even possible to handle such user inputs with Alexa. Is there something I can do about the AMAZON.DATE slot or some other way to handle such user utterances?

Tushar Aggarwal
  • 827
  • 1
  • 10
  • 26

2 Answers2

1

You cannot do that with built-in date slot,

Utterances that map to a specific date (such as “today”, or “november twenty-fifth”) convert to a complete date: 2015-11-25. Note that this defaults to dates on or after the current date.

Source :- https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/built-in-intent-ref/slot-type-reference#date

one logic you can try it out is to do a custom logic. Take the difference between todays date and next year same date of user input. If the difference of months between those is greater than 6 months then the nearest would be future date. If difference is less that 6 months then past date. Say user gives an input July 25th (on 2017 ) and todays date is August 25th 2017. Now you can add 1 year to July 25th then you will get July 25th 2018. Difference between July 25th 2018 and August 25th 2017 is greater than 6 months so the date you want is past date, which is July 25th 2017 and vice versa. For more accuracy you can count in days instead of months

Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43
  • OK, is it a good practice to define 3 different slots, one for each portion of the date string, say `SLOT_DAY`, `SLOT_MONTH`, `SLOT_YEAR`? Or give up? – Tushar Aggarwal Sep 20 '17 at 15:48
  • I suggest using AMAZON.DATE, then check the returned date in your Lambda. If it is in the future, then subtract 1 year. – Ron Lisle Sep 21 '17 at 11:28
  • @RonLisle he needs the closest date. It can be future date or past date. Say for example If he say 25th June on year 2016 on the date 25th July 2016 then he needs past date (25th June 2016). If he tell the same on May 25th 2017 then he needs 25th June 2017 – Vijayanath Viswanathan Sep 21 '17 at 11:55
  • @VijayanathViswanathan Sorry, that's not what Tushar states above: "I want to use the closest past date instead of closest future dates". – Ron Lisle Sep 23 '17 at 12:14
  • @RonLisle appologize for the mistake. Deleting my comment – Vijayanath Viswanathan Sep 23 '17 at 13:43
0

After converting Alexa's slot input date string to a js date object, (probably with amazon-date-parser) the below could work

if (dateobj > new Date()) {
      var currentMonth = new Date().getMonth()
      var dateMonth = dateobj.getMonth()
      var offset = dateMonth > currentMonth ? 1 : 0 //  if current month is to be set with past year use >=
      dateobj.setFullYear(new Date().getFullYear() - offset);
 }
ajan
  • 398
  • 1
  • 5
  • 16