Back to Blog

How to create a dynamic opening based on the part of the day

A general opening is great but with Actions, you can personalize the experience even more. One of our power-users shared a piece of code that he’s using to tailor an opening message based on the part of day.

1. Create a Trigger

To get started we first need to create an intent/text trigger or event. Create a new flow and drag and drop a text trigger onto your canvas. Give a name to your intent and save your design.

dynamicopening

2. Create your action

One of Flow.ai’s key features is the possibility to use actions. Actions are pieces of code (JavaScript) that you can use for all sorts of custom building blocks. It allows you to make API-calls, incorporate advanced business logic, and to tailor bot replies.

Head over to actions and create a new action. Name your action and copy and paste the code below.

async payload => {


var date = new Date()
var time = date.getHours()


if (time >= 7 && time < 12) {
  greeting = 'Good morning'
}
if (time >= 12 && time < 18) {
  greeting = 'Good afternoon'
}
if (time >= 18 && time < 24) {
  greeting = 'Goedenavond'
}
if (time >= 24 && time < 7) {
  greeting = 'Wow some people never sleep'
}

// Create a message and quick reply
const message = new Message(`${greeting}, how can I help?`)
message.addQuickReply(new QuickReply({ label: 'Book a table', value: 'Book a table' }))

  return message  

}

3. Select your action

Within the Flow section, you can now select your action from the dropdown menu. Are you planning on incorporating your chatbot in multiple time zones? No worries the timezone of the user is stored in

payload.user.profile.timezone

With some JavaScript skills, you can incorporate the timezone in the code above and turn it into a universal solution.

Get StartedContact us