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


We’re inching close to building our first bot. I am sure you want to build a better, more complex bot later on instead of just an echo bot. So in this tutorial, instead of actually building the bot, we will refactor (re-arrange) our code so the code remains easy to read and adding more features stays easier for you.

Concept #7: NodeJS Modules

NodeJS modules are a great way to “divide” your code into different parts. They makes it easy to test the code and also to re-use it. We’ve added request and restify modules to our package.json. We have “re-used” them. Someone wrote them, we used them. Now, we’ll create our own modules. The way we are going to do that is by cut-pasting some part of our code into a different file and adding a few lines to tell NodeJS that this is a module that can be require()’d. We’ll start simple.

Note: All these modules must be in the same folder as index.js

Step #16: Creating the hello.js Module

In our index.js we currently have a “/hello/:name” endpoint to check if everything is working properly or not. The way we created this endpoint is by associating a function with a GET request on “/hello/:name”. What we will do now is cut-paste the function into a file of its own named hello.js

see how much I have selected?

Look at how much I have selected. I have just selected the function. 
To be specific, I have selected function(req,res,next){ .. }. This we will cut paste into a new file. So go ahead and open your text editor, cut paste this selection there. It should look like this:

This is not a module, yet. We will make it a module by adding just one more line before the function like so:

All I have done is add “module.exports =” before the function. This made our function into a module.

The reason why we are doing this is because the function which will be called when Facebook sends message to you can get very large very quickly depending on how complicated you want to make your bot. So, this is a preemptive step to maintain cleanliness.

Step #17: Requiring the Module

In the index.js file, require this newly-made module by adding this line below the other require() statements:

var hello = require(“./hello”);

Now, you have your server.get() looking like this:


Plug in your module by typing “hello” right after the comma. You can go back and test if all went fine by using npm start like I showed you in Part 2.

We’ll now make 2 more modules. In the first module we will simply cut-paste the function we have for “/webhook”. The second one will be an empty module which we are creating in anticipation of Facebook messages.

Step #18: Creating the webhook_get.js Module

Cut-paste the function for “/webhook” into a file of its own named “webhook_get.js”. 
Add “module.exports =” before the function name. 
Add var webhookGet = require(“./webhook_get.js”) below the require statements you already have. 
Complete the server.get(“/webhook”) by add webhookGet after the comma.

Step #19: Creating the webhook_post.js Module:

Just copy paste the following into a file named webhook_post.js:

This is where we will be filling in the code.

Step #19: Requiring the Module:

In index.js, add the following line below the current requires:

var webhookPost = require(“./webhook_post.js”);

And then, add the following line:“/webhook”,webhookPost);

Your index.js should look like this

Step #20: Add to Git

In the terminal, enter the following commands one by one:

git add .
git commit -m “Modularized the project”

We have now created a template bot that can be reused to create more bots later on. You have to remove the Git remote but I’ll defer that for later. Copy paste your entire folder somewhere for later use. :)

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.