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