Chatbot lessons from a coffee machine
5 stars based on
Photo by Jeremy Waldorph. Have a great idea for a Slack bot, want to share it with the world and maybe get it listed in the Slack App Directory!
But you don't know where to start. In this tutorial we'll go through prepping a very simple bot, running it on your laptop or desktop computer, and connecting it to Slack. In a future tutorial we'll cover deploying your bot to a service like Heroku so others can use it too. We're going to use Botkit in this tutorial — Howdy. First, we need to install some things. Make sure you have node installed on your local machine. If not, you have several options for installing node.
Begin by forking and then cloning this GitHub repository: This is just the simplest possible project built around one of the Botkit example bots. It includes everything we need to not only write a bot, but set it up for distribution via the "Add to Slack" Button or even the App Directory, easy-peasy. This is a Node. Next, edit the package. Bots have claustrophobia — we need to give them some light and air. Also, if the bot is holed up in the basement apartment of your local machine, Slack won't be able to see it.
So let's open the windows a bit. But I'm going to use localtunnel. Just use npm to install:. Once your bot is running on local portit will be accessible at https: You might consider changing my-awesome-bot to something more Now we need to let Slack know that your bot exists.
Don't worry too much about the support URLs. But do be clear on the redirect URL. Look to the localtunnel output: The redirect URL will be https: Click on the "Add a bot to this app" button. You'll need to provide an -name for your bot at this point.
Finally, you need to take note of a couple of items at the top of the page: Have those ready for the next step. And then visit https: Of course, this bot is just plain dumb. You can do better than this silly example! Botkit is structured around event listeners. The most important of these is the hears listener, which kicks off an action when your bot hears something in one of the channels to which it belongs.
Let's look at an example:. This event handler is triggered when the bot receives a direct bot status like for machines from a user that contains the word "hello". The bot responds in the direct message with "Hello! You can listen for many kinds of messages besides direct messages: You can filter by messages that contain an -mention of your bot, that begin with an -mention, or simply any message at all.
You can listen for any regular expression, or a list of multiple regular expressions. Bot kit is very flexible in bot status like for machines way. To start with, let's re-write this event listener to be a bit more flexible. Perhaps we want to listen for any message directed to our bot that contains different kinds of greetings. Now our bot will respond any time it sees "hello", "hi", or "greetings" in either a DM, or a message that -mentions the bot.
Don't forget to restart your bot after each edit! If you're having trouble getting your regular expressions just right, I find this online regex tester super helpful. At this point bot status like for machines will probably want to start doing more sophisticated things, like making requests to external services, so your bot can respond with timely and useful information depending on what your bot does, of course.
There's a lot more to Botkit than this! You can learn more about Botkit's awesome features by simply perusing the Botkit documentation. Of course, you don't want to host your bot status like for machines on your laptop forever.
Or maybe you do? Over the next articles, I will show you how easy it is to deploy your bot to hosting services like Heroku, give your bot a more permanent memory, and extend your bot's functionality with artificial intelligence services.
Exposing your bot to the outside bot status like for machines Bots have claustrophobia — we need to give them some light and air. Just use npm to install: Pass the good word to Slack Now we need to let Slack know that your bot exists. Scroll down until you bot status like for machines the bot section: Running your bot Now, the time has come to run your bot. Giving your bot a voice Of course, this bot is just plain dumb. Bot status like for machines look at an example: