Keep Coding

Never Stop Learning

Build your “What’s for lunch” Slack Bot

Botonomous Framework LogoHave you ever wondered having a Slack Bot in your team that suggests where to have Friday work lunch? If so, this tutorial will walk you through the process of building your own Slack Bot in PHP using Botonomous Framework and Slash Commands.

Why Botonomous?

Botonomous is a fast and modern framework for Slack that helps developers to make bots easier. It supports both Events API and Slash Commands and is advanced enough to handle all the communications with Slack.

Why Slash Commands?

Slash commands are simpler to setup comparing to Events API and Real Time Messaging API. Adding a command to a Slack app is straightforward and it only needs a few config changes in Botonomous.

What are the requirements?

In order to build a bot using Botonomous the followings are required:

  • Server running PHP 7.0+
  • Slack team and the permission to install custom integrations

Prepare the server

Either your bot uses Events API or Slash Commands there should be a server sitting somewhere listening to Slack requests. For example when someone types /lunch and hits enter in a channel this data would be posted to a URL served by your server:

I personally use DigitalOcean to host my web applications but for the sake of simplicity in this tutorial we are going to use a local server like MAMP. I assume it is already installed on your machine and is publicly accessible using ngrok. If you have not done it before follow the instruction here but all you need to do is to run the following command in the terminal after downloading ngrok file:
./ngrok http 8888

This generates a public URL e.g. https://c28fcdb5.ngrok.io which is pointed to your local environment:

ngrok

Please keep this terminal open.

Setup the command in Slack

Creating a slash command in Slack is briefly mentioned here. For the full details you can also follow these steps:

  • Head to “Building Slack apps” page and click on “Create a Slack app” button
  • Name your app, choose the team (if you have multiple teams) and click on “Create App”

Create an App

  • Under “Basic Information” click on “Slash Commands” and then click on “Create New Command”
  • Fill out the required fields including “Command”, “Request URL” and “Short Description” as follow. For the URL field enter your ngrok one.

 

Create New Command

  • In “Your Apps” click on the one you just created and under “Install your app to your team” click on “Install App to Team”
  • In the access authorization page click on “Authorize” to finish creating the command:

Authorize Slack App

Install Botonomous

The easiest way to install Botonomous Framework is by using Composer:

  • In a new terminal change the current directory to MAMP htdocs:
    cd /Applications/MAMP/htdocs
  • Run this to make lunch-bot folder as well as installing all the dependencies:
    composer create-project botonomous/botonomous lunch-bot
  • Set the MAMP root document to this by clicking on “Preferences…” and “Web Server” tab:
    /Applications/MAMP/htdocs/lunch-bot/src/Botonomous/public

MAMP Web Server

  • With any text editor open:
    /Applications/MAMP/htdocs/lunch-bot/src/Botonomous/Config.php

    And update verificationToken with the one in your app page under “App Credentials” in Slack

  • Open:
    /Applications/MAMP/htdocs/lunch-bot/src/Botonomous/Dictionary/question-answer.json

    And update it as follow:

Now each time someone types /lunch where to go? in a channel the bot randomly answers one of the options:

Lunchbot demo

Troubleshooting

There are a number of ways to figure out why your bot does not work as expected:

  • Make sure your server receives Slack requests by checking “HTTP Requests” in the ngrok terminal. If you do not see any requests check to see if “Request URL” is the same as the one generated by ngrok.
  • If requests appear in the ngrok terminal but the bot does not give you any answers check MAMP PHP logs:
    tail -f /Applications/MAMP/logs/php_error.log

Well, as you saw integrating a custom bot in your team is almost a quick job. However, this was a simple example mainly to show you how Slash Commands and Botonomous Framework work together. For more advanced bots you might need to check Botonomous events listener, something that I will write about it next time.

2 Comments

  1. You are using DigitalOcean server. here I find a nice tutorial on managing servers on DO by creating a slackbot.

    https://www.cloudways.com/blog/how-to-create-slack-chat-bot-using-cloudways-api/

Leave a Reply

Your email address will not be published.

*