|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Slackify. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Strime <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Strime\Slackify\Webhooks; |
|
13
|
|
|
|
|
14
|
|
|
use Strime\Slackify\Exception\RuntimeException; |
|
15
|
|
|
use Strime\Slackify\Exception\InvalidArgumentException; |
|
16
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
17
|
|
|
|
|
18
|
|
|
class Webhook extends AbstractWebhook |
|
19
|
|
|
{ |
|
20
|
|
|
const SLACK_VALID_VALUES = "message,channel,link,link_text,icon,username"; |
|
21
|
|
|
const SLACK_VALID_ATTACHMENTS = "fallback,text,pretext,color,fields,author,author_link,title,title_link,image_url,thumb_url,footer,footer_icon,ts"; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
protected $url; |
|
26
|
|
|
/** @var array */ |
|
27
|
|
|
protected $attachments; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
public function __construct($url) { |
|
31
|
|
|
$this->url = $url; |
|
32
|
|
|
$this->attachments = array(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getUrl() { |
|
41
|
|
|
return $this->url; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $url |
|
46
|
|
|
* |
|
47
|
|
|
* @return Webhook |
|
48
|
|
|
*/ |
|
49
|
|
|
public function setUrl($url) { |
|
50
|
|
|
|
|
51
|
|
|
if (!is_string($url)) { |
|
52
|
|
|
throw new InvalidArgumentException('The URL must be a string.'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->url = $url; |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $values |
|
65
|
|
|
* @return Webhook |
|
66
|
|
|
*/ |
|
67
|
|
|
public function sendMessage($values) { |
|
68
|
|
|
|
|
69
|
|
|
// Parse the values to make sure they are valid |
|
70
|
|
|
foreach ($values as $key => $value) { |
|
71
|
|
|
|
|
72
|
|
|
// Create an array with the valid values |
|
73
|
|
|
$valid_values = explode(",", self::SLACK_VALID_VALUES); |
|
74
|
|
|
|
|
75
|
|
|
if (!in_array($key, $valid_values)) { |
|
76
|
|
|
unset($values[$key]); |
|
77
|
|
|
} |
|
78
|
|
|
else { |
|
79
|
|
|
if (!is_string($value)) { |
|
80
|
|
|
throw new InvalidArgumentException("The " . $key . " value must be a string."); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// Check if a message has been defined |
|
86
|
|
|
if (!isset($values["message"])) { |
|
87
|
|
|
throw new InvalidArgumentException("The message value is missing."); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Encode the message |
|
91
|
|
|
$values["message"] = str_replace('&', '&', $values["message"]); |
|
92
|
|
|
$values["message"] = str_replace(' < ', ' < ', $values["message"]); |
|
93
|
|
|
$values["message"] = str_replace(' > ', ' > ', $values["message"]); |
|
94
|
|
|
|
|
95
|
|
|
// Add the link to the message if needed. |
|
96
|
|
|
if (isset($values["link"]) && ($values["link"] !== NULL)) { |
|
97
|
|
|
$values["message"] .= "\n"; |
|
98
|
|
|
$values["message"] .= "<" . $values["link"]; |
|
99
|
|
|
if (isset($values["link_text"]) && ($values["link_text"] !== NULL)) { |
|
100
|
|
|
$values["message"] .= "|" . $values["link_text"]; |
|
101
|
|
|
} |
|
102
|
|
|
$values["message"] .= ">"; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// Set the parameters for the request |
|
106
|
|
|
$params = array( |
|
107
|
|
|
"text" => $values["message"] |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
if (isset($values["channel"]) && ($values["channel"] !== NULL)) { |
|
111
|
|
|
$params["channel"] = $values["channel"]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (isset($values["icon"]) && ($values["icon"] !== NULL)) { |
|
115
|
|
|
|
|
116
|
|
|
// The icon may either be a URL or an emoji |
|
117
|
|
|
// We need to check the form of the string to know which parameter to choose. |
|
118
|
|
|
$icon_type = NULL; |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
if ((strcmp(substr($values["icon"], 0, 1), ":") == 0) && (strcmp(substr($values["icon"], -1), ":") == 0)) { |
|
121
|
|
|
$icon_type = "emoji"; |
|
122
|
|
|
} |
|
123
|
|
|
else { |
|
124
|
|
|
$icon_type = "url"; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if ($icon_type != NULL) |
|
128
|
|
|
$params["icon_" . $icon_type] = $values["icon"]; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (isset($values["username"]) && ($values["username"] !== NULL)) { |
|
132
|
|
|
$params["username"] = $values["username"]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// Check if there are attachments to add to the request |
|
136
|
|
|
if (is_array($this->attachments) && (count($this->attachments) > 0)) { |
|
137
|
|
|
$params["attachments"] = $this->attachments; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// Send the request |
|
141
|
|
|
try { |
|
142
|
|
|
$client = new \GuzzleHttp\Client(); |
|
143
|
|
|
$json_response = $client->request('POST', $this->url, [ |
|
|
|
|
|
|
144
|
|
|
'body' => json_encode($params) |
|
145
|
|
|
]); |
|
146
|
|
|
} |
|
147
|
|
|
catch (RequestException $e) { |
|
148
|
|
|
throw new RuntimeException('The request to the webhook failed: '.$e->getMessage(), $e->getCode(), $e); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// We re-initialize the attachments to prevent any conflict with a future message. |
|
152
|
|
|
$this->attachments = array(); |
|
153
|
|
|
|
|
154
|
|
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* {@inheritdoc} |
|
161
|
|
|
* |
|
162
|
|
|
* @param array $attachments |
|
163
|
|
|
* @return Webhook |
|
164
|
|
|
*/ |
|
165
|
|
|
public function setAttachments($attachments) |
|
166
|
|
|
{ |
|
167
|
|
|
if (!is_array($attachments)) { |
|
168
|
|
|
throw new InvalidArgumentException('The attachments must be an array.'); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// Browse the attachments to see if the key is valid. |
|
172
|
|
|
foreach ($attachments as $attachment) { |
|
173
|
|
|
foreach ($attachment as $key => $value) { |
|
174
|
|
|
|
|
175
|
|
|
// Create an array with the valid values |
|
176
|
|
|
$valid_values = explode(",", self::SLACK_VALID_ATTACHMENTS); |
|
177
|
|
|
|
|
178
|
|
|
if (!in_array($key, $valid_values)) { |
|
179
|
|
|
unset($attachment[$key]); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// Check if a message has been defined |
|
184
|
|
|
if (!isset($attachment["fallback"], $attachment["fields"], $attachment["fields"]["title"])) { |
|
185
|
|
|
throw new InvalidArgumentException("Attachment fields are missing."); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$this->attachments = $attachments; |
|
190
|
|
|
|
|
191
|
|
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.