Skip to content

Modules

Description

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

Messenger Documentation

For building Facebook Messenger messages (quick replies, buttons, lists, generic templates etc.) you can use the facebook-bot-messenger module within 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()) must 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() }}});

Messenger Platform

Xml2js 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:

xmljs.xml2js(<XML_DATA>);

Back to top