How to receive date and time in a script
$DATETIME
is a system entity that receives and records information about date and time.
It relies on the Duckling library and processes text input in a variety of formats.
- For the date, it can be 15 December, Dec the 15th, 15/12, etc.
- For the time, it can be 12:45, 12.45, quarter to one, etc.
Besides absolute time references (e. g., June 20 or 23:00), $DATETIME
accepts relative ones, like today, tomorrow, or in two hours.
$DATETIME contents
$DATETIME
value contains an object with the following properties (for the input 20 December 2019 12:45):
{
"iso": "2019-12-20T00:45:00", // Date and time according to ISO 8601 standard
"year": 2019,
"month": 12,
"day": 20,
"dayOfWeek": 6, // 1 is Sunday, 2 is Monday, 7 is Saturday
"hour": 0,
"minute": 45,
"second": 0,
"timestamp": 1576802700000 // Unix timestamp, in milliseconds
}
If no date is specified in the input, $DATETIME
assumes the today’s date. If no time is specified, $DATETIME
assumes the time to be midnight.
How to add $DATETIME to a script
Use a built-in intent to add a $DATETIME
entity to your script.
The Date&Time intent accepts inputs like 15/05, 20 December, July 12, 12:00, and so on. To recognize the date even among other words, you can use template syntax:
In this case, the intent will recognize not just December 15, but also Okay, it will be December 15 then.
To test how $DATETIME
works, you can create a script that looks like this:
In this case, if the input text is not processed by $DATETIME
, the bot will reply Sorry, I can’t understand you. Otherwise, the bot will print out the $DATETIME
contents, then return to the input screen for you to test another input option.
Select Test to open the test widget and see how the bot processes different date and time inputs.
How to use time in the script
-
Connect the Date&Time intent to a new screen with a Conditions block and add the condition:
$time = $DATETIME.hour + ":" + ("0" + $DATETIME.minute).slice(-2)
-
Make a connection to a Text block and write the text:
Do you want to have your appointment at $time?
Regardless of the way the client writes the time, either in words or in numbers, the bot will save the time according to the format specified in the second Conditions block.
In this script, if the client writes quarter past noon, the bot will save 12:45 into the $time
variable.
How to use date in the script
-
Connect the Date&Time intent to a new screen with a Conditions block and add the condition:
$date = $DATETIME.month + "/" + $DATETIME.day + "/" + $DATETIME.year
-
Make a connection to a Text block and add the text:
Do you want to book a room on $date?
Regardless of the way the client writes the date, either in words or in numbers, the bot will save the date according to the format specified in the second Conditions block.
In this script, if the client writes 20 October, the bot will save 10/20/2022 into the $date
variable, where instead of 2022 there will be the current year.
What’s next?
Now you can upgrade your script, for example:
- Check if the chosen date is available for booking and let your clients book a date.
- Process other information, e. g. the client name, phone number, or email.
Or send the received data:
- to Google Sheets
- as a Telegram direct message
- as a Telegram message to a group chat
- as a direct message in VK
- as an email