-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendMessageToSlack.php
More file actions
73 lines (59 loc) · 1.95 KB
/
SendMessageToSlack.php
File metadata and controls
73 lines (59 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* user: Timo Stankowitz
* mail: timo.stankowitz@gmail.com
* Date: 03.04.16
* Time: 21:12
*
* make sure php5-curl is installed on your system
*
*/
class SendMessageToSlack {
private $curlObject;
private static $url = 'https://hooks.slack.com/services/XXXX/YYYYY/ZZZ'; // replace with your url
private $message;
private $color;
private $payload;
/**
* SendMessageToSlack constructor.
* @param string $color string color of the notification
* @param $message string what message should be send.
*/
public function __construct($color = 'CCCCCC', $message) {
// create new curl object
$this->curlObject = curl_init(self::$url);
$this->color = $color;
$this->message = $message;
$this->buildPayload();
$this->execute();
}
private function buildPayload() {
$this->payload = [
"channel" => "XXXX", // name of the channel
"username" => "XXX", // name of the username
"attachments" => [
[
"fallback" => $this->message,
"color" => $this->color,
"text" => $this->message
]
]
];
}
private function execute() {
// convert to json
$enc = json_encode($this->payload);
// curl stuff
curl_setopt($this->curlObject, CURLOPT_POST, 1);
curl_setopt($this->curlObject, CURLOPT_POSTFIELDS, $enc);
curl_setopt($this->curlObject, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($this->curlObject, CURLOPT_VERBOSE, 0); // don't show output on the screen
curl_setopt($this->curlObject, CURLOPT_RETURNTRANSFER, true);
// and send !
$result = curl_exec($this->curlObject);
if($result != 'ok') {
// curl error
error_log("curl error: " . curl_error($this->curlObject));
}
}
}