Non-Techie’s Guide to Building Bots. Part 2.

Welcome to the dark side

Let’s get into actually building a bot. There are a few things you’ll need installed first. I won’t get into how to install these things because it’s very specific to the operating system you are using. I am using Ubuntu Linux but I’ll try my best to keep it as simple as possible. Here’s what you need:

  1. NodeJS
  2. Heroku Toolbelt
  3. Git

Stop right here. Install them and then come back to follow along.

So you’ve installed them and you are ready to follow along. We’ll build a very simple echo bot that simply echos back everything the user types. Nothing fancy. No carousels. No images. No buttons. Just a simple annoying bot that never stops repeating what you say.

Step #1: Create a Folder

Create a folder somewhere. We’ll be putting all our code here. Give it an appropriate name. Don’t be like me and name it acsdvdvtuvtkb. Give it a meaningful name — probably the name of the bot you are building.

Step #2: Open a Terminal in that Folder

Well, there is no easy way to explain this. You’ll have to open the terminal window (the command line) in the folder you just created. In my case, I just opened the folder, right click, and “Open in Terminal”. In your operating system it may be different. Look around.

Step #3: Start an empty Node project

Start an empty project by typing the following in your terminal:

npm init --yes

This will create a package.json (Oh look, JSON in a file!!) which contains default settings for your software. Here’s what the contents look like:

Like I mentioned in my previous post, JSON is human-readable. package.json has all the information that’s needed for your software to run. We haven’t added other modules yet. Here’s what you need to know about package.json. It has the name, version, and description of the app. Pretty self-explanatory. Then there is “main”. This is the main file of your software where NodeJS will look for your code. It says “index.js”. We’ll need to create a file named “index.js” which we will do a little later. Then there is “scripts” field which says that no tests have been specified. Ideally, all software must be tested before being released to the user. We aren’t building anything mission critical so we’ll remove this. When Node starts executing your software, it’ll go to the “scripts” field of package.json and execute the tests. Also, it will start the software by looking at the “start” field. We need to add this.

Step 4: Edit package.json

Remove the “test” line completely. Add the following line in its place:

“start” : “node index.js”

So now your package.json should look like this:

All nice and dandy so far. Let’s add the modules we’ll need to build our bot.

Step 5: Add Modules

From yor terminal, execute these command one after the other

npm install --save restify
npm install --save request

This will download two modules — Restify and Request. Restify is what we need to build our communication “infrastructure” with Facebook. Request is what we will use to send the messages back to the user. The --save causes these names to be saved in package.json under “dependencies” field. These modules are “dependencies” — we depend on them. You’ll also see a node_modules folder which actually contains these modules and the modules *they* depend on.

Step 6: Create index.js

Open your favorite text editor and type the following and save the file:

Let’s go line by line. We start by require-ing the modules that we had added previously. We then create the server. We add a couple of plugins so we can process the data that is sent to us. We then create a simple endpoint called “hello”. We are creating this endpoint to measure our progress. If all goes well, we should be able to see a “Hi!” sent to us.

The mechanics is this: 
When you send a “GET” request to the server on the url like “/hello/Mark”, our server will call the function we passed to it. The function will look for the name sent to it or it will send assume the name to be “there!” if no name was sent. We create a simple object and store our message in the “text” field of that object. We then send a response back using the send() method of the response object and then we move onto the next() thing.

Step 7: Seeing if it works

While your terminal is in the folder where index.js is, type the following:

npm start

This will start our server. You should see the log message printed in your terminal. Our server is listening on port 8080. A port is a point of communication. Let’s see if all is working well. Open your browser and enter the following URL:

http://localhost:8080/hello/bot

This should give you the response:

{“text”:”Hi, bot”}

Note: If you are on a Mac machine and you encounter something like this:

{ [Error: Cannot find module ‘./build/Release/DTraceProviderBindings’] code: ‘MODULE_NOT_FOUND’ }

then ignore it. It’s benign.

I’ll stop here for now. More to come in part 3 :) Happy coding!