1
|
|
|
<?php |
2
|
|
|
namespace PortlandLabs\Slackbot\Slack\Api\Payload; |
3
|
|
|
|
4
|
|
|
use CL\Slack\Payload\ChatPostMessagePayload; |
5
|
|
|
use CL\Slack\Payload\ChatPostMessagePayloadResponse; |
6
|
|
|
use CL\Slack\Payload\PayloadInterface; |
7
|
|
|
use CL\Slack\Payload\PayloadResponseInterface; |
8
|
|
|
use PortlandLabs\Slackbot\Slack\Api\Client; |
9
|
|
|
|
10
|
|
|
class Builder |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** @var ChatPostMessagePayload|ChatUpdatePayload */ |
14
|
|
|
protected $payload; |
15
|
|
|
|
16
|
|
|
/** @var ChatPostMessagePayload */ |
17
|
|
|
protected $lastPayload; |
18
|
|
|
|
19
|
|
|
/** @var ChatPostMessagePayloadResponse */ |
20
|
|
|
protected $lastResponse; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $username; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Prepare to start building |
27
|
|
|
* |
28
|
|
|
* @param string $username |
29
|
|
|
* |
30
|
|
|
* @return Builder |
31
|
|
|
* @internal |
32
|
|
|
*/ |
33
|
|
|
public function prepare(string $username): Builder |
34
|
|
|
{ |
35
|
|
|
$this->username = $username; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new ChatPostMessagePayload builder flow |
42
|
|
|
* |
43
|
|
|
* @param string $text |
44
|
|
|
* |
45
|
|
|
* @return Builder |
46
|
|
|
*/ |
47
|
|
|
public function send($text = ''): Builder |
48
|
|
|
{ |
49
|
|
|
$this->payload = new ChatPostMessagePayload(); |
50
|
|
|
$this->payload->setText($text); |
51
|
|
|
$this->payload->setUsername($this->username); |
52
|
|
|
$this->lastResponse = null; |
53
|
|
|
$this->lastPayload = null; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Create a new ChatUpdatePayload builder flow |
60
|
|
|
* |
61
|
|
|
* @return Builder |
62
|
|
|
*/ |
63
|
|
|
public function update(): Builder |
64
|
|
|
{ |
65
|
|
|
if (!$this->lastPayload || !$this->lastResponse) { |
66
|
|
|
throw new \RuntimeException('No chat post to update.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$response = $this->lastResponse; |
70
|
|
|
$message = $this->lastPayload; |
71
|
|
|
|
72
|
|
|
$payload = new ChatUpdatePayload(); |
73
|
|
|
$payload->setSlackTimestamp($response->getSlackTimestamp()); |
74
|
|
|
$payload->setChannelId($response->getChannelId()); |
75
|
|
|
$payload->setText($message->getText()); |
76
|
|
|
$payload->setLinkNames($message->getLinkNames()); |
77
|
|
|
$payload->setParse($message->getParse()); |
78
|
|
|
|
79
|
|
|
// Set the new payload on the new builder |
80
|
|
|
$this->payload = $payload; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function updateRtmMessage(array $responseData): Builder |
86
|
|
|
{ |
87
|
|
|
$payload = new ChatUpdatePayload(); |
88
|
|
|
$payload->setSlackTimestamp(array_get($responseData, 'timestamp')); |
|
|
|
|
89
|
|
|
$payload->setChannelId(array_get($responseData, 'channel')); |
|
|
|
|
90
|
|
|
$payload->setText(array_get($responseData, 'text')); |
|
|
|
|
91
|
|
|
|
92
|
|
|
// Set the new payload on the new builder |
93
|
|
|
$this->payload = $payload; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set the channel to send to |
100
|
|
|
* Note: Only works when sending new messages, not when updating |
101
|
|
|
* |
102
|
|
|
* @param $channel |
103
|
|
|
* |
104
|
|
|
* @return Builder |
105
|
|
|
*/ |
106
|
|
|
public function to($channel): Builder |
107
|
|
|
{ |
108
|
|
|
if (!$this->payload instanceof ChatPostMessagePayload) { |
109
|
|
|
throw new \RuntimeException('You can\'t update the channel, use ->send to create a new message instead first'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->payload->setChannel($channel); |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set the icon on the active builder |
118
|
|
|
* Note: Only works when sending new messages, not when updating |
119
|
|
|
* |
120
|
|
|
* @param string $icon |
121
|
|
|
* @return Builder |
122
|
|
|
*/ |
123
|
|
|
public function withIcon(string $icon): Builder |
124
|
|
|
{ |
125
|
|
|
if (!$this->payload instanceof ChatPostMessagePayload) { |
126
|
|
|
throw new \RuntimeException('You can\'t update the icon'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->payload->setIconEmoji($icon); |
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set the icon on the active builder |
135
|
|
|
* Note: Only works when sending new messages, not when updating |
136
|
|
|
* |
137
|
|
|
* @param string $iconUrl |
138
|
|
|
* @return Builder |
139
|
|
|
*/ |
140
|
|
|
public function withIconUrl(string $iconUrl): Builder |
141
|
|
|
{ |
142
|
|
|
if (!$this->payload instanceof ChatPostMessagePayload) { |
143
|
|
|
throw new \RuntimeException('You can\'t update the icon'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->payload->setIconUrl($iconUrl); |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Set the text on the active payload |
152
|
|
|
* |
153
|
|
|
* @param string $text |
154
|
|
|
* @return Builder |
155
|
|
|
*/ |
156
|
|
|
public function withText(string $text): Builder |
157
|
|
|
{ |
158
|
|
|
$this->payload->setText($text); |
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Add an attachment to the active payload |
164
|
|
|
* |
165
|
|
|
* @param AttachmentPayload $attachment |
166
|
|
|
* @return Builder |
167
|
|
|
*/ |
168
|
|
|
public function withAttachment(AttachmentPayload $attachment): Builder |
169
|
|
|
{ |
170
|
|
|
$this->payload->addAttachment($attachment); |
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Execute the active payload on the client |
176
|
|
|
* |
177
|
|
|
* @param Client $client |
178
|
|
|
* |
179
|
|
|
* @return PayloadResponseInterface |
180
|
|
|
* |
181
|
|
|
* @throws \CL\Slack\Exception\SlackException |
182
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
183
|
|
|
*/ |
184
|
|
|
public function execute(Client $client): PayloadResponseInterface |
185
|
|
|
{ |
186
|
|
|
$result = $client->send($this->payload); |
187
|
|
|
|
188
|
|
|
if ($result instanceof ChatPostMessagePayloadResponse) { |
189
|
|
|
$this->lastPayload = clone $this->payload; |
|
|
|
|
190
|
|
|
$this->lastResponse = $result; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $result; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get the active payload |
198
|
|
|
* |
199
|
|
|
* @return ChatPostMessagePayload|ChatUpdatePayload |
200
|
|
|
*/ |
201
|
|
|
public function payload(): PayloadInterface |
202
|
|
|
{ |
203
|
|
|
return $this->payload; |
204
|
|
|
} |
205
|
|
|
} |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.