Create simple Slack Bot with NodeJs and Botkit

For developers, it's no stranger to Slack . For those of you who don't know Slack, I would like 30 seconds of advertising.

Slack is a chat tool, making it easier to exchange information and update work than ever. There will be a lot of people wondering, using Skype is more than enough, so why Slack Better than Skype. Can be summarized as follows, Slack is more stable than Skype, better support on mobile, notification smarter, and above all, support better integration than other tools like Trello, Github, … also if you still wondering what Trello and Github are, one is to search google, the other is to look here .

So what is Slack Bot? Listening to a call can easily be guessed that it is a bot used in Slack, when we call it with a specific command, it will execute a certain task for us.

Such introduction is considered sufficient. Start time.

First, make sure your computer has installed the environment for NodeJs. If not, go here to download and install.

After making sure your computer has installed the environment for NodeJs. Then we will go to the next step, create and install the project.

Create a directory to contain your project, assuming the name is slackbot. In the slackbot folder, in the terminal interface, enter the following.

Please fill it out or skip it if you feel it is unnecessary, I think, you should leave it all by default, we can fix it later, then npm will generate the package.json file. This file will contain information about your project.

After you have the package.json file, please install the botkit package, about the botkit and the related documents you can refer here . This can be described as if you include a library in your project. In the terminal interface you enter the following.

You can remove –save from the statement without any error, we can understand –save here means that it will save the dependency in the package.json file so that if you have it Retrieving the code and rebuilding it on a new environment, npm will automatically download the saved dependencies in the package.json file.

After we have installed the botkit package, we create the index.js file, this is the file that we will code for the bot. Open the index.js file and prepare the code.

First, you create the bot with the botkit as follows.

As you can see, in the above code, I initialize a slackbot with the debug attribute true, why? Because I want to see that log. The nature of the bot is to use Slack's Real Time Messaging API . The bot will connect with Slack's API via API Token, below I will guide you to get API Token.

To get the Token API, you must create a bot on Slack's system via the following link https://my.slack.com/services/new/bot and follow the instructions. You will then see the API Token as shown below.

Screen Shot 2016-04-10 at 4.png

So you have created the bot. To run the test, in the terminal, enter the following command.

Next we need to add behaviors to the bot. I plan to create a dirty talking bot, entertainment purpose. Specifically, this bot will salute when we say hello, if it knows my name then it will say yes to my name. Also, we can ask for its name, then tell it how it should be called.

The first is the greeting. Open up index.js file.

As you can see, I propose the context when the bot sees it receiving a message directly, or being referred to as a "hello" content, or "hello," it responds by checking. if it already knows the user, if it does, it will send a "hi …!" message otherwise the simpler message is "hello!"

Next will be the repertoire for the bot to know how to call me.

As you can see, to let the bot know how to call us, we will tell it "you can call me …", in the processing section we will save the name that the user typed in corresponding to the user id . Then the bot will answer "oh, from now on I'll call you …".

Similarly, I did some things and I finally got the conversation with the bot as follows.

Screen Shot 2016-04-10 at 3.18.01 PM.png

Interesting isn't it, through this article I hope you understand the most basic things about using botkits to create Slack bots. Wish you have fun creating Slack bots and creating more useful bots than I did.

Source code in the lesson you can find at https://github.com/codeaholicguy/nodejs-slack-bot

See you in the next article.

Have fun!

ITZone via codealohic

Share the news now