Skip to content

Modules

In a code node, you're able to 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.

moment.js

Moment.js Documentation

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();
actions.output(utc);

Lodash

Lodash Documentation

Cognigy.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);
actions.output(last);
In this example we're extracting the last element of the favouriteFoods array and sending it as an output message.

Messenger Platform

XML-js Documentation

In 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

Text Cleaner

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

Parameter Type Description
locale string The locale for which to instantiate the class. For example, 'de' or 'en').
options object See 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 } 
}

Returns

Instance of the Text Cleaner class.