Node.js & Custom code

You can add javascript code in your dialog flows using the Bot does block.
Drag and drop the block and connect it to the state where you want to see executed.
You can then add your own code, it will be hosted and executed in the secure and scalable servers of the bot platform.

Warning : If you use a "bot does" after a "bot shows", the bot does will be prioritarty

Embedded Code Editor

You can then double click on the block to open the integrated code editor.
You can see some commented code snippets that may be helpful.
We currently offer support for Node.js, additional languages may be available in the future.

The code editor comes with a dark mode.

The code editor has a dark mode πŸŒ“The code editor has a dark mode πŸŒ“

The code editor has a dark mode πŸŒ“

What can I do with the Node.js?

  • Create a dynamic answer
  • Access entities and contextual values
  • Send requests to your preferred APIs and databases
  • Control connected devices
  • Implement business logic or complex algorithms
  • ...and much much more!

Is there any limitation to what I can put in my code?

While coding your business logic, please keep two things in mind:

  • In the case your code takes time to be fully executed, please keep in mind that you have a 90 seconds limit, after that, beyond that limit, the code execution will be stopped and your bot will return a timeout.
  • Always have the `callback(output)``in your code, wich indicates to the platform that your custom code has been fully executed.
callback(output); // πŸ‘ˆ Never Forget this callback

Can I keep some contextual data?

Yes you can.
Here is how you can store data as conversation context

conversation.shared_memory.my_new_field = 'my_value';

πŸ‘

Shared memory

If you are using the main bots / micro bots architecture, the data stored in the conversation.shared_memory will be available across all the microbots.

How can I leverage user data to create more personal experiences ?

If some user data has been sent to the bot platform, you may access it by code using the following syntax:

var first_name = user_data.first_name;

Can I get the user input?

Sure! Here is the code to so:

var user_message = input.text;

Can I get interesting entities from the user input?

Sure, that's the purpose of custom and system entities.
Let's see how you can do that.

  1. Custom entities
    Assuming, you have created an entity called colors, and added it to an intent that has been detected,. You can get your custom entity using the following syntax:
var color = input.custom_entities.colors[0].value;
  1. System entities
    The platform offers pre-trained entities that you can capture anytime using the appropriate syntax.
    Also, please check the system entity availability in the Language Support page.
// time: today, Monday, Feb 18, the 1st of march
var time = input.system_entities.time[0].value; 

// temperature: 70Β°, 72Β° Fahrenheit, thirty two celsius
var duration = input.system_entities.duration[0].value; 

// number: eighteen, 0.77, 100K
var number = input.system_entities.number[0].value; 

// amount of money: ten dollars, 4 bucks, $20
var amount_of_money = input.system_entities.amount_of_money[0].value; 

// duration: 2 hours,  4 days,  3 minutes
var duration = input.system_entities.duration[0].value; 

// email: [email protected]
var email = input.system_entities.number[0].value; 

// url:  https://smartly.ai,  www.google.com
var url = input.system_entities.url[0].value; 

// phone number: 012345678
var phone_number = input.system_entities.phone_number[0].value;

You can change the editor’s theme to dark and also make the editor full screen.

How can I make API calls from the bot to my information system safer?

Leveraging your webservices can unleash valuable use cases to your users.
Here is a list of things you can do to keep a good level of security while connecting your bot to your IS via public APIs:

  • Use a public key in your API calls
  • Use a private key for the bot and change it at least once a month
  • Use a dedicated and expirable access token for each user
  • Use IP filtering to restrict the access to your API to the following adresses: Bot Platform IP adresses

What version of Node.js do I get?

Current version of Node.js you can use in your bots is 10.1.0 Dubnium.

Is there any pre-installed packages that I can use?

The following packages have been pre-installed.
You can use them directly in your bot projects.

async,
bluebird,
crypto-js,
debug,
express,
github,
googleapis,
googleapis,
google-auth-library,
htmlparser2,
http,
https,
jsrender,
libxmljs,
lodash,
mathjs,
md5,
moment,
moment-timezone,
mqtt
mysql
nodemailer,
nodemailer-smtp-transport,
node-salesforce,
oauth,
purecloud_api_sdk_javascript,
purecloud-platform-client-v2,
querystring,
request,
restify,
run-parallel,
sequence,
soap,
sync,
url,
util,
uuid,
webcom,
xml,
yahoo-weather,
xml2js

As usual in Node, simply add a require('package name') at the beginning of your code before using the package.

//if you want to use the mysql package
var mysql = require ('mysql');

πŸ‘

Need a specific package?

If you need a specific package for your app, send us a request at [email protected] and our team will review it.