Code-based Actions: Access the last flow or event

You can use Code Actions to save the recent most flow with a name. This is helpful to access and reuse the flow in terms of contexual challenges while switching between flows during a conversation.

An example would be an user feedback flows: Did that help? and Do you still need help? When one of these reusable flows gets triggered, a user leaves the current context. 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 gets an answer to their question and then enters the Feedback flow. However, in situations like the “Do you still need help?” flow, things can get more complicated. When the user responds with a Yes, you can trigger a handover. What if you want to route the user back to their original context to make them complete their flow? You need to save the last interaction as a flow for the sake of context.

Another 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 within specific time period. If so, you can set up the timer trigger with a Do you still need help? flow. When the user responds to that message with Yes, the user should be able to continue from where they left off instead of making 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 and 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 multiple flow journeys the users have navigated. By routing to the last event, you can ensure that the user skips the steps to navigate repeatedly.

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.

📘

It is important to start the flow with an event with the same name as the flow.

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  
      }]  
    }  
  }  
}

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. 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  
      }]  
    }  
  }  
}

Ensure to add this code action below all events you want to access.

Route back to the last event

The following code action is for routing back to the last event:

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.")  
  }

}