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.
Join the DZone community and get the full member experience.Join For Free
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.
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
Hodor will reply with PONG.
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
In a script, if you want Hubot to reply to chat that is directed to him, use
If you want Hubot to reply even if it is not directed to him then use
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”.
Congrats on building your first bot!
Next, we will cover adapters and how to integrate your bot with Slack.
Published at DZone with permission of Devashish Mamgain. See the original article here.
Opinions expressed by DZone contributors are their own.