Community Post

Creating a Slack bot using botmanio

Valentine Oleka

Slack is where work flows. It's where the people you need, the information you share, and the tools you use come together to get things done. It is a top of the chart communication tool that makes working in teams seamless and interesting. Slackbots are there to make the experience even way better. With slackbots, you can automate reminders, connect to your application on a different server or just tell a joke every 1pm on a work day. The possibilities? Limitless.

What is Botman?

If you check botman.io you’d see “BotMan is a framework agnostic PHP library that is designed to simplify the task of developing innovative bots for multiple messaging platforms, including Slack, Telegram, Microsoft Bot Framework, Nexmo, HipChat, Facebook Messenger, WeChat and many more”.

In simple English, it’s a PHP framework that removes all the long processes and late nights in front of a green screen for like a gazillion hours, and lets us just build a simple bot without having to go to code school. Easy right?

For this tutorial, we are going to build a chat bot for Slack. Yes, it is not very difficult to do and you don’t need to be a coding ninja to do it. By the way, ninjas sneak around in the dark and probably kill people but coders always have the lights on at night.

This is an example of what we are building.

This is what we are building

Requirements

There is no special requirements. You just need to have a few things on your computer

  1. You need to have git installed your computer (if you want to push the code to a repository)
  2. Composer (we would use it to install botman)

Let us set up a few things to help us get started

  1. Create a team on Slack.
  2. Just some advice, name all your new creations something similar just so it looks cooler, nothing else.
  3. Download and install ngrok from https://ngrok.com/download

Create an app and a bot user for our slack team.

  1. We would go to https://api.slack.com/ and create our app from there.
  2. Click on Start Building
  3. When it opens up, click Create New App

  4. Add a name for your bot and select the slack team you want. Then Create App

  1. Create a bot user from here

We are done for now. Do not worry, we would still come back to this to get stuff working.

Let’s get started with Botmain

To begin, we will need to install the botman studio. Launch Terminal and type in the command below:

$ composer global require "botman/installer"

Once the installation is complete, we would have access to a botmancommand that we can then use to create bots.

To create a new bot, enter the command below:

$ botman new bot

When the installation is complete, our application is now able to communicate with Slack. To start a PHP server run the command below:

$ php artisan serve

Configuring Botman to work with Slack

First, we will need to subscribe our app to an event. Slack would not give us an access token if we do not have the app installed, and we cannot install the bot without subscribing to at least one event. Makes sense?

To subscribe to an event, we need to have a url where slack can authenticate to be sure we actually have a real app that will work on slack. Remember when we installed ngrok? Let’s go make it useful to us now.

Open your terminal/command line and change your working directory into where you downloaded ngrok (most likely downloads for most persons). When you are there, run the following command

Our botman app uses laravel which serves on port 8000 by default, so ngrok will tunnel whatever is running on port 8000 so that you can access it from anywhere in the world.

Copy the url it generates for you. Slack only accepts connections via https, so take the one with https.

Now, head back to slack and have it setup

Setup events

After enabling events, enable interactive messages so we can send clickable buttons to slack.

Setup interactive message

Install the app when you are done and get the Bot User OAuth Access Token. Open your terminal and change your working directory to the installation we made for our bot. Run the following command

$ echo SLACK_TOKEN=YOUR_SLACK_BOT_USER_TOKEN >> .env

This will add our SLACK_TOKEN to our app.

Let’s define some Logic for our bot now.

<?php
use App\Http\Controllers\BotManController;

$botman = resolve('botman');
$botman->hears('Hi', function($bot) {
  $bot->reply('Hello!');
});
$botman->hears('Who is your daddy?', function($bot) {
  $bot->reply('Uncle Neo!');
});
$botman->hears('Start conversation', BotManController::class.'@startConversation');

$botman->hears - This is the message sent to the bot privately or posted to a channel to which you want to react.

$botman->reply - This is what your bot should respond when the message comes in.

There is another case for conversations that have buttons and all, but we would not get into all of that. You can check it out yourself later.

So, use hears and reply to define a few conversations for your bot. When you are done, save the file and go test on Slack. When you are done, go test it on Slack.

Congratulations! You have built a Slack chatbot using Botman.

CONCLUSION

There are a lot of cool stuff that can be done with Slack bots, but it entirely depends on who is building and what they hope to achieve. Make sure to read Slack documentation on apps and bots to learn about what is possible. Keep practicing and trying out cool stuff. Do not forget to put it on your repo so that people can see and even try out what you have build.

Here is a link to my repo where the code above lives Repo Link. Feel free to check it out if you get stuck while making the app. Also, checkout Botman.io documentation for more features botman has to offer https://botman.io/2.0/.