Skip to content
FacebookYouTubeX (Twitter)

Code Action

The Code Action provides a great method to maximize your flows flexibility using Javascript language.

The language you can use is actually Javascript. We plan to add more languages in the future, starting with Python support.

You will mainly use the Code Action to develop “low code” automation when you need advanced features only provided by a programmatic approach. Keep in mind that no async features are allowed. You can only use synchronous coding. If you need to get some data from a remote web services you can do it prepending a Web Request action to the Code Action.

Use context.attributes.KEY or context.attributes[KEY] to read the value of a flow attribute

Example:

let age = context.attributes.age;

Use context.setAttribute(KEY, VALUE) to set the value of an attribute

Example:

context.setAttribute('age', 35);

In the following example you can see how the Code Action works.

Code Action Example

In this example we execute a couple of tasks:

  1. Modify the existing “age” attribute set up using the Set Attribute action (age = 2)
  2. Add a totally new attribute (jsondata) to the flow

You are able to see the result printed in the reply:

Code Action Result

The Code Action can be used to create sophisticated A/B testing scenarios in your chatbot flows. You can create logic that randomly assigns users to different conversation paths, tracks their interactions, and helps you optimize your chatbot’s performance.

Here’s what you can achieve with A/B testing using Code Action:

  • Random User Assignment: Automatically assign users to different conversation variants
  • Performance Tracking: Track which version performs better
  • Dynamic Content: Show different content based on the assigned variant
  • Statistical Analysis: Collect data for informed decision-making
// Generate a random number to assign users to variant A or B
let randomValue = Math.random();
let variant = randomValue < 0.5 ? 'A' : 'B';

// Set the variant as an attribute for later use
context.setAttribute('variant', variant);

// Track the assignment (you could send this to analytics)
context.setAttribute('test_start_time', new Date().toISOString());

// You can then use this variant in conditional logic throughout your flow
if (variant === 'A') {
    context.setAttribute('welcome_message', 'Welcome! How can we help you today?');
} else {
    context.setAttribute('welcome_message', 'Hi there! What brings you here today?');
}

This approach allows you to systematically test different conversation flows and optimize your chatbot based on real user data.

If you have questions about the Code Action or other Pingstreams features feel free to send an email to support@pingstreams.io or leave us feedback.