Sending a basic response | laravel-slack-slash-command | Spatie

 SPATIE

  Laravel Slack Slash Command
==============================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-slack-slash-command](https://spatie.be/docs/laravel-slack-slash-command/v1)  Usage  Sending a basic response

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/laravel-slack-slash-command/v1)

  Sending a basic response
- [ Introduction ](https://spatie.be/docs/laravel-slack-slash-command/v1/introduction)
- [ Postcardware ](https://spatie.be/docs/laravel-slack-slash-command/v1/postcardware)
- [ Requirements ](https://spatie.be/docs/laravel-slack-slash-command/v1/requirements)
- [ Installation and Setup ](https://spatie.be/docs/laravel-slack-slash-command/v1/installation-and-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-slack-slash-command/v1/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/laravel-slack-slash-command/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-slack-slash-command/v1/about-us)

Usage
-----

- [ General flow ](https://spatie.be/docs/laravel-slack-slash-command/v1/usage/general-flow)
- [ Sending a basic response ](https://spatie.be/docs/laravel-slack-slash-command/v1/usage/sending-a-basic-response)
- [ Making your response look good ](https://spatie.be/docs/laravel-slack-slash-command/v1/usage/making-your-response-look-good)
- [ Making your attachments interactive ](https://spatie.be/docs/laravel-slack-slash-command/v1/usage/making-your-attachments-interactive)

Advanced usage
--------------

- [ Sending delayed responses ](https://spatie.be/docs/laravel-slack-slash-command/v1/advanced-usage/sending-delayed-responses)
- [ Using signature handlers ](https://spatie.be/docs/laravel-slack-slash-command/v1/advanced-usage/using-signature-handlers)
- [ Responding to multiple commands ](https://spatie.be/docs/laravel-slack-slash-command/v1/advanced-usage/responding-to-multiple-commands)

 Sending a basic response
========================

###  On this page

1. [ The request object ](#content-the-request-object)
2. [ Visibility of the response ](#content-visibility-of-the-response)

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](/laravel-slack-slash-command/v1/advanced-usage/sending-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 \Spatie\SlashCommand\Request $request
     *
     * @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 \Spatie\SlashCommand\Request $request
     *
     * @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](https://docs.spatie.be/laravel-slack-slash-command/v1/usage/making-your-response-look-good) 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();
    }
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
