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 $icon = ''; |
24
|
|
|
|
25
|
|
|
/** @var \Illuminate\Support\Collection */ |
26
|
|
|
protected $attachments; |
27
|
|
|
|
28
|
|
|
/** @var \GuzzleHttp\Client */ |
29
|
|
|
protected $client; |
30
|
|
|
|
31
|
|
|
public static function create(Request $request): Response |
32
|
|
|
{ |
33
|
|
|
$client = app(Client::class); |
34
|
|
|
|
35
|
|
|
return new static($client, $request); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function __construct(Client $client, Request $request) |
39
|
|
|
{ |
40
|
|
|
$this->client = $client; |
41
|
|
|
|
42
|
|
|
$this->request = $request; |
43
|
|
|
|
44
|
|
|
$this->channel = $request->channelName; |
45
|
|
|
|
46
|
|
|
$this->displayResponseToUserWhoTypedCommand(); |
47
|
|
|
|
48
|
|
|
$this->attachments = new \Illuminate\Support\Collection(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $text |
53
|
|
|
* |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function withText(string $text) |
57
|
|
|
{ |
58
|
|
|
$this->text = $text; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function onChannel(string $channelName) |
64
|
|
|
{ |
65
|
|
|
$this->channel = $channelName; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function displayResponseToUserWhoTypedCommand() |
74
|
|
|
{ |
75
|
|
|
$this->responseType = 'ephemeral'; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param \Spatie\SlashCommand\Attachment $attachment |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function withAttachment(Attachment $attachment) |
86
|
|
|
{ |
87
|
|
|
$this->attachments->push($attachment); |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $multiAttachments |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function withMultiAttachments(array $multiAttachments){ |
98
|
|
|
foreach ($multiAttachments as $attachment) { |
99
|
|
|
$this->attachments->push($attachment); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $channelName |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function displayResponseToEveryoneOnChannel(string $channelName = '') |
111
|
|
|
{ |
112
|
|
|
$this->responseType = 'in_channel'; |
113
|
|
|
|
114
|
|
|
if ($channelName !== '') { |
115
|
|
|
$this->onChannel($channelName); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set the icon (either URL or emoji) we will post as. |
123
|
|
|
* |
124
|
|
|
* @param string $icon |
125
|
|
|
* |
126
|
|
|
* @return this |
127
|
|
|
*/ |
128
|
|
|
public function useIcon(string $icon) |
129
|
|
|
{ |
130
|
|
|
$this->icon = $icon; |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getIconType(): string |
136
|
|
|
{ |
137
|
|
|
if (empty($this->icon)) { |
138
|
|
|
return ''; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (starts_with($this->icon, ':') && ends_with($this->icon, ':')) { |
142
|
|
|
return 'icon_emoji'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return 'icon_url'; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Send the response to Slack. |
150
|
|
|
*/ |
151
|
|
|
public function send() |
152
|
|
|
{ |
153
|
|
|
$this->client->post($this->request->responseUrl, ['json' => $this->getPayload()]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getIlluminateResponse(): IlluminateResponse |
157
|
|
|
{ |
158
|
|
|
return new IlluminateResponse($this->getPayload()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
protected function getPayload(): array |
162
|
|
|
{ |
163
|
|
|
$payload = [ |
164
|
|
|
'text' => $this->text, |
165
|
|
|
'channel' => $this->channel, |
166
|
|
|
'link_names' => true, |
167
|
|
|
'unfurl_links' => true, |
168
|
|
|
'unfurl_media' => true, |
169
|
|
|
'mrkdwn' => true, |
170
|
|
|
'response_type' => $this->responseType, |
171
|
|
|
'attachments' => $this->attachments->map(function (Attachment $attachment) { |
172
|
|
|
return $attachment->toArray(); |
173
|
|
|
})->toArray(), |
174
|
|
|
]; |
175
|
|
|
|
176
|
|
|
if (! empty($this->icon)) { |
177
|
|
|
$payload[$this->getIconType()] = $this->icon; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $payload; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|