Free Form Input on Dates

Problem

You want users to input any date format e.g. 1 April 2019, 4/01/2019, 1 4 2019

Solution

Use DialogFlow Intent Detection node with slot detection, format dates in a function node.

Example

freeform-date.pngz

[{"id":"5bf8d403.82214c","type":"dialogue","z":"a635abea.f55998","name":"Can I have some date please?","message":"Can I have some date please?","messageType":"str","displayTimer":1.5,"enableDisplay":false,"x":250,"y":620,"wires":[["4b81a25f.ed9e5c"]]},{"id":"4b81a25f.ed9e5c","type":"input","z":"a635abea.f55998","name":"","x":450,"y":620,"wires":[["6bc794a6.070c0c"]]},{"id":"6bc794a6.070c0c","type":"intentDetection","z":"a635abea.f55998","name":"","secretName":"srn:vault::sbbotsofdublin:secretdoc:dateformat","projectId":"bb-vmyyjo","nlp":"DialogFlow","msgContext":false,"context":[],"contextMsg":"","x":620,"y":620,"wires":[["65a8f210.0fc17c"],[],["73795ccc.cdbec4"]]},{"id":"73795ccc.cdbec4","type":"dialogue","z":"a635abea.f55998","name":"Does not look a like a date.","message":"Does not look a like a date.","messageType":"str","displayTimer":"","enableDisplay":false,"x":640,"y":740,"wires":[["5bf8d403.82214c"]]},{"id":"276e972d.083a78","type":"function","z":"a635abea.f55998","name":"formatDate","func":"\nconst myDate = new Date(msg.payload.result.slots.date.stringValue);\n\nlet month = '' + (myDate.getMonth() + 1);\nlet day = '' + myDate.getDate();\nlet year = myDate.getFullYear();\n\nif (month.length < 2) {\n    month = '0' + month;\n}\n\nif (day.length < 2) {\n    day = '0' + day;\n}\n\nmsg.payload.dev = myDate;\nmsg.payload.dateUS = `${month}/${day}/${year}`\nmsg.payload.dateUK = `${day}/${month}/${year}`\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":1090,"y":620,"wires":[["b4b67823.17c818"]]},{"id":"b4b67823.17c818","type":"change","z":"a635abea.f55998","name":"dateUS, dateUK","rules":[{"t":"set","p":"payload.user.dateUS","pt":"msg","to":"payload.dateUS","tot":"msg"},{"t":"set","p":"payload.user.dateUK","pt":"msg","to":"payload.dateUK","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1280,"y":620,"wires":[[]]},{"id":"65a8f210.0fc17c","type":"switch","z":"a635abea.f55998","name":"check for date slot","property":"msg.payload.result.slots.date","propertyType":"msg","rules":[{"t":"nnull"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":850,"y":620,"wires":[["276e972d.083a78"],["73795ccc.cdbec4"]]}]

To try it out simply copy the above json and import it in the flow designer.
Key shortcut Ctrl/⌘-i, Menu option Import

Discussion

Prerequisites

You will need to create a Dialogflow agent and a secret for this agent. The agent needs to be built with a date slot. Dialogflow slot filling.

Agent and Intent configuration

df-date.png

Dialogflow V2 Secret

To set up a Secret for Dialogflow, please follow the instructions from the Dialogflow V2 Secret section.