Write your own Slack bot using Laravel

Write your own Slack bot using Laravel

This guide will show you how to configure Slack and get started with writing your first Laravel-based Slack bot using my PHP Slack Bot API.

Set up Slack

Start by creating a new Slack app for your team.

Visit https://api.slack.com/apps?new_app=1 and give your app a fancy name, as well as selecting the Slack Team you want to have this bot connected to.

Click “Create App” and take note of your App’s Client ID and Client Secret — you’ll need this in your Laravel app.

Click on “OAuth & Permissions” and enter the redirect URL: http://YOUR-LARAVEL-APP-HERE/connect/slack/.

That’s it for now — we need to revisit the Slack settings later — but first let’s move ahead with our Laravel app.


Notice that we’ve already added a variable SLACK_TOKEN which will hold our Bot token later.

We will use Slack’s new Event API to communicate with the Slack Bot and our Laravel application. The Event API works by sending a given URL POST requests with various event information, which we can then respond to.

In order to validate the URL, Slack needs some special treatment. Add the following snippet to your new Laravel application. This will add a /slack POST route to your app, which will handle Slack's URL verification.

Route::post('/slack', function(\Illuminate\Http\Request $request){ $payload = $request->json(); if ($payload->get('type') === 'url_verification') { return $payload->get('challenge'); } // Bot logic will be placed here }

Okay — so this assures that Slack can successfully send events to our application, which we can then use with our Bot.

There’s one more thing we need to do, which is setting up a simple OAuth flow in order to retrieve a Bot access token. We will simply use Laravel Socialite for this.

Setting up Laravel Socialite OAuth flow

Originally published at laravelfeed.com.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.