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