A reader recently asked:
How do we create a script that will ask for a date of birth?
My immediate thought was, “Oh, that’s easy. You just ask for the date and use the built-in system entity etc”. And quite stupidly, I also acted on that thought and sent the email out. 🙁
But that doesn’t work
It is more nuanced than that. Of course!
Turns out, because API.AI is so flexible in the way it parses dates, if the user were to respond to that question with just the month and the day API.AI automatically assumes the current year. Now, at the rate at which kids are adopting technology, it is possible that a kid born this year actually used the bot.
But let us suppose that didn’t happen. 🙂
So, this is based on a thread which does have the answer.
Except that the link in that thread isn’t valid (you really need to work on keeping your docs up to date, API.AI team).
There is just this vague mention of incomplete dates, but the link didn’t seem to be going anywhere. But I spent a little time on that page linked out and found the following section:
(Hope they don’t change that URL in their next docs update!)
Extracting the correct value
Now, when you capture the date using the @sys.date (best to use template mode for this, I think), you will use the $date.partial value as shown below.
Two types of user input
Now, the $date.partial can handle both the incomplete as well as the complete date input. When user inputs partial date, this is what the output looks like:
The year simply contains UUUU – so you know the user didn’t fill in the year and you can prompt the user to do so (from your webhook).
On the other hand, if the user did add the year, it is correctly identified:
While this approach works well for the simplest scenario shown here, it seems to be a bit flaky for the following:
- dates which are input in MM/DD format without the year added
- when there is a string appearing before the date, for e.g. “my birthday is on Feb 8th”
- here, if you added the string into the userSays, unfortunately it isn’t doing a good job of parsing incomplete dates
One workaround is to prompt the user specifically for the year of their birth first, and then ask them to enter their birthday. This way, no matter how the user inputs the information, you will be able to get the full date.