1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SlashCommand; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Illuminate\Http\Response; |
7
|
|
|
|
8
|
|
|
class SlashCommandResponse |
9
|
|
|
{ |
10
|
|
|
/** @var \Spatie\SlashCommand\SlashCommandData */ |
11
|
|
|
protected $slashCommandData; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
protected $text; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $responseType = 'ephemeral'; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $attachments = ''; |
21
|
|
|
|
22
|
|
|
/** @var \GuzzleHttp\Client */ |
23
|
|
|
protected $client; |
24
|
|
|
|
25
|
|
|
public static function create(SlashCommandData $slashCommandData): SlashCommandResponse |
26
|
|
|
{ |
27
|
|
|
$client = app(Client::class); |
28
|
|
|
|
29
|
|
|
return (new static($client, $slashCommandData)) |
30
|
|
|
->displayResponseOnlyToUserWhoTypedCommand(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function __construct(Client $client, SlashCommandData $slashCommandData) |
34
|
|
|
{ |
35
|
|
|
$this->client = $client; |
36
|
|
|
|
37
|
|
|
$this->slashCommandData = $slashCommandData; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $text |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
|
|
public function setText(string $text) |
45
|
|
|
{ |
46
|
|
|
$this->text = $text; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function displayResponseOnlyToUserWhoTypedCommand() |
55
|
|
|
{ |
56
|
|
|
$this->responseType = 'ephemeral'; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function displayResponseToEveryoneOnChannel() |
65
|
|
|
{ |
66
|
|
|
$this->responseType = 'in_channel'; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Send the response to Slack |
73
|
|
|
*/ |
74
|
|
|
public function send() |
75
|
|
|
{ |
76
|
|
|
$this->client->post($this->slashCommandData->reponseUrl, ['json' => $this->getPayload()]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/* |
80
|
|
|
* Get the http response |
81
|
|
|
*/ |
82
|
|
|
public function getHttpResponse(): Response |
83
|
|
|
{ |
84
|
|
|
return new Response($this->getPayload()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function getPayload(): array |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
'text' => $this->text, |
91
|
|
|
'link_names' => true, |
92
|
|
|
'unfurl_links' => true, |
93
|
|
|
'unfurl_media' => true, |
94
|
|
|
'mrkdwn' => true, |
95
|
|
|
'response_type' => $this->responseType, |
96
|
|
|
'attachments' => $this->attachments, |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|