-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify_matrix.class.php
210 lines (189 loc) · 4.32 KB
/
notify_matrix.class.php
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Sending Messages to a matrix Webhook
*
* @author ViWa Invest GmbH
* @copyright (c) 2020, viwa.de
* @see https://github.com/turt2live/matrix-appservice-webhooks
*/
class notify_matrix {
/**
* instantiate the class
*
* @param array $options
* @throws Exception
* @return object
*/
function __CONSTRUCT($options = false) {
$this->_format = "plain";
$this->_displayName = "Webhook";
$this->_avatarUrl = "https://www.viwa.de/assets/img/v.png";
$this->_text = false;
$this->_webhookUrl = false;
try {
if (is_array($options)) {
if(isset($options['format']))
$this->setFormat($options['format']);
if(isset($options['displayname']))
$this->setDisplayName($options['displayname']);
if(isset($options['avatarurl']))
$this->setAvatarUrl($options['avatarurl']);
if(isset($options['text']))
$this->setText($options['text']);
if(isset($options['webhookurl']))
$this->setWebhookUrl($options['webhookurl']);
}
} catch (Exception $e) {
throw new Exception ($e->getMessage());
}
return $this;
}
/**
* sets Text
*
* @param string $text
* @return object
*/
public function setText($text) {
$this->_text = $text;
return $this;
}
/**
* sets Text
*
* @param string $format
* @throws Exception
* @return object
*/
public function setFormat($format) {
if(!in_array($format, array("html", "plain"))) throw new Exception('Format must be one of html, plain');
$this->_format = $format;
return $this;
}
/**
* sets displayname
*
* @param string $displayname
* @return object
*/
public function setDisplayName($displayname) {
$this->_displayName = $displayname;
return $this;
}
/**
* sets Avatar url
*
* @param string $url
* @throws Exception
* @return object
*/
public function setAvatarUrl($url) {
if (!filter_var($url, FILTER_VALIDATE_URL)) throw new Exception('AvatarUrl must be a URL');
$this->_avatarUrl = $url;
return $this;
}
/**
* sets WebHook url
*
* @param string $url
* @throws Exception
* @return object
*/
public function setWebhookUrl($url) {
if (!filter_var($url, FILTER_VALIDATE_URL)) throw new Exception('WebhookUrl must be a URL');
$this->_webhookUrl = $url;
return $this;
}
/**
* Gets Text
*
* @param void
* @return string
*/
public function getText() {
return $this->_text;
}
/**
* Gets Format
*
* @param void
* @return string
*/
public function getFormat() {
return $this->_format;
}
/**
* Gets displayname
*
* @param void
* @return object
*/
public function getDisplayName() {
return $this->_displayName;
}
/**
* Gets Avatar url
*
* @param void
* @return string
*/
public function getAvatarUrl() {
return $this->_avatarUrl;
}
/**
* Gets Webhook url
*
* @param void
* @return string
*/
public function getWebhookUrl() {
return $this->_webhookUrl;
}
/**
* Gets json encoded Message for http post request
*
* @param void
* @return string
*/
private function _getMessage() {
$msg['text'] = $this->getText();
$msg['format'] = $this->getFormat();
$msg['displayName'] = $this->getDisplayName();
$msg['avatarUrl'] = $this->getAvatarUrl();
$json_msg = json_encode($msg);
return $json_msg;
}
/**
* Sends request
*
* @param void
* @throws Exception
* @return bool
*/
public function send() {
if($this->getWebhookUrl()==false) throw new Exception("WebhookUrl missing", 1);
if($this->getText()==false) throw new Exception("Message missing", 1);
if(!function_exists('curl_version')) throw new Exception('PHP curl extension is missing');
/** init curl object and set options */
$ch = curl_init($this->getWebhookUrl());
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_getMessage());
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/** execute api call */
$result = curl_exec($ch);
/** get http status code of the api call and handle errors*/
if(!curl_errno($ch)) {
switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
case 200: # OK
return true;
break;
default:
throw new Exception('Unexpected curl http code: '. $http_code);
}
} else {
throw new Exception(curl_errno($ch));
}
}
}
?>