Modules
Description

In a code node you're able to use some predefined modules.
On this page you'll find all the necessary informations to run these modules in a code node.
moment.js

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

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 informations.
Example
Let's see the following example to get the capabilites 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 output it to the contact.
Messenger Platform

For building Facebook Messenger messages (quck replies, buttons, lists, generic templates etc.) you can use the facebook-bot-messenger module witihn a code node.
Usage
In a code node you can create a message type of your choice by using the MessengerPlatform namespace. For a list of possible message types please visit the facebook-bot-messenger documentation.
Example
The following snippet can be used to generate a quick reply message. Please keep in mind that the built message (builder.buildMessage()
) has to be the value of the message property of the _facebook object.
// use facebook-bot-messenger to compile reply
const builder = new MessengerPlatform.QuickRepliesMessageBuilder('Pick a color:');
builder.addImageOption('Red', 'DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_RED', 'http://petersfantastichats.com/img/red.png')
.addImageOption('Green', 'DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_GREEN', 'http://petersfantastichats.com/img/green.png');
// output the reply
actions.output("test", { "_cognigy": { "_facebook": {"message": builder.buildMessage() }}});
xml2js

In case you want to parse xml data you can use this module.
Usage
Within a code node you can call the following method:
xmljs.xml2js(<XML_DATA>);
Example
You can download this XML Bot
and import it into your project.
Description
It displays a simple use case for the xml2js module by requesting xml data from an API and parsing the result to and object (in the code node).
Updated over 3 years ago