|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\SlashCommand\Handlers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Bus\Dispatcher; |
|
6
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
|
7
|
|
|
use Spatie\SlashCommand\Exceptions\SlackSlashCommandException; |
|
8
|
|
|
use Spatie\SlashCommand\HandlesSlashCommand; |
|
9
|
|
|
use Spatie\SlashCommand\Jobs\SlashCommandResponseJob; |
|
10
|
|
|
use Spatie\SlashCommand\Request; |
|
11
|
|
|
use Spatie\SlashCommand\Response; |
|
12
|
|
|
|
|
13
|
|
|
abstract class BaseHandler implements HandlesSlashCommand |
|
14
|
|
|
{ |
|
15
|
|
|
use DispatchesJobs; |
|
16
|
|
|
|
|
17
|
|
|
/** @var \Spatie\SlashCommand\Request */ |
|
18
|
|
|
protected $request; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(Request $request) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->request = $request; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function respondToSlack(string $text = ''): Response |
|
26
|
|
|
{ |
|
27
|
|
|
return Response::create($this->request)->withText($text); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
protected function dispatch(SlashCommandResponseJob $job) |
|
31
|
|
|
{ |
|
32
|
|
|
$job->setRequest($this->request); |
|
33
|
|
|
|
|
34
|
|
|
return app(Dispatcher::class)->dispatch($job); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function abort($response) |
|
38
|
|
|
{ |
|
39
|
|
|
throw new SlackSlashCommandException($response); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getRequest(): Request |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->request; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* If this function returns true, the handle method will get called. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \Spatie\SlashCommand\Request $request |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
abstract public function canHandle(Request $request): bool; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Handle the given request. Remember that Slack expects a response |
|
58
|
|
|
* within three seconds after the slash command was issued. If |
|
59
|
|
|
* there is more time needed, dispatch a job. |
|
60
|
|
|
* |
|
61
|
|
|
* @param \Spatie\SlashCommand\Request $request |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Spatie\SlashCommand\Response |
|
64
|
|
|
*/ |
|
65
|
|
|
abstract public function handle(Request $request): Response; |
|
66
|
|
|
} |
|
67
|
|
|
|