Trigger Event if User Abandons Chat

Abby Updated by Abby

If a user abandons a chat there are several actions we can take to get them to re-engage.

Here we will useΒ PipedreamΒ to trigger an HSM template for users who didn't complete the flow

In this example, we wait 48 hours after the chat has been initiated, then use Pipedream to check the Landbot API to see if the variable we added for 'flow completed' is false, if so we send a follow up message with an HSM template

However, if we set the timer to 23 hours (within the 24 hour window) you could alsoΒ assign them to a webhook blockΒ to create a ticket inΒ ZendeskΒ orΒ Intercom, or simply send a message without the need to use HSM

The flow:

We can make our flow as complicated, or as simple as we'd like, the important things here are the variables and the Trigger Automation block

At the beginning of our flow we need a variable 'completedflow' that equals false, if you change the name of this variable you will need to change the code in Pipedream, so unless you have a developer it's not advised to change the name here

At the end of our flow we will change 'completedflow' to true, this means we won't send any follow up if they reach this point

Let's go to Pipedream now (you can use this Pipedream template to get started)

Pipedream:

The first module we use here is a trigger, this takes in the user ID from Landbot (we'll get to that later)

This is what the configuration will look like:

Next we'll copy the unique url that it gives us:

Let's go back to the bot, we'll add a Trigger Automation block with the following test values:

We'll test this using 'Test webhook trigger' just to be sure we've got the right url, it should return a 200 in Landbot, and the results should look like this in Pipedream:

Let's press continue

Next we'll add the workflow_delay module, we can select seconds, minutes, or hours. I've selected 48 hours for the bot in production, however for testing I've selected 8 seconds

Let's press continue

Next we'll add a Node.JS module, here you'll need to add your own API Token where you see XXXXXX, and change the HSM template ID for the one you choose to send

Get your API Token from here.

You can paste the following code in the Node module to send a Template:

import axios from 'axios';
export default defineComponent({
async run({ steps, $ }) {
const customerID = steps.trigger.event.body.id;
const res = await axios.get('https://api.landbot.io/v1/customers/' + customerID + '/', {
headers: { 'Authorization': 'Token XXXXXXXXXXXXXX' }});
let completedFlow = res.data.customer.completedflow;
console.log(completedFlow);
if(completedFlow == 'false'){
let auth = 'XXXXXXXXXXXXXXXXXX';
const options = {headers: {'Content-Type': 'application/json','Authorization': 'Token ' + auth}};
return await axios.post(`https://api.landbot.io/v1/customers/${customerID}/send_template/`, {"template_id": 3424,"template_params": { "header": { "params": [] }, "body": { "params": [] }, "buttons":[ { "params": [] } ] },"template_language": "en"} , options)
}
else{
}
}
})

Let's test, deploy and start using our workaround!

Or in case you need just to send a Message, use this code instead:

import axios from 'axios';
export default defineComponent({
async run({ steps, $ }) {
const customerID = steps.trigger.event.body.id;
const res = await axios.get('https://api.landbot.io/v1/customers/' + customerID + '/', {
headers: { 'Authorization': 'Token XXXXXXXXXXXXXX' }});
let completedFlow = res.data.customer.completedflow;
console.log(completedFlow);
if(completedFlow == 'false'){
let auth = 'XXXXXXXXXXXXXXXXXX';
const options = {headers: {'Content-Type': 'application/json','Authorization': 'Token ' + auth}};
return await axios.post(`https://api.landbot.io/v1/customers/${customerID}/send_text/`, { "message": "Hello" } , options)
}
else{
}
}
})

How did we do?

Different ways to format numbers with JS

Calculate Distances in WhatsApp

Contact