-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamElements.php
365 lines (320 loc) · 7.41 KB
/
StreamElements.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
/**
* Created by PhpStorm.
* User: Lx
* Date: 19.06.2018
* Time: 18:00
*/
class StreamElements
{
/**
* URL to SE API
* @var string
*/
protected $url = 'https://api.streamelements.com/kappa/v2';
/**
* oAuth2 handler
* @var string
*/
protected $provider;
/**
* Auth type
* @var string
*/
protected $auth;
/**
* Token
* @var string
*/
protected $token;
/**
* API limits
* @var string
*/
protected $apiLimits;
/**
* CURL Options array
* @var array
*/
protected $options;
/**
* @var \GuzzleHttp\Client
*/
protected $api;
/**
*
* @var string
*/
public $channelName;
/**
* Profile information received on handshake
* @var object
*/
public $profile;
/**
* Channel id received on handshake
* @var int
*/
public $channelId;
/**
* bot data
* @var array
*/
public $botInfo;
/**
* StreamElements constructor.
* @param $token - OAuth/JWT Token
* @param $auth - Authorization method (Bearer,OAuth)
* @return boolean
* @throws Exception
*/
public function __construct($token, $auth)
{
$this->api = new GuzzleHttp\Client();
$this->auth = $auth;
$this->options = array(
"headers" => array(
"Accept" => "application/json",
"Authorization" => $auth . " " . $token
),
"debug" => false,
"verify" => false);
if (!$this->getInfo()) {
throw new Exception("Invalid credentials");
}
}
/**
*
*/
public function __destruct()
{}
/**
* @param $method
* @param $endpoint
* @param array $params
* @param bool $isUpload
* @return bool|mixed
*/
protected function sendRequest($method, $endpoint, $params = array(), $isUpload = false)
{
if (!is_null($this->apiLimits) && is_array($this->apiLimits['global']))
{
if ($this->apiLimits['global']['reset'] > new DateTime() && !$this->apiLimits['global']['remaining']) return false;
}
$url = $this->url . '/' . $endpoint;
$options = $this->options;
if (is_array($params))
{
$options['json'] = $params;
}
if ($isUpload)
{
$options['multipart'][] = $params;
unset($options['json']);
}
$res = array();
try
{
if ($method == 'GET')
{
$res = $this->api->get($url, $this->options);
}
else if ($method == 'POST')
{
$res = $this->api->post($url, $options);
}
else if ($method == 'PUT')
{
$res = $this->api->put($url, $options);
}
else if ($method == 'DELETE')
{
$res = $this->api->delete($url, $options);
}
$headers = $res->getHeaders();
$scope = $headers['x-ratelimit-bucket'][0];
$this->apiLimits[$scope] = [
'limit' => $headers['x-ratelimit-limit'][0],
'remaining' => $headers['x-ratelimit-remaining'][0],
'reset' => new DateTime()
];
$this->apiLimits[$scope]['reset']->setTimestamp((int)($headers['x-ratelimit-reset'][0] / 1000));
}
catch (GuzzleHttp\Exception\ClientException $e)
{
if ($e->hasResponse())
{
return (json_decode($e->getResponse()->getBody()->getContents(), 1));
}
return false;
}
//var_dump($res);
//die();
$response = json_decode($res->getBody()->getContents(), true);
return $response;
}
/**
* @return bool
*/
public function getInfo()
{
$endpoint = 'channels/me';
try
{
$res = $this->sendRequest('GET', $endpoint);
}
catch (Exception $e)
{
echo($e->getMessage());
$this->__destruct();
return false;
}
$this->profile = $res;
if (!isset($this->profile["username"]) || !isset($this->profile["_id"])) return false;
$this->channelName = $this->profile["username"];
$this->channelId = $this->profile["_id"];
return true;
}
// BOT
/**
* Gather bot information
* @return bool|mixed
*/
public function botInit()
{
$url = 'bot/' . $this->channelId;
$res = $this->sendRequest('GET', $url);
$this->botInfo = $res['bot'];
return $res;
}
/**
* Sends a message to channel as bot
* @param $message
* @return bool|mixed
*/
public function botSay($message)
{
if (!$this->botInfo) $this->botInit();
if (!$this->botInfo['joined']) return false;
if ($this->botInfo['muted']) return false;
$url = 'bot/' . $this->channelId . '/say';
$params = array('message' => $message);
$res = $this->sendRequest('POST', $url, $params);
return $res;
}
/**
* List all timers
* @return bool|mixed
*/
public function listTimers()
{
$url = 'bot/timers/' . $this->channelId;
$res = $this->sendRequest('GET', $url);
return $res;
}
/**
* Get timer based on provided id
* @param string $timerId
* @return bool|mixed
*/
public function getTimer($timerId)
{
$url = 'bot/timers/' . $this->channelId . '/' . $timerId;
$res = $this->sendRequest('GET', $url);
return $res;
}
/**
* Create new timer
* @param string $name
* @param array $messages
* @param string $chatLines
* @param bool $online
* @param int $onlineInt
* @param bool $offline
* @param int $onlineInt
* @return bool|mixed
*/
public function createTimer($name, $messages, $chatLines, $online, $onlineInt, $offline, $offlineInt)
{
$url = 'bot/timers/' . $this->channelId;
$timer = array(
"online" => array(
"enabled" => $online,
"interval" => $onlineInt
),
"offline" => array(
"enabled" => $offline,
"interval" => $offlineInt
),
"enabled" => true,
"chatLines" => $chatLines,
"messages" => $messages,
"name" => $name
);
$res = $this->sendRequest('POST', $url, $timer);
return $res;
}
/**
* Update timer
* @param string $timerId
* @param string $name
* @param array $messages
* @param string $chatLines
* @param bool $enabled
* @param bool $online
* @param int $onlineInt
* @param bool $offline
* @param int $onlineInt
* @return bool|mixed
*/
public function updateTimer($timerId, $name, $messages, $chatLines, $enabled, $online, $onlineInt, $offline, $offlineInt)
{
$url = 'bot/timers/' . $this->channelId . '/' . $timerId;
$timer = array(
"online" => array(
"enabled" => $online,
"interval" => $onlineInt
),
"offline" => array(
"enabled" => $offline,
"interval" => $offlineInt
),
"enabled" => $enabled,
"chatLines" => $chatLines,
"messages" => $messages,
"name" => $name
);
$res = $this->sendRequest('PUT', $url, $timer);
return $res;
}
/**
* @param string $timerId
* @return bool|mixed
*/
public function deleteTimer($timerId)
{
$url = 'bot/timers/' . $this->channelId . '/' . $timerId;
$res = $this->sendRequest('DELETE', $url);
return $res;
}
/**
* @return bool|mixed
*/
public function listCommands()
{
$url = 'bot/commands/' . $this->channelId;
$res = $this->sendRequest('GET', $url);
return $res;
}
/**
* @param string $commandId
* @return bool|mixed
*/
public function getCommand($commandId)
{
$url = 'bot/commands/' . $this->channelId . '/' . $commandId;
$res = $this->sendRequest('GET', $url);
return $res;
}
}