Khoros Flow Code-based Actions

Cloud code actions provide an easy way to make your bot truly dynamic. When using code-based actions, keep these best practices in mind.

Code Actions best practices

Use async

Cloud code is a single JavaScript (ES6) function, so we recommend writing async functions.

async payload => {  
  // your code there  
}  

Previously, you had to write nested code with callbacks.

Using the async allows you to write readable pieces of code.

// The old way  
(function(payload) => {  
  request('<https://awesome.org/1'>)  
    .then(response1 => {  
      // Do something with the first response  
      request('<https://awesome.org/2'>)  
        .then(response2 => {  
          // Do something with the second response  
        })  
        .catch(err => {  
          console.error('Error', err)  
        })  
    })  
    .catch(err => {  
      console.error('Error', err)  
    })  
})  

Async functions enable you to write simple code like:

// The old way
async payload => {
  try {
    const response1 = await request('https://awesome.org/1')
    // Do something with the first response
    const response2 = await request('https://awesome.org/2')
    // Do something with the second response
  } catch(err) {
    console.error('Error', err)
  }
}

Keep it short and simple

It's better to break up different actions.

Check if a parameter exists

Providing the parameters for the code actions is optional. Check if they are present or not. Since parameters are always lists (arrays), you can use an example such as:

if(Array.isArray(payload.params.myparam)) {  
   // We have a param  
}

Use return instead of reply

The reply() method is only intended for callback functions.

Use the await calls and return messages instead of using the reply method.

Use triggers instead of messages

Using triggers for static content. It enables you to keep all content inside the Flow editor and makes it much easier to build cross-language code and maintain it.

Read more on the code-based actions: