Use BotKit to build a Slack chatbot with Sun Tzu and Tony the Panda. Complete beginners guide! Part 2

Use BotKit to build a Slack chatbot with Sun Tzu and Tony the Panda. Complete beginners guide! Part 2

Day two, 6 o’clock in the morning, somewhere in the mountains

Ah! You! Great to see you again, did you enjoy your walk up here? You had to wake up at 3 am? You say it’s strange to meet at the peak of the mountain to learn to build chatbots?

Agh! Don’t be such a weener! Here man, drink some stream water. I put a little something in. It’s called The Bad Dragon. Total badass. Not completely legal in these mountains. Shit, sorry! I put a panda dose!

Oh brilliant! What did you do Tony? You drugged our guest! Look at his eyes! Ok… he’s in 10th dimension now…

He looks relaxed.

Hopefully, you will be able to focus my friend. I’ll try to make this lesson short. Well, in your condition, everything complex would probably be lethal.

Yesterday we learnt how to build a simple Slack chatbot using node-slack-sdk. If you try to concentrate you will recall that that chatbot had a serious flaw. It replied to EVERYTHING ANYONE said inside the chat.

Today I will initiate you to the second circle of wisdom. We shall learn what BotKit is and how to use it to build chatbots. At the end of this lesson, our bot will do exactly the same but will only reply to messages that are either directed at him or mentioning him.

Tony, please explain what BotKit is.

Hey, buddy, you hear me? Wake up!

BotKit is a toolkit for creating chatbots compatible with various platforms:

  • Slack
  • Cisco Spark
  • Facebook Messenger
  • Twilio IP Messaging
  • Microsoft Bot Framework

Basic BotKit functionality covers:

  • Sending and receiving messages. It abstracts the communication protocols and underlying data structures.
  • Building complex conversations with multiple topics/threads.
  • Sending questions, receiving answers and handling unexpected behaviour.
  • OAuth process. You can quickly start a server handling OAuth and logging in. Very useful for Slack Apps.
  • Building buttons and interacting with users.

The tool was created by a company called Howdy (one of the first companies that started building Slack chatbots). BotKit can be supplemented by a BotKit Studio, which, as far as my panda brain reaches, is the first IDE dedicated to chatbots.

If that’s not enough, go to BotKit.ai website, BotKit GitHub page or to their GitHub page specific to Slack chatbots.

Was this good sensei? Can I go for a burger now?

It was perfect Tony. Here, take 10 pandollars and make it three burgers. When our guest comes back to earth, he’ll be hungry like a sloth.

One burger later

You think he’s awake?

I don’t know. His face looks funny. Poke him with a bamboo stick maybe?

[pokes you with a bamboo stick]

He’s not moving sensei. Maybe it’s something in these burgers. They had a funny taste.

[suspiciously looks at his untouched burger and hides it in under a bonsai tree pot]

Naah, mine was fine. It’s probably the air. Very thick here, too much oxygen.

Let’s continue anyway.

This time we will work with gomix only. If you wish to work locally please do the following:

If you need more details go back to the past where we discussed the matter more carefully.

If, however, you don’t want to setup your environment and you just want to learn, simply remix this code. Check these gomix instructions if you don’t know what gomix is.

To run this bot you must first follow part one instructions to add a custom integration to your Slack team. If you paid attention last time you should have a slack bot token at hand. Inside gomix, open the .env file and paste that token there:

And that’s it. Open up Slack and you should be able to talk to your bot. Isn’t that pure magic?

It’s time to delve into the code.

Open suntzu.js file.

Not much has changed since the last time. In fact, the code is now simpler. Look at lines 24-31:

You must first create an instance of a controller. To do so we used Botkit.slackbot method. It is able to take a plethora of arguments, but in this example, we are just telling it to disable the debug mode and opt out from sending BotKit statistics. Please feel free to opt in if you wish to contribute to their insight. I imagine they use it to measure a number of users and improve their API.

Controller.spawn spawns an instance of your bot and connects it to your Slack. startRTM initiates Real-Time Messaging connection.

There is only one more piece of code which changed:

Observe how we replaced the old, clumsy code handling the communication with this line:

The first argument of this method is a regular expression. Your bot will react to everything that matches it. In this case, we want our bot to reply to every message. Except! We don’t want it to reply to everything. Hence the second argument which takes a list of filters is telling your bot what to react to. This bot will reply when you write a direct (private) message to him, when you directly mention its name (direct mention begins with the bot’s name for example “@sun-tzu please help!”) or you mention it otherwise in the message (for instance in the middle of a sentence “We need @sun-tzu to help us with global warming.”).

To see how using BotKit simplified sending and updating a message let’s take a look at line 61:

You just need to call this method with your original message as the second parameter (the first parameter is the original message your bot is replying to) and later call the updateCb method (yes, it is a callback).

Wasn’t that simple? You’ve just learnt the basics of BotKit with just a few lines of code. And, you have a bot you can use in your office, just for fun!

Now please make yourself at home in my garden. Your room is also ready if you want to meditate in isolation.

I must now take a break. You shall find me in my meditation cave right behind those trees, tomorrow. I will be waiting for you with a new lesson: how to move from custom Slack integration to a Slack app.

Take care and make a comment if you want me to write about something else!

Leave a Reply

Your email address will not be published.