Gupshup has launched a new tool called Bot Scripting Tool. Using this one can develop a bot with natural language capabilities without much effort of training the system. It just requires you to define the intent and the platform takes care of the rest.
This tool can be used from the new IDE only. A Node.js library “bot-script” has been created to support this tool.
In this guide we will understand how to get started with this using the new IDE. To summarize there are following activities which needs to be completed in order for this to work:
- Creating a bot
- Installing the node library called “bot-script”.
- Creating a file with all the rules by defining the intent and the response for them.
- Using the bot-script library into the main bot code.
- Deploying your code.
Creating a bot:
Go to My bot section and create a bot and select “code your bot” option.
Installing the node library: Navigate to the new IDE and select the bot folder then go to the console section of the new IDE and use the below command to install the bot script library.
npm install bot-script ?save
Using the library in the bot code: Once the bot-script library is installed sucessfully, go to the index.js file of the bot and add the below code.
var executor = require('bot-script').executor;
function MessageHandler(context, event) {
var options = {};
options.current_dir = __dirname; //This line is required for the bot to search for the script file inside it?s own directory otherwise it start it?s search from the root directory.
options.success = function(opm){
context.sendResponse(JSON.stringify(opm));
};
options.error = function (err) {
console.error(err.stack);
context.simpledb.roomleveldata = {};
context.sendResponse("Some Error Occurred");
};
executor.execute(options, event, context);
}
function EventHandler(context, event) {
context.simpledb.roomleveldata = {};
MessageHandler(context, event);
}
Creating the script file with rules: Start off by creating a file named “default.scr” by doing a right click on the bot folder and selecting “New file” option. This is the file where you define the script of the bot.
Writing the rules in this file -
1. Every file must start with a section which has to be defined inside square brackets []. The default section is [main]. The bot tries to search for a file called default.scr and the section [main] unless declared otherwise. If you want to start the script from any other section that you have defined then add the below code in the MessageHandler before the executor.execute line.
options.start_section = "default.<section_name>" ;
Example: Let's say you have defined 2 sections main and start
and you want the bot to begin with the “start” section then add this line of code
options.start_section = "default.start;
You can also make a call to one section from another section by adding this in the script
:call <fileName>.<sectionName>
Example -
NOTE
1. A section named [common] is reserved as special section for small talk and you can?t start the script with this section.
2. You can name the SCR file anything other than default.scr too. But make sure that you mention that in the code of MessageHandler.
Example - You can name the scr file as "main.scr" with a section as [start] then you need to add the code too in the bot to start with this SCR file
options.start_section = "main.start;2. After declaring the section you can create the script which has to be executed. This script has two states
a. Bot state: This determines what message bot will send to the user.
b. User state: This determines what the user want to say to the bot. You can choose to define only the intents which you are expecting from the user.
Every section must start with Bot state followed by user states and bot states.Example:
In the above sample the first sentence is the bot state which will be sent to the user when the user starts the interaction with the bot.
Then next indented line is user state where an intent called cost is defined which will identify any sentence user says related to cost, like ? what is the cost for trading? Or
what is the price for trading?
The next line is again bot state which is sent to the user after intent matching.You can also train the bot for variations of the same intent by adding those variations to the same user state by separating them using vertical bar.
3. To send multiple messages to the user you can add a “:continue” keyword at the end of the 1st sentence. This keyword notifies the bot that the next line is also the bot state and has to be sent to the user.
Example -
These are the very basic information to get you started with bot scripting tool. In the next set of guides we will see how to send structured messages from the bot state and how to define different responses for intents and their variations.