Skip to main content
In a Code Node, you can use a selection of predefined NPM modules. On this page, you’ll find all the necessary information to run these modules in a Code Node.
You can use the moment.js module for parsing, validating, manipulation and displaying dates.

Usage

In a Code Node, call the following function:
moment(inp?:any, format?: any, strict?: boolean);

Example

the following example gets the current UTC time and date and outputs it back to the contact.
const utc = moment.utc();
api.output(utc);
For more information, see the Moment.js Documentation.
Lodash DocumentationCognigy.AI also supports the utility library Lodash within Code Nodes.

Usage

You can call functions of the Lodash module by using the following syntax:
_.keys(<OBJECT>);
You can use any Lodash function within a Code Node. See their documentation for further information.

Example

The following example shows a typical use-case and syntax of the Lodash module.
const favouriteFoods = ["pizza", "spaghetti", "burger"];
const last = _.last(favouriteFoods);
api.output(last);
In this example we’re extracting the last element of the favouriteFoods array and sending it as an output message.
XML-js DocumentationIn case you want to parse XML data, you can use this module.

Usage

Within a Code Node, you can call the following method:
const xml =
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<note importance="high" logged="true">' +
    '    <title>Happy</title>' +
    '    <todo>Work</todo>' +
    '    <todo>Play</todo>' +
    '</note>';

const result = xmljs.xml2json(xml, { compact: true, spaces: 4 });

// result will now contain a JSON representation of the xml above

Usage

Within a Code Node, you can invoke the getTextCleaner function, which retrieves an instance of the Text Cleaner class. This class provides access to various text-cleaning functions.Parameters
ParameterTypeDescription
localestringThe locale for which to instantiate the class. For example, ‘de’ or ‘en’.
optionsobjectSee below for the config options.
Config Options
{
    // additional characters which won't be cleaned by cleanDisallowedSymbols
    additionalAllowedCharacters: string[], 

    // additional symbols which are replace (for example, "minus": "-")
    additionalMappedSymbols: { [key: string]: string }, 

    // additional phrases which are replaced
    additionalSpecialPhrases: { [key: string]: string }, 

    // additions to the phonetic alphabet (for example, "cognigy": "c")
    additionalPhoneticAlphabet: { [key: string]: string } 
}
ReturnsInstance of the Text Cleaner class.
I