How to Build a Slack Bot Using Laravel

by Noor Ali  September 29, 2016

Today, I demonstrate how you can build a simple “Hello World” Slackbot by using Laravel. Slack is a cloud-based team collaboration tool co-founded by Stewart Butterfield, Eric Costello, Cal Henderson, and Serguei Mourachov.

It offers a lot of IRC-like features including persistent chat rooms (channels) organized by topic, as well as private groups and direct messaging. Today, we will be building up a Slack bot using Laravel.

how to build laravel powered slack bot

Let’s have a quick look at what do we require before starting the project.

Here are some prerequisites:

  • You must have a Slack account.
  • You must have an active Cloudways Hosting server. (This one’s optional, though.)
  • You must have a working Laravel app.

Step 1: Get the Package

First, you have to install the necessary package for handling the Slack messages.

Now, let’s add Maknz\Slack\Laravel\ServiceProvider to the provider array in config/app.php:

Then add facade to your aliases array:

Finally, publish the Config file:

Step 2: Setup Config

Use the following syntax to edit the slack.php file:

Now, let’s get the webhook URL so the stack could be integrated with the Laravel app. Click on Gear ⚙ icon and add an app or integration from your Slack room.

laravel-slack-bot-1

Click the Build button in the top corner.

laravel-slack-bot-2

Now, click Make a Custom Integration.

laravel-slack-bot-3

Next, click Incoming Webhooks.

laravel-slack-bot-4

The webhook URL would look like:

laravel-slack-bot-5

Copy this URL. Next, edit config/slack.php to set up the endpoint.

Change this:

To this:

Remember to input your Channel and User Name.

Step 3: Make the Routes

For this app, I will create a simple triggering route that will push the Hello World message onto the Slackbot.

Step 4: Make the Controller

This will be a simple controller that contains the actual logic to push the message to the Slackbot.

Save the file. Once done, it is time to test the bot.

Go to the link of the app that might look something: http://phplaravel-xxxx-xxxxx-xxxx.cloudwaysapps.com/HelloWorld

The Slackbot would serve the Hello World message.

Try cURLing again and again and you will see the Hello World message popping again and again. 🙂

I hope that you have set up the Slackbot without any issues. If you have a question or would like to add to the discussion, please add a comment below.

Create Laravel apps without the worry of server management.

Deploy your app on optimized PHP hosting servers for Laravel.

About Noor Ali

Noor Ali is an Associate Software Engineer at Cloudways. He loves to solve technical problems through programming and mathematics.

Stay Connected:

You Might Also Like...