Code¶

Description¶
Code Nodes enable a Flow creator to execute custom JavaScript or TypeScript code within the Flow. The editor has full IntelliSense and exposes all Cognigy objects and actions. For better performance, the TypeScript code will be transpiled when the node is saved. If the transpilation fails the code will be saved and an error icon will appear in the top right corner of the Code Node, indicating that there is a potential issue with the code.
The execution of the Code Node will be synchronous, the Flow will continue after the Code Node has finished executing.
Just as within other Nodes, input
, context
and profile
can be accessed (and modified) within Code Nodes. If the script crashes or takes longer than one second to execute, it will be stopped and throw an error. In case of an uncaught error, the Flow Execution will be stopped.
The actions
object provides access to most internal node functions within the Code Node.
For convenience, the lodash
(https://lodash.com/) and moment
(https://momentjs.com/) libraries are exposed for use within Code Nodes.
Restrictions¶
- Maximum number of characters in the code editor is 200K.
- Maximum code execution time is 1 second. If the limit is exceeded, an error is returned, and the message can be accessed under input.codeNodeError.message. The maximum execution time is not configurable. To run code that takes longer than one second to execute, we recommend using custom Extensions.
Sending Facebook JSON¶
You can send Facebook JSON directly from within Code Nodes using the output action. You can do this in two ways:
-
Writing the JSON yourself
// build the faceboook reply const obj = { "_cognigy": { "_facebook": { "message": { "text": "Hello World", "quick_replies": [ { "content_type": "text", "condition": "", "title": "Hi", "image_url": "", "payload": "Hi" } ] } } } } // output the reply actions.output("test", obj);
-
Using the facebook-bot-messenger module (more information on GitHub
// 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() }}});
Auto-completion in Code-Nodes Editor¶
When using the "input." operator in the Node Editor of a "Code-Node", all available input objects will be automatically displayed e.g.
question
entryPoint
flowName
traceId
localeId
conditionalEntryPointWasExecuted
Adding Log Statements to Code Nodes¶
To see log statements on the Logs page, place api.log()
statements into the Code Node.
- Input code in a Code Node:
const testKeyAPILOG = "Test for api.log" actions.addToContext("test.contextKeyAPI", testKeyAPILOG, "simple") api.log("debug", testKeyAPILOG);
- Result on the Logs page:
2023-01-12 10:27:08 <mark>debug</mark> ***Test for api.log*** { "flowId": "94311a23-b905-4e38-b121-9bffeb658783", "entrypoint": "63bff4588642adbc590be047", "userId": "user1234"