Lessons From a Snow Storm

This article was originally written on February 12, 2014, during a pretty bad snow storm here in NC. Original here. The big news today has been the snow pounding the south east, and I am right here…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Creating a Baby Slack Bot with Node.js

Next, I headed to my work-space and created a new channel. In the Create a channel form in the field “Send Invites to” I added my bot. Slack will auto suggest the bot for you.

Then I created my application folder and run the following command inside it to setup my project .

For simplicity, I only inserted he name of the application and the main file as index.js. The rest of the fields were left by default by pressing enter until node created the package.json file.

To be able for my project to communicate with slack I run the following command to install the Slack SDK. This command is used to install node modules in our project.

After setting up my Slack channel and my node environment, it was time to code my basic bot.

To initialize the real time messaging with Slack I created a new RtmClient instance and started the session. Here I had to insert the token generated when I created my bot application on Slack.

Then I simply added three RTM event listeners:

AUTHENTICATED: Slack response when the bot successfully authenticates. In this response the work-space information can be extracted. As you will see below.

MESSAGE: This listener waits for every message sent on the work-space. As you will see below, I filtered the listener to only process the messages of one channel.

Finally was time to test my bot with the following command.

If you are following the process, runt this command and check if in your terminal you get the message “Logged in as [your-bot-name] of team [yout-team-name]”.

If you got that message, your bot is connected to your slack work-space. Now when you open your slack account, under the channel you added your bot you should see the greetings message from the bot. Awesome right?!

In my MESSAGE listener, I checked if any user “called” the bot inside the message and if the appropriate command is given. The Bot then will give an answer depending on the user command. If a command is not recognized, the bot will notify the user the he is confused 😵 and suggests the help command for a list of commands.

Here it is! Under 70 lines of code I got my 👶 bot is up an running. I hope you guys like it. Drop a comment if you have any question or have any challenges for me.

Add a comment

Related posts:

Deb Gabor Strategically Brands Companies

Deb Gabor is the author of Branding Is Sex: Get Your Customers Laid and Sell the Hell Out of Anything. She is the founder of Sol Marketing, which has led brand strategy engagements for organizations…

A Partnership To Support Creative Learning

Recently the Clear Launch team was given a unique opportunity to give back to the community. The Entrepreneurs Foundation of Central Texas contacted us about providing technology support to another…

Why the Rays Kevin Cash Has Best Case for Manager of the Year

Dave and Andy have said it multiple times over the past two years, and they’re right. Kevin Cash has to do more managing from the second inning on then anyone in the game. But that’s not the reason…