Slack has exploded in popularity in the last year or so with thousands of companies adopting it as the central communication tool for their team or organisation.

Slack is basically just modern IRC with a better user interface, but Slack has also built a way to allow all types of services to integrate and push data into it as a central information hub.

This is really useful for using third-party services that your company already uses.

But it also makes it very easy to build your own internal integrations for your own application.

For example, instead of adding a page to an internal admin application, you can simply add a Slack slash command that can be used in Slack.

This is really handy for requests that simply need to return a chunk of data from time to time.

In today’s tutorial we’re going to be looking at setting up Slack slash commands in your Laravel application.

Setting up the Slash Command

The first thing we need to do is to set up the Slash Command in Slack.

If you go to your companies integrations and then choose “Slash Commands” you should be presented with a walkthrough for creating a new command.

The important bits are choosing the URL and the token as we will need those two bits of information to build the integration on the Laravel side.

Adding the route

The first thing I’m going to do is to define the route for Slack commands:

use Illuminate\Contracts\Routing\Registrar;

class SlackRoutes
{
    /**
     * Define the routes
     *
     * @param Registrar $router
     * @return void
     */
    public function map(Registrar $router)
    {
        $router->post('slack/{command}', [
            'as'   => 'slack.commands',
            'uses' => 'SlackController@commands'
        ]);
    }
}