Sending delayed responses | 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)  Advanced-usage  Sending delayed responses

 Version   v1

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

  Sending delayed responses
- [ 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 delayed responses
=========================

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. After that initial fast response you're allowed to send 5 more responses in the next 30 minutes for the command. Let's see how that works.

Imagine you need to call a slow API to get a response for a slash command. Let's first create a handler that will send the initial fast response.

```
namespace App\SlashCommandHandlers;

use Spatie\SlashCommand\Request;
use Spatie\SlashCommand\Response;

class SlowApi 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 starts_with($request->text, 'give me the info');
    }

    /**
     * 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
    {
        $this->dispatch(new SlowApiJob());

        return $this->respondToSlack("Looking that up for you...");
    }
}
```

Notice that we're dispatching a job right before sending a response. Behind the scenes Laravel will queue that job (make sure you've set up a queue driver other than `sync`).

This is how that `SlowApiJob` would look like.

```
namespace App\SlashCommand\Jobs;

use Spatie\SlashCommand\Jobs\SlashCommandResponseJob;

class SlowApiJobJob extends SlashCommandResponseJob
{
    // notice here that Laravel will automatically inject dependencies here
    public function handle(MyApi $myApi)
    {
        $response = $myApi->fetchResponse();

        $this
           ->respondToSlack("Here is your response: {$response}")
           ->send();
    }
}
```

When a user types in `/your-command get me the info` a quick response `Looking that info for you...` will be displayed. After a little while, when `MyApi` has done it's job `Here is your response: ...` will be sent to the channel.

Notice that, unlike in the `Handlers` you'll need to call `send()` after the `respondToSlack`-method. You may send up to five responses in a timespan of 30 minutes in this job.

You can also avoid sending a response in the handler and rely on responses send in the job only. Do remember to give early feedback to users.

```
    public function handle(Request $request): Response
    {
        $this->dispatch(new ComplexJob());

        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)
