Setting Time of Day Conditions

Problem

You want users to have different experience inside and outside of working hours.

Solution

Use military time representation and simple number comparison.

Function node get current time does the time work, you can see that it has three outputs:

  • top output - is used when the current time is earlier than the work start time e.g. current time is 8, but the work starts at 8:30
  • middle output - is used when the current time is later than work end time. e.g. current time is 19:08 but the work end at 17:45
  • bottom output - is used when the current time is during work hours - between, work start and work end time.

you can use EARLY and LATE dialog nodes to have message tailored for that scenario, you can also remove those nodes and use function output ports as you wish. e.g. route user to a different path through your bot.

Example

time-of-day-switch.png

[{"id":"a799346d.3b4598","type":"change","z":"a635abea.f55998","name":"Set work hours","rules":[{"t":"set","p":"payload.user.workHoursStart","pt":"msg","to":"8:00","tot":"str"},{"t":"set","p":"payload.user.workHoursEnd","pt":"msg","to":"17:45","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":180,"wires":[["44bd1d4c.855d14"]]},{"id":"44bd1d4c.855d14","type":"function","z":"a635abea.f55998","name":"get current time","func":"const date = new Date();\n\nconst currentMil = parseInt(`${date.getHours()}${date.getMinutes()}`, 10);\n\nconst startMil = parseInt(msg.payload.user.workHoursStart.split(':').join(''), 10);\nconst endMil = parseInt(msg.payload.user.workHoursEnd.split(':').join(''), 10);\n\nmsg.payload.user.dev = {currentMil, startMil,endMil };\n\nif (currentMil < startMil) {\n    // TOO EARLY\n    msg.payload.user.outsideWorkingHours = true;\n    return [msg, null, null];\n}\n\nif (currentMil > endMil) {\n    // TOO LATE\n    msg.payload.user.outsideWorkingHours = true;\n    return [null, msg, null]\n}\n\nmsg.payload.user.outsideWorkingHours = false;\nreturn [null, null, msg]\n\n\n","outputs":3,"noerr":0,"x":580,"y":180,"wires":[["6eb656bf.68a048"],["539d9da3.0efed4"],["2bc48e34.cad8b2"]]},{"id":"6eb656bf.68a048","type":"dialogue","z":"a635abea.f55998","name":"EARLY","message":"Sorry our operators have not started work yet, we are open from ${workHoursStart} to ${workHoursEnd}","messageType":"str","displayTimer":1.5,"enableDisplay":false,"x":770,"y":120,"wires":[["2bc48e34.cad8b2"]]},{"id":"539d9da3.0efed4","type":"dialogue","z":"a635abea.f55998","name":"LATE","message":"Sorry our operators have finished their work for today. We are open from ${workHoursStart} to ${workHoursEnd}","messageType":"str","displayTimer":1.5,"enableDisplay":false,"x":770,"y":160,"wires":[["2bc48e34.cad8b2"]]},{"id":"2bc48e34.cad8b2","type":"markupInteraction","z":"a635abea.f55998","name":"Dynamic menu","markup":"<TimelineMessage>\n  <List selectable=\"true\" title=\"Select option.\" style=\"standard\" preventRetries=\"true\" interactionType=\"event\">\n    <Item id=\"1\" title=\"Option 1\" />\n    #if ( $outsideWorkingHours == false )\n      <Item id=\"2\" title=\"Option only available within working hours\" />\n    #end\n    #if ( $outsideWorkingHours == true )\n      <Item id=\"2\" title=\"Outside of working hours, request a call back next working day\" />\n    #end\n    <Item id=\"3\" title=\"Option 3\" />\n  </List>\n</TimelineMessage>\n\n  ","syntax":"markdown","outputs":5,"x":960,"y":180,"wires":[["d822c0e8.5dcf"],["d822c0e8.5dcf"],["d822c0e8.5dcf"],[],[]]},{"id":"d822c0e8.5dcf","type":"dialogue","z":"a635abea.f55998","name":"selected","message":"payload.markupInteraction","messageType":"msg","displayTimer":1.5,"enableDisplay":false,"x":1180,"y":180,"wires":[[]]}]

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

Discussion

Set your working hours

You will need to set your working hours, you must use 24 hour time format. set-working-hours.png

Conditional Markup

Within Markup and MarkupInteraction nodes we can use VTL https://velocity.apache.org/ language.
In this example we use IF CONDITION conditional-markup.png

Testing

To test the bot behaviour outside of the working hours, you don’t have to be the last one in the office xD, simply adjust times in the Set work hours node.' such that your current time falls either before or after them.