How to Create a Simple Bot Using Hubot

DZone 's Guide to

How to Create a Simple Bot Using Hubot

Learn how to create your own chatbot using Hubot and customize scripts for it in the first article of a planned series.

· IoT Zone ·
Free Resource


As bots become more and more popular, it's easy to want to make one yourself. In this tutorial, you'll learn how to develop a bot using Hubot.

What You'll Need

  • Node and npm. Pretty straightforward.

What Is Hubot?

Hubot is your scriptable robot friend. Hubot is open source, written in CoffeeScript on Node.js

You can use Hubot to automate your tasks, deploy websites, test your APIs, and a lot more.

Screen Shot 2016-08-07 at 3.32.41 PM

Step 1: Install Node

Of course, first off, we need to get Node installed. This link will get you where you need to go.

Step 2: Install Hubot Generator

Open terminal and run the following:

% npm install -g yo generator-hubot

Step 3: Make a Bot Called ‘hodor’

% mkdir hodor

% cd hodor

% yo hubot

Now, you will be asked a few questions, like who is creating the bot and adapter you want to use. Ignore the adapter part for now. We'll come to it in our next blog,“Integrate Hubot to Your Slack”.

Step 4: Run the Functional Hubot Inside “bin/hubot” Folder

% bin/hubot

Press enter.

hodor>hodor ping

Hodor will reply with PONG.

Type hodor help to see the list of commands supported. Now go to the “hodor” folder in your system. You can add your scripts inside “scripts” folder. Read more about CoffeeScript here.

Open “example.coffee” in your editor.

Add the following after ‘module.exports = (robot) ->’

robot.hear /hi|hello|hey|/i, (res) ->
res.reply ‘hey, whats up’
robot.respond /what is your name/i, (res) ->
res.reply ‘Hodor Hodor Hodor’

Stop and run Hubot again via % bin/hubot.

In a script, if you want Hubot to reply to chat that is directed to him, use robot.respond. 

If you want Hubot to reply even if it is not directed to him then use robot.hear.

Type any of hi/hello/hey and Hubot will reply with “hey, whats up”.

Type “hodor what is your name” and you will get the reply “Hodor Hodor Hodor”.

Screen Shot 2016-08-07 at 2.53.37 PM

Congrats on building your first bot!

Next, we will cover adapters and how to integrate your bot with Slack.

bot, bots, chat, chatbot, chatbot development, chatbot platform, coffeescript, hubot, messaging, node

Published at DZone with permission of Devashish Mamgain . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}