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:
- Channel specific flows
- Conversational Calculator How to use Math in your Chatbot
- Create a location-aware chatbot and find the nearest store locations around the user
- Create quick replies in Cloud Code Actions
- How to add a delayed opening to the web widget
- How to build a chatbot quiz for WhatsApp
- How to catch API errors within Code actions
- How to check and verify user input
- How to check if an object property is undefined
- How to create a dynamic opening based on the part of the day
- How to create a Dynamic URL
- How to create a Webview in 3 steps
- How to let your bot handle images in 3 steps
- How to retrieve data from a database
- How to retrieve filtered values from an external database with multiple properties
- How to route back to the last flow or event
- How to route your chatbot for numbers that are greater/less than or equal to a specific number
- How to send a confirmation of receipt by email
- How to send chatbot data to a database
- How to send emails to multiple recipients
- How To Use Dynamic Google Maps Images
- How to use QR codes for AI chatbots on Web, Messenger, and WhatsApp
- How to use Javascript Comments in actions
- How to Store Parameters
- The easiest way to capture and validate an address
- Where to find your Organization ID
- Working with dates
- How to add a Web Widget to your Wordpress website
- How to check if the user is on a mobile or desktop PC
- How to check if the user is on the homepage or a specific page
- How to combine functions in Javascript
- How to set profile attributes when a user is anonymous
Updated 11 months ago