cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Code-based Actions: Access the last flow or event

Code-based Actions: Access the last flow or event

This guide will explain how to add a code action for storing the name of the last flow or event so you can route back to it after losing the context.

A good way to create chatbots in a more efficient way is to make use of reusable flows. These flows are generic and can be re-used in multiple situations. Some examples are a feedback (“Did that help?”) flow and a “Do you still need help?” flow.

When one of these reusable flows gets triggered, a user leaves the current context.

In most situations, this doesn’t cause any problems. When a Feedback flow gets triggered, the user probably doesn't need to return to the previous flow. It’s a linear flow where the user got an answer to their question, and afterwards entered the Feedback flow.

However, in situations like the “Do you still need help?” flow, things can get more complicated.

When the user responds with “Yes”, you could trigger a handover, but what if you want to route the user back to their original context to make them complete their flow first?

An example would be when the user has to select an option from a menu. You can add a Timer Trigger to detect if the user doesn’t respond for a specific time period. If so, you can let that timer trigger the “Do you still need help?” flow.

When the user responds to that message with “Yes”, it would be better for the customer experience if the user is able to continue from where they left, instead of letting them select how they would like to continue their conversation with the bot.

Routing to flows vs events

You can route the user back to their original context by triggering the last visited flow or event. It depends on the use case which method would be best.

If you have a flow containing multiple events, routing to the flow would always route the user back to the first step (event) of that flow, regardless of how much of that flow the user previously went through already.

By routing to the last event, you can make sure the user skips the steps he or she has already gone through, which would be best in most situations.

However, there are also some use cases where routing to the last flow could have some benefits as well. For example when using periodic broadcasts over longer time periods.

Store the last flow name

For every flow, you want to be able to go back to, add the following code to save the flow name.

async payload => {

  //console.log(payload)
  console.log(payload.match.flow.title)
  
  return {
    params: {
      // Using a spread operator, we add all provided params in the result
      ...payload.params,
      // Now we override the lastflow param
      lastflow: [{
        value: payload.match.flow.title,
        match: payload.match.flow.title
      }]
    }
  }
}

It’s also important to let the flow start with an event that has exactly the same name as the flow.

Route back to the last flow

Now you have stored the name of the last flow into a parameter, you can use another code action to dynamically route to an event with the name of the value of that parameter:

async payload => {
  
  if  (Array.isArray(payload.params.lastflow))
  {
    let lastflowname = payload.params.lastflow[0].value
  
    trigger(payload.params.lastflow[payload.params.lastflow.length - 1].value)
  }
  else
  {
    console.error("Lastflow was undefined.")
  }

}


Store the last event name

You can do the same for the last event name. Simply replace payload.match.flow.title with payload.match.step.title and change lastflow to lastevent.

async payload => {

  //console.log(payload)
  console.log(payload.match.step.title)
  
  return {
    params: {
      // Using a spread operator, we add all provided params in the result
      ...payload.params,
      // Now we override the lastevent param
      lastevent: [{
        value: payload.match.step.title,
        match: payload.match.step.title
      }]
    }
  }
}

Don’t forget to add this code action underneath all events you want to be able to route back to.

Route back to the last event

Then the code action for routing back to the last event will look like this.

async payload => {
  
  if  (Array.isArray(payload.params.lastevent))
  {
    let lasteventname = payload.params.lastevent[0].value
  
    trigger(payload.params.lastevent[payload.params.lastevent.length - 1].value)
  }
  else
  {
    console.error("Lastevent was undefined.")
  }

}
Labels (1)

Code-based Actions: Access the last flow or event

This guide will explain how to add a code action for storing the name of the last flow or event so you can route back to it after losing the context.

A good way to create chatbots in a more efficient way is to make use of reusable flows. These flows are generic and can be re-used in multiple situations. Some examples are a feedback (“Did that help?”) flow and a “Do you still need help?” flow.

When one of these reusable flows gets triggered, a user leaves the current context.

In most situations, this doesn’t cause any problems. When a Feedback flow gets triggered, the user probably doesn't need to return to the previous flow. It’s a linear flow where the user got an answer to their question, and afterwards entered the Feedback flow.

However, in situations like the “Do you still need help?” flow, things can get more complicated.

When the user responds with “Yes”, you could trigger a handover, but what if you want to route the user back to their original context to make them complete their flow first?

An example would be when the user has to select an option from a menu. You can add a Timer Trigger to detect if the user doesn’t respond for a specific time period. If so, you can let that timer trigger the “Do you still need help?” flow.

When the user responds to that message with “Yes”, it would be better for the customer experience if the user is able to continue from where they left, instead of letting them select how they would like to continue their conversation with the bot.

Routing to flows vs events

You can route the user back to their original context by triggering the last visited flow or event. It depends on the use case which method would be best.

If you have a flow containing multiple events, routing to the flow would always route the user back to the first step (event) of that flow, regardless of how much of that flow the user previously went through already.

By routing to the last event, you can make sure the user skips the steps he or she has already gone through, which would be best in most situations.

However, there are also some use cases where routing to the last flow could have some benefits as well. For example when using periodic broadcasts over longer time periods.

Store the last flow name

For every flow, you want to be able to go back to, add the following code to save the flow name.

async payload => {

  //console.log(payload)
  console.log(payload.match.flow.title)
  
  return {
    params: {
      // Using a spread operator, we add all provided params in the result
      ...payload.params,
      // Now we override the lastflow param
      lastflow: [{
        value: payload.match.flow.title,
        match: payload.match.flow.title
      }]
    }
  }
}

It’s also important to let the flow start with an event that has exactly the same name as the flow.

Route back to the last flow

Now you have stored the name of the last flow into a parameter, you can use another code action to dynamically route to an event with the name of the value of that parameter:

async payload => {
  
  if  (Array.isArray(payload.params.lastflow))
  {
    let lastflowname = payload.params.lastflow[0].value
  
    trigger(payload.params.lastflow[payload.params.lastflow.length - 1].value)
  }
  else
  {
    console.error("Lastflow was undefined.")
  }

}


Store the last event name

You can do the same for the last event name. Simply replace payload.match.flow.title with payload.match.step.title and change lastflow to lastevent.

async payload => {

  //console.log(payload)
  console.log(payload.match.step.title)
  
  return {
    params: {
      // Using a spread operator, we add all provided params in the result
      ...payload.params,
      // Now we override the lastevent param
      lastevent: [{
        value: payload.match.step.title,
        match: payload.match.step.title
      }]
    }
  }
}

Don’t forget to add this code action underneath all events you want to be able to route back to.

Route back to the last event

Then the code action for routing back to the last event will look like this.

async payload => {
  
  if  (Array.isArray(payload.params.lastevent))
  {
    let lasteventname = payload.params.lastevent[0].value
  
    trigger(payload.params.lastevent[payload.params.lastevent.length - 1].value)
  }
  else
  {
    console.error("Lastevent was undefined.")
  }

}
Labels (1)
Last Reviewed:
02-11-2022 03:04 AM

Code-based Actions: Access the last flow or event

This guide will explain how to add a code action for storing the name of the last flow or event so you can route back to it after losing the context.

A good way to create chatbots in a more efficient way is to make use of reusable flows. These flows are generic and can be re-used in multiple situations. Some examples are a feedback (“Did that help?”) flow and a “Do you still need help?” flow.

When one of these reusable flows gets triggered, a user leaves the current context.

In most situations, this doesn’t cause any problems. When a Feedback flow gets triggered, the user probably doesn't need to return to the previous flow. It’s a linear flow where the user got an answer to their question, and afterwards entered the Feedback flow.

However, in situations like the “Do you still need help?” flow, things can get more complicated.

When the user responds with “Yes”, you could trigger a handover, but what if you want to route the user back to their original context to make them complete their flow first?

An example would be when the user has to select an option from a menu. You can add a Timer Trigger to detect if the user doesn’t respond for a specific time period. If so, you can let that timer trigger the “Do you still need help?” flow.

When the user responds to that message with “Yes”, it would be better for the customer experience if the user is able to continue from where they left, instead of letting them select how they would like to continue their conversation with the bot.

Routing to flows vs events

You can route the user back to their original context by triggering the last visited flow or event. It depends on the use case which method would be best.

If you have a flow containing multiple events, routing to the flow would always route the user back to the first step (event) of that flow, regardless of how much of that flow the user previously went through already.

By routing to the last event, you can make sure the user skips the steps he or she has already gone through, which would be best in most situations.

However, there are also some use cases where routing to the last flow could have some benefits as well. For example when using periodic broadcasts over longer time periods.

Store the last flow name

For every flow, you want to be able to go back to, add the following code to save the flow name.

async payload => {

  //console.log(payload)
  console.log(payload.match.flow.title)
  
  return {
    params: {
      // Using a spread operator, we add all provided params in the result
      ...payload.params,
      // Now we override the lastflow param
      lastflow: [{
        value: payload.match.flow.title,
        match: payload.match.flow.title
      }]
    }
  }
}

It’s also important to let the flow start with an event that has exactly the same name as the flow.

Route back to the last flow

Now you have stored the name of the last flow into a parameter, you can use another code action to dynamically route to an event with the name of the value of that parameter:

async payload => {
  
  if  (Array.isArray(payload.params.lastflow))
  {
    let lastflowname = payload.params.lastflow[0].value
  
    trigger(payload.params.lastflow[payload.params.lastflow.length - 1].value)
  }
  else
  {
    console.error("Lastflow was undefined.")
  }

}


Store the last event name

You can do the same for the last event name. Simply replace payload.match.flow.title with payload.match.step.title and change lastflow to lastevent.

async payload => {

  //console.log(payload)
  console.log(payload.match.step.title)
  
  return {
    params: {
      // Using a spread operator, we add all provided params in the result
      ...payload.params,
      // Now we override the lastevent param
      lastevent: [{
        value: payload.match.step.title,
        match: payload.match.step.title
      }]
    }
  }
}

Don’t forget to add this code action underneath all events you want to be able to route back to.

Route back to the last event

Then the code action for routing back to the last event will look like this.

async payload => {
  
  if  (Array.isArray(payload.params.lastevent))
  {
    let lasteventname = payload.params.lastevent[0].value
  
    trigger(payload.params.lastevent[payload.params.lastevent.length - 1].value)
  }
  else
  {
    console.error("Lastevent was undefined.")
  }

}
Labels (1)
Version history
Last update:
‎06-19-2021 11:11 AM
Updated by:
Contributors