Code-based Actions: Create a customized opening message

With the customizable messages, you can personalize the user experience and tailor opening messages. A generic opening is good however, with Actions, you can personalize the user experience further.

To create customized messages:

  1. Create a Trigger
    We need to create an intent or 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.

  1. Create your Action
    Actions are pieces of JavaScript code that you can use for custom building blocks. It allows you to make API calls, incorporate advanced business logic, and tailor bot replies.

In Actions 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 = 'Good evening'  
}  
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  

}
  1. Select your action
    Within the Flow section, you can select your Action from the dropdown menu.

If you are planning on incorporating your bot in multiple time zones, you can use the timezone of the user stored in.

payload.user.profile.timezone

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