Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Slack Slash Command Laravel Package

spatie/laravel-slack-slash-command

Build Slack slash commands in Laravel. Define handlers to validate and process incoming Slack requests, reply within 3 seconds or dispatch jobs for longer work, and send structured responses back to Slack. Includes request/response helpers and simple routing of commands.

View on GitHub
Deep Wiki
Context7

title: Sending a basic response weight: 2

Whenever a user types in a slash command Slack will send an http request to the Laravel app. Keep in mind that you have only 3 seconds to respond. If handling the request takes longer than that you should use delayed responses.

Whenever a request from slack hits the Laravel app the package will go over all classes in the handlers key of the config file to see which one should send a response. Let's review how you can create your own handler.

Here's a simple example:

namespace App\SlashCommandHandlers;

use Spatie\SlashCommand\Request;
use Spatie\SlashCommand\Response;
use Spatie\SlashCommand\Handlers\BaseHandler;

class Hodor extends BaseHandler
{
    /**
     * If this function returns true, the handle method will get called.
     *
     * [@param](https://github.com/param) \Spatie\SlashCommand\Request $request
     *
     * [@return](https://github.com/return) bool
     */
    public function canHandle(Request $request): bool
    {
        return true;
    }

    /**
     * Handle the given request. Remember that Slack expects a response
     * within three seconds after the slash command was issued. If
     * there is more time needed, dispatch a job.
     * 
     * [@param](https://github.com/param) \Spatie\SlashCommand\Request $request
     * 
     * [@return](https://github.com/return) \Spatie\SlashCommand\Response
     */
    public function handle(Request $request): Response
    {
        return $this->respondToSlack("Hodor, hodor...");
    }
}

That handler must be registered in the config file.

// app/config/laravel-slack-slash-command
    'handlers' => [
        App\SlashCommandHandlers\Hodor::class,
        ...
    ], 

Now whenever you type in your slash command, you'll see that it'll response with Hodor, hodor....

Let's create a slightly more advanced command.

namespace App\SlashCommandHandlers;

use Spatie\SlashCommand\Request;
use Spatie\SlashCommand\Response;
use Spatie\SlashCommand\Handlers\BaseHandler;

class Repeat extends BaseHandler
{
    public function canHandle(Request $request): bool
    {
        return starts_with($request->text, 'repeat');
    }

    public function handle(Request $request): Response
    {   
        $textWithoutRepeat = substr($request->text, 7)
        
        return $this->respondToSlack("You said {$textWithoutRepeat}");
    }
}

Let's register this handler as well.

// app/config/laravel-slack-slash-command

    'handlers' => [
        App\SlashCommandHandlers\Repeat::class,
        App\SlashCommandHandlers\Hodor::class,
        ...
    ],    

If you type in /your-command repeat Hi, everybody in a slack channel now, you'll get a response Hi, everybody back. When you type in /your-command this does not exists you'll get a response Hodor, hodor... because the Hodor handler is the first one which canHandle-method returns true.

The request object

Notice that Spatie\SlashCommand\Request being past in canHandle and handle in the handler of the previous example? It contains all data that's being passed by Slack to our Laravel app. These are it's most important properties:

  • command: the command name without the / that the user typed in. In our previous example this would be your-command.
  • text: all text text after the command. In our the example above this would be repeat Hi, everybody.
  • userName: the Slack username of the person that typed in the command
  • userId: the Slack user id of the person that typed in the command
  • channelName: the name of the channel where the user typed in the command
  • teamDomain: the name of the Slack subdomain. So if your team is on example.slack.com this would be example.

Visibility of the response

By default the response will be sent to the user who typed in the original message. If you want the response to be visible to all users in the channel you can do this:

    public function handle(Request $request): Response
    {
        return $this
           ->respondToSlack("Hodor, hodor...")
           ->displayResponseToEveryoneOnChannel();
    }

There are a lot of ways to format your message. Take a look at the docs on formatting responses to learn more.

You can also avoid sending a response in text directly. This can be useful if you want to interact with Slack API directly or have another way of providing feedback to users. Do remember to give feedback to users.

    public function handle(Request $request): Response
    {
        return $this->acknowledgeToSlack();
    }
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport