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 $channel; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $attachments = ''; |
24
|
|
|
|
25
|
|
|
/** @var \GuzzleHttp\Client */ |
26
|
|
|
protected $client; |
27
|
|
|
|
28
|
|
|
public static function create(SlashCommandData $slashCommandData): SlashCommandResponse |
29
|
|
|
{ |
30
|
|
|
$client = app(Client::class); |
31
|
|
|
|
32
|
|
|
return (new static($client, $slashCommandData)) |
33
|
|
|
->displayResponseOnlyToUserWhoTypedCommand(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function __construct(Client $client, SlashCommandData $slashCommandData) |
37
|
|
|
{ |
38
|
|
|
$this->client = $client; |
39
|
|
|
|
40
|
|
|
$this->slashCommandData = $slashCommandData; |
41
|
|
|
|
42
|
|
|
$this->channel = $slashCommandData->channelName; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $text |
47
|
|
|
* |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
|
|
public function setText(string $text) |
51
|
|
|
{ |
52
|
|
|
$this->text = $text; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function onChannel(string $channelName) |
58
|
|
|
{ |
59
|
|
|
$this->channel = $channelName; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function displayResponseOnlyToUserWhoTypedCommand() |
68
|
|
|
{ |
69
|
|
|
$this->responseType = 'ephemeral'; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function displayResponseToEveryoneOnChannel() |
78
|
|
|
{ |
79
|
|
|
$this->responseType = 'in_channel'; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Send the response to Slack. |
86
|
|
|
*/ |
87
|
|
|
public function send() |
88
|
|
|
{ |
89
|
|
|
$this->client->post($this->slashCommandData->responseUrl, ['json' => $this->getPayload()]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* |
93
|
|
|
* Get the http response |
94
|
|
|
*/ |
95
|
|
|
public function getHttpResponse(): Response |
96
|
|
|
{ |
97
|
|
|
return new Response($this->getPayload()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function getPayload(): array |
101
|
|
|
{ |
102
|
|
|
return [ |
103
|
|
|
'text' => $this->text, |
104
|
|
|
'channel' => $this->channel, |
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
|
|
|
|