Setting Day of the Week Conditions

Problem

You want users to have different experience during working days and days off.

Solution

Use JavaScript Date.getDay() function which returns numbers representing days of the week - Sunday=0, Monday=1 etc.

Function node get today's day checks if today’s day is a working day or a day off. You can see that it has two outputs:

  • top output - is used when today is a working day
  • bottom output - is used when today is a day off.

you can use OFF DAY dialog node to display a message about the day off. You can also remove this node and use function output ports as you wish. e.g. route user to a different path through your bot.

Example

day-of-the-week-switch.png

[{"id":"e73aa532.5b20b8","type":"change","z":"a635abea.f55998","name":"Set OFF days","rules":[{"t":"set","p":"payload.user.DAYS_OFF","pt":"msg","to":"saturday,sunday","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":220,"wires":[["3cb22000.41aeb"]]},{"id":"3cb22000.41aeb","type":"function","z":"a635abea.f55998","name":"get today's day","func":"const daysOff = msg.payload.user.DAYS_OFF.split(',').map(dayName => {\n  const day = dayName.toLowerCase();\n  switch (day) {\n    case 'sunday':\n      return 0;\n    case 'monday':\n      return 1;\n    case 'tuesday':\n      return 2;\n    case 'wednesday':\n      return 3;\n    case 'thursday':\n      return 4;\n    case 'friday':\n      return 5;\n    case 'saturday':\n      return 6;\n  }\n})\n\nconst day = (new Date()).getDay();\n\n// TODAY IS AN OFF DAY\nif(daysOff.includes(day)){\n    msg.payload.user.workingDay = false;\n  return [null, msg]\n} \n\n// WORKING DAY\nmsg.payload.user.workingDay = true;\nreturn [msg, null]\n\n\n","outputs":2,"noerr":0,"x":500,"y":220,"wires":[["11d5f011.8cc22"],["9c87384a.b560b8"]]},{"id":"9c87384a.b560b8","type":"dialogue","z":"a635abea.f55998","name":"OFF DAY","message":"Sorry, we are closed today, please come back on the next working day.","messageType":"str","displayTimer":1.5,"enableDisplay":false,"x":680,"y":260,"wires":[["11d5f011.8cc22"]]},{"id":"11d5f011.8cc22","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 ( $workingDay == false )\n      <Item id=\"2\" title=\"Request a call back next working day\" />\n    #end\n    #if ( $workingDay == true )\n      <Item id=\"2\" title=\"Option only available during work days\" />\n    #end\n    <Item id=\"3\" title=\"Option 3\" />\n  </List>\n</TimelineMessage>\n\n  ","syntax":"markdown","outputs":5,"x":880,"y":220,"wires":[["f19432f9.9be3e"],["f19432f9.9be3e"],["f19432f9.9be3e"],["f19432f9.9be3e"],[]]},{"id":"f19432f9.9be3e","type":"dialogue","z":"a635abea.f55998","name":"selected","message":"payload.markupInteraction","messageType":"msg","displayTimer":1.5,"enableDisplay":false,"x":1100,"y":220,"wires":[[]]},{"id":"c4d6af42.49e93","type":"comment","z":"a635abea.f55998","name":"WORKING DAY","info":"","x":700,"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 days off, comma separated e.g. . set-off-days.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 come in on the weekend, simply add today’s day to the DAYS_OFF in the Set OFF days node.