1 | <?php |
||
2 | /** |
||
3 | * Root class file for all api wrappers. |
||
4 | * |
||
5 | * @package LivePersonInc\LiveEngageLaravel |
||
6 | * |
||
7 | */ |
||
8 | |||
9 | namespace LivePersonInc\LiveEngageLaravel; |
||
10 | |||
11 | use Carbon\Carbon; |
||
12 | use GuzzleHttp\Client; |
||
13 | use GuzzleHttp\HandlerStack; |
||
14 | use GuzzleHttp\Subscriber\Oauth\Oauth1; |
||
15 | use LivePersonInc\LiveEngageLaravel\Models\Info; |
||
16 | use LivePersonInc\LiveEngageLaravel\Models\MetaData; |
||
17 | use LivePersonInc\LiveEngageLaravel\Models\AccountStatus; |
||
18 | use LivePersonInc\LiveEngageLaravel\Models\MessagingInfo; |
||
19 | use LivePersonInc\LiveEngageLaravel\Models\Payload; |
||
20 | use LivePersonInc\LiveEngageLaravel\Models\Visitor; |
||
21 | use LivePersonInc\LiveEngageLaravel\Models\Agent; |
||
22 | use LivePersonInc\LiveEngageLaravel\Models\Skill; |
||
23 | use LivePersonInc\LiveEngageLaravel\Models\Campaign; |
||
24 | use LivePersonInc\LiveEngageLaravel\Models\Engagement; |
||
25 | use LivePersonInc\LiveEngageLaravel\Models\Conversation; |
||
26 | use LivePersonInc\LiveEngageLaravel\Models\Message; |
||
27 | use LivePersonInc\LiveEngageLaravel\Collections\EngagementHistory; |
||
28 | use LivePersonInc\LiveEngageLaravel\Collections\Skills; |
||
29 | use LivePersonInc\LiveEngageLaravel\Collections\AgentParticipants; |
||
30 | use LivePersonInc\LiveEngageLaravel\Exceptions\LiveEngageException; |
||
31 | use LivePersonInc\LiveEngageLaravel\Collections\ConversationHistory; |
||
32 | |||
33 | /** |
||
34 | * LiveEngageLaravel class holds all of the root package functions. |
||
35 | * |
||
36 | * All "setting" functions will return `$this` so method can be chained. Most methods will return a class object or Laravel collection. |
||
37 | */ |
||
38 | class LiveEngageLaravel |
||
39 | { |
||
40 | /** |
||
41 | * account - LiveEngage account number, usually set by configuration |
||
42 | * |
||
43 | * (default value: false) |
||
44 | * |
||
45 | * @var long |
||
46 | * @access private |
||
47 | */ |
||
48 | private $account = false; |
||
49 | /** |
||
50 | * skills - holds the skills for history retrieval |
||
51 | * |
||
52 | * (default value: []) |
||
53 | * |
||
54 | * @var array |
||
55 | * @access private |
||
56 | */ |
||
57 | private $skills = []; |
||
58 | /** |
||
59 | * config - holds the configuration key where keyset is stored in config/services.php |
||
60 | * |
||
61 | * (default value: 'services.liveperson.default') |
||
62 | * |
||
63 | * @var string |
||
64 | * @access private |
||
65 | */ |
||
66 | private $config = 'services.liveperson.default'; |
||
67 | /** |
||
68 | * version - api version |
||
69 | * |
||
70 | * (default value: '1.0') |
||
71 | * |
||
72 | * @var string |
||
73 | * @access private |
||
74 | */ |
||
75 | private $version = '1.0'; |
||
76 | /** |
||
77 | * history_limit - stores the history page limit |
||
78 | * |
||
79 | * (default value: 50) |
||
80 | * |
||
81 | * @var int |
||
82 | * @access private |
||
83 | */ |
||
84 | private $history_limit = 50; |
||
85 | private $interactive = true; |
||
86 | private $ended = true; |
||
87 | /** |
||
88 | * bearer - bearer token for V2 authentication |
||
89 | * |
||
90 | * (default value: false) |
||
91 | * |
||
92 | * @var string |
||
93 | * @access private |
||
94 | */ |
||
95 | private $bearer = false; |
||
96 | /** |
||
97 | * revision |
||
98 | * |
||
99 | * (default value: 0) |
||
100 | * |
||
101 | * @var int |
||
102 | * @access private |
||
103 | */ |
||
104 | private $revision = 0; |
||
105 | |||
106 | /** |
||
107 | * domain - api domain storage |
||
108 | * |
||
109 | * (default value: false) |
||
110 | * |
||
111 | * @var bool |
||
112 | * @access private |
||
113 | */ |
||
114 | private $domain = false; |
||
115 | |||
116 | /** |
||
117 | * retry_limit - number of times the request will attempt before it throws the exception. |
||
118 | * |
||
119 | * (default value: 5) |
||
120 | * |
||
121 | * @var int |
||
122 | * @access private |
||
123 | */ |
||
124 | private $retry_limit = 5; |
||
125 | /** |
||
126 | * retry_counter - stores current count of retries. |
||
127 | * |
||
128 | * (default value: 0) |
||
129 | * |
||
130 | * @var int |
||
131 | * @access private |
||
132 | */ |
||
133 | private $retry_counter = 0; |
||
134 | |||
135 | private $request_version = 'V1'; |
||
136 | |||
137 | /** |
||
138 | * __get magic function to retrieve private properties of the class. |
||
139 | * |
||
140 | * @access public |
||
141 | * @param mixed $attribute |
||
142 | * @return mixed |
||
143 | */ |
||
144 | 5 | public function __get($attribute) |
|
145 | { |
||
146 | 5 | return $this->$attribute; |
|
147 | } |
||
148 | |||
149 | private $request; |
||
150 | |||
151 | /** |
||
152 | * __construct function. |
||
153 | * |
||
154 | * @access public |
||
155 | * @return void |
||
156 | */ |
||
157 | 8 | public function __construct() |
|
158 | { |
||
159 | 8 | $this->account = config("{$this->config}.account"); |
|
160 | 8 | $this->version = config("{$this->config}.version") ?: $this->version; |
|
161 | 8 | $this->request = new LiveEngageRequest($this->config); |
|
162 | 8 | } |
|
163 | |||
164 | /** |
||
165 | * key function sets the keyset the class should use. Setting this once will be stored for script execution, but not for the session. |
||
166 | * |
||
167 | * @access public |
||
168 | * @param string $key (default: 'default') |
||
169 | * @return this |
||
170 | */ |
||
171 | 1 | public function key($key = 'default') |
|
172 | { |
||
173 | 1 | $this->config = "services.liveperson.$key"; |
|
174 | 1 | $this->__construct(); |
|
175 | |||
176 | 1 | return $this; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * version function. |
||
181 | * |
||
182 | * @access public |
||
183 | * @param string $version (default: 'V1') |
||
184 | * @return void |
||
185 | */ |
||
186 | public function version($version = 'V1') { |
||
187 | $this->request_version = 'V2'; |
||
188 | |||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * nonInteractive function. |
||
194 | * |
||
195 | * @access public |
||
196 | * @return void |
||
197 | */ |
||
198 | 1 | public function nonInteractive() |
|
199 | { |
||
200 | 1 | $this->interactive = false; |
|
201 | 1 | return $this; |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * active function. |
||
206 | * |
||
207 | * @access public |
||
208 | * @return void |
||
209 | */ |
||
210 | 1 | public function active() |
|
211 | { |
||
212 | 1 | $this->ended = false; |
|
213 | 1 | return $this; |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * limit function. |
||
218 | * |
||
219 | * @access public |
||
220 | * @param mixed $limit |
||
221 | * @return void |
||
222 | */ |
||
223 | 1 | public function limit($limit) |
|
224 | { |
||
225 | 1 | $this->history_limit = $limit; |
|
226 | |||
227 | 1 | return $this; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * retry function. |
||
232 | * |
||
233 | * @access public |
||
234 | * @param mixed $limit |
||
235 | * @return void |
||
236 | */ |
||
237 | 1 | public function retry($limit) |
|
238 | { |
||
239 | 1 | $this->retry_limit = $limit; |
|
240 | |||
241 | 1 | return $this; |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * account function. |
||
246 | * |
||
247 | * @access public |
||
248 | * @param mixed $accountid |
||
249 | * @return void |
||
250 | */ |
||
251 | 1 | public function account($accountid) |
|
252 | { |
||
253 | 1 | $this->account = $accountid; |
|
254 | |||
255 | 1 | return $this; |
|
256 | } |
||
257 | |||
258 | /** |
||
259 | * domain function sets the LivePerson domain for the secified service. Like `key` it is set for the execution script, but not session. It must be run each time. |
||
260 | * |
||
261 | * @access public |
||
262 | * @param mixed $service |
||
263 | * @return this |
||
264 | */ |
||
265 | 1 | public function domain($service) |
|
266 | { |
||
267 | 1 | $response = $this->request->V1("https://api.liveperson.net/api/account/{$this->account}/service/{$service}/baseURI.json?version={$this->version}", 'GET'); |
|
268 | |||
269 | 1 | $this->domain = $response->baseURI; |
|
270 | |||
271 | 1 | return $this; |
|
272 | } |
||
273 | |||
274 | /** |
||
275 | * visitor function gets or sets visitor attribute information - this only works for CHAT, not messaging. |
||
276 | * |
||
277 | * @access public |
||
278 | * @param string $visitorID |
||
279 | * @param string $sessionID |
||
280 | * @param mixed $setData (default: false) |
||
281 | * @return mixed |
||
282 | * @codeCoverageIgnore |
||
283 | */ |
||
284 | public function visitor($visitorID, $sessionID, $setData = false) |
||
285 | { |
||
286 | $this->domain('smt'); |
||
287 | |||
288 | if ($setData) { |
||
289 | $url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/events?v=1&sid={$sessionID}"; |
||
290 | |||
291 | return $this->request->V1($url, 'POST', $setData); |
||
292 | } else { |
||
293 | $url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/state?v=1&sid={$sessionID}"; |
||
294 | |||
295 | return $this->request->V1($url, 'GET'); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * chat function |
||
301 | * |
||
302 | * @codeCoverageIgnore |
||
303 | * |
||
304 | * @todo connect this to the server chat api - this function currently does nothing. |
||
305 | */ |
||
306 | public function chat() |
||
307 | { |
||
308 | $this->domain('conversationVep'); |
||
309 | |||
310 | $url = "https://{$this->domain}/api/account/{$this->account}/chat/request?v=1&NC=true"; |
||
311 | |||
312 | $args = [ |
||
313 | |||
314 | ]; |
||
315 | $payload = new Payload($args); |
||
316 | |||
317 | $response = $this->request->V1($url, 'POST', $payload); |
||
318 | |||
319 | return $response; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * retrieveHistory function. |
||
324 | * |
||
325 | * @access public |
||
326 | * @final |
||
327 | * @param Carbon $start |
||
328 | * @param Carbon $end |
||
329 | * @param string $url (default: false) |
||
330 | * @return mixed |
||
331 | */ |
||
332 | 1 | final public function retrieveHistory(Carbon $start, Carbon $end, $url = false) |
|
333 | { |
||
334 | 1 | $this->domain('engHistDomain'); |
|
335 | |||
336 | 1 | $url = $url ?: "https://{$this->domain}/interaction_history/api/account/{$this->account}/interactions/search?limit={$this->history_limit}&offset=0"; |
|
337 | |||
338 | 1 | $start_str = $start->toW3cString(); |
|
339 | 1 | $end_str = $end->toW3cString(); |
|
340 | |||
341 | 1 | $data = new Payload([ |
|
342 | 1 | 'interactive' => $this->interactive, |
|
343 | 1 | 'ended' => $this->ended, |
|
344 | 'start' => [ |
||
345 | 1 | 'from' => strtotime($start_str) . '000', |
|
346 | 1 | 'to' => strtotime($end_str) . '000', |
|
347 | ], |
||
348 | 1 | 'skillIds' => $this->skills |
|
349 | ]); |
||
350 | |||
351 | 1 | $result = $this->request->V1($url, 'POST', $data); |
|
352 | 1 | $result->records = $result->interactionHistoryRecords; |
|
353 | 1 | $result->interactionHistoryRecords = null; |
|
354 | |||
355 | 1 | return $result; |
|
356 | } |
||
357 | |||
358 | /** |
||
359 | * retrieveMsgHistory function. |
||
360 | * |
||
361 | * @access public |
||
362 | * @final |
||
363 | * @param Carbon $start |
||
364 | * @param Carbon $end |
||
365 | * @param string $url (default: false) |
||
366 | * @return mixed |
||
367 | */ |
||
368 | 1 | final public function retrieveMsgHistory(Carbon $start, Carbon $end, $url = false) |
|
369 | { |
||
370 | 1 | $this->domain('msgHist'); |
|
371 | 1 | $version = $this->request_version; |
|
372 | |||
373 | 1 | $url = $url ?: "https://{$this->domain}/messaging_history/api/account/{$this->account}/conversations/search?limit={$this->history_limit}&offset=0&sort=start:desc"; |
|
374 | |||
375 | 1 | $start_str = $start->toW3cString(); |
|
376 | 1 | $end_str = $end->toW3cString(); |
|
377 | |||
378 | 1 | $data = new Payload([ |
|
379 | 1 | 'status' => $this->ended ? ['CLOSE'] : ['OPEN', 'CLOSE'], |
|
380 | 'start' => [ |
||
381 | 1 | 'from' => strtotime($start_str) . '000', |
|
382 | 1 | 'to' => strtotime($end_str) . '000', |
|
383 | ], |
||
384 | 1 | 'skillIds' => $this->skills |
|
385 | ]); |
||
386 | |||
387 | 1 | $result = $this->request->$version($url, 'POST', $data); |
|
388 | 1 | $result->records = $result->conversationHistoryRecords; |
|
389 | 1 | $result->conversationHistoryRecords = null; |
|
390 | |||
391 | 1 | return $result; |
|
392 | } |
||
393 | |||
394 | /** |
||
395 | * skills function gets collection of skills associated with the account. |
||
396 | * |
||
397 | * @access public |
||
398 | * @return Collections\Skills |
||
399 | */ |
||
400 | 1 | public function skills() |
|
401 | { |
||
402 | 1 | $this->domain('accountConfigReadOnly'); |
|
403 | |||
404 | 1 | $url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/skills?v=4.0"; |
|
405 | |||
406 | 1 | return new Skills($this->request->V2($url, 'GET')); |
|
407 | } |
||
408 | |||
409 | /** |
||
410 | * getSkill function gets skill object based on ID. |
||
411 | * |
||
412 | * @access public |
||
413 | * @param int $skillId |
||
414 | * @return Models\Skill |
||
415 | */ |
||
416 | 1 | public function getSkill($skillId) |
|
417 | { |
||
418 | 1 | $this->domain('accountConfigReadOnly'); |
|
419 | |||
420 | 1 | $url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/skills/{$skillId}?v=4.0"; |
|
421 | |||
422 | 1 | return new Skill((array) $this->request->V2($url, 'GET')); |
|
423 | } |
||
424 | |||
425 | /** |
||
426 | * getAgent function gets agent object based on ID. |
||
427 | * |
||
428 | * @access public |
||
429 | * @param int $userId |
||
430 | * @return Models\Agent |
||
431 | */ |
||
432 | 1 | public function getAgent($userId) |
|
433 | { |
||
434 | 1 | $this->domain('accountConfigReadOnly'); |
|
435 | |||
436 | 1 | $url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/users/{$userId}?v=4.0"; |
|
437 | |||
438 | 1 | return new Agent((array) $this->request->V2($url, 'GET')); |
|
439 | } |
||
440 | |||
441 | /** |
||
442 | * updateAgent function. |
||
443 | * |
||
444 | * @access public |
||
445 | * @param mixed $userId |
||
446 | * @param mixed $properties |
||
447 | * @return void |
||
448 | * @codeCoverageIgnore |
||
449 | */ |
||
450 | public function updateAgent($userId, $properties) |
||
451 | { |
||
452 | $agent = $this->getAgent($userId); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
453 | |||
454 | $this->domain('accountConfigReadWrite'); |
||
455 | |||
456 | $url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/users/{$userId}?v=4.0"; |
||
457 | $headers = [ |
||
458 | 'X-HTTP-Method-Override' => 'PUT', |
||
459 | 'if-Match' => '*' |
||
460 | ]; |
||
461 | |||
462 | return new Agent((array) $this->request->V2($url, 'PUT', $properties, $headers)); |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * agents function gets collection of agents from account. |
||
467 | * |
||
468 | * @access public |
||
469 | * @return Collections\AgentParticipants |
||
470 | */ |
||
471 | 1 | public function agents() |
|
472 | { |
||
473 | 1 | $this->domain('accountConfigReadOnly'); |
|
474 | |||
475 | 1 | $select = implode(',', [ |
|
476 | 1 | 'id', |
|
477 | 'pid', |
||
478 | 'deleted', |
||
479 | 'loginName', |
||
480 | 'skills', |
||
481 | 'nickname', |
||
482 | 'dateCreated', |
||
483 | 'userTypeId', |
||
484 | 'isApiUser', |
||
485 | 'profileIds', |
||
486 | 'permissionGroups', |
||
487 | 'allowedAppKeys', |
||
488 | 'changePwdNextLogin', |
||
489 | 'maxChats', |
||
490 | 'skillIds', |
||
491 | 'lpaCreatedUser', |
||
492 | 'email', |
||
493 | 'lobs', |
||
494 | 'profiles', |
||
495 | 'fullName', |
||
496 | 'employeeId', |
||
497 | 'managedAgentGroups', |
||
498 | 'dateUpdated', |
||
499 | 'isEnabled', |
||
500 | 'lastPwdChangeDate', |
||
501 | 'pictureUrl' |
||
502 | ]); |
||
503 | |||
504 | 1 | $url = "https://{$this->domain}/api/account/{$this->account}/configuration/le-users/users?v=4.0&select=$select"; |
|
505 | |||
506 | 1 | return new AgentParticipants($this->request->V2($url, 'GET')); |
|
507 | } |
||
508 | |||
509 | /** |
||
510 | * getAgentStatus function gets status of agents based on provided Skill IDs. |
||
511 | * |
||
512 | * @access public |
||
513 | * @param int/array $skills |
||
514 | * @return Collections\AgentParticipants |
||
515 | */ |
||
516 | 1 | public function getAgentStatus($skills) |
|
517 | { |
||
518 | 1 | $skills = is_array($skills) ? $skills : [$skills]; |
|
519 | |||
520 | 1 | $this->domain('msgHist'); |
|
521 | |||
522 | 1 | $url = "https://{$this->domain}/messaging_history/api/account/{$this->account}/agent-view/status"; |
|
523 | |||
524 | 1 | $data = ['skillIds' => $skills]; |
|
525 | |||
526 | 1 | $response = $this->request->V1($url, 'POST', $data); |
|
527 | 1 | $collection = new AgentParticipants($response->agentStatusRecords); |
|
528 | 1 | $collection->metaData = new MetaData((array) $response->_metadata); |
|
529 | |||
530 | 1 | return $collection; |
|
531 | |||
532 | } |
||
533 | |||
534 | /** |
||
535 | * conversationHistory function. |
||
536 | * |
||
537 | * @access public |
||
538 | * @param Carbon $start (default: null) |
||
539 | * @param Carbon $end (default: null) |
||
540 | * @param int/array $skills (default: []) |
||
541 | * @return Collections\ConversationHistory |
||
542 | */ |
||
543 | 1 | public function conversationHistory(Carbon $start = null, Carbon $end = null, $skills = []) |
|
544 | { |
||
545 | 1 | $this->retry_counter = 0; |
|
546 | 1 | $this->skills = $skills; |
|
547 | |||
548 | 1 | $start = $start ?: (new Carbon())->today(); |
|
549 | 1 | $end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59); |
|
550 | |||
551 | 1 | $results_object = $this->retrieveMsgHistory($start, $end); |
|
552 | |||
553 | 1 | $results_object->_metadata->start = $start; |
|
554 | 1 | $results_object->_metadata->end = $end; |
|
555 | |||
556 | 1 | $meta = new MetaData((array) $results_object->_metadata); |
|
557 | |||
558 | 1 | $collection = new ConversationHistory($results_object->records); |
|
559 | 1 | $collection->metaData = $meta; |
|
560 | |||
561 | 1 | return $collection; |
|
562 | |||
563 | } |
||
564 | |||
565 | /** |
||
566 | * getConversation function. |
||
567 | * |
||
568 | * @access public |
||
569 | * @param mixed $conversationId |
||
570 | * @return Models\Conversation |
||
571 | */ |
||
572 | 1 | public function getConversation($conversationId) |
|
573 | { |
||
574 | 1 | $this->domain('msgHist'); |
|
575 | |||
576 | 1 | $url = "https://{$this->domain}/messaging_history/api/account/{$this->account}/conversations/conversation/search"; |
|
577 | |||
578 | 1 | $data = new Payload([ |
|
579 | 1 | 'conversationId' => $conversationId |
|
580 | ]); |
||
581 | |||
582 | 1 | $result = $this->request->V1($url, 'POST', $data); |
|
583 | 1 | if (!count($result->conversationHistoryRecords)) { |
|
584 | return null; // @codeCoverageIgnore |
||
585 | } |
||
586 | |||
587 | 1 | return new Conversation((array) $result->conversationHistoryRecords[0]); |
|
588 | } |
||
589 | |||
590 | /** |
||
591 | * engagementHistory function. |
||
592 | * |
||
593 | * @access public |
||
594 | * @param Carbon $start (default: null) |
||
595 | * @param Carbon $end (default: null) |
||
596 | * @param int/array $skills (default: []) |
||
597 | * @return Collections\EngagementHistory |
||
598 | */ |
||
599 | 1 | public function engagementHistory(Carbon $start = null, Carbon $end = null, $skills = []) |
|
600 | { |
||
601 | 1 | $this->retry_counter = 0; |
|
602 | 1 | $this->skills = $skills; |
|
603 | |||
604 | 1 | $start = $start ?: (new Carbon())->today(); |
|
605 | 1 | $end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59); |
|
606 | |||
607 | 1 | $results_object = $this->retrieveHistory($start, $end); |
|
608 | |||
609 | 1 | $results_object->_metadata->start = $start; |
|
610 | 1 | $results_object->_metadata->end = $end; |
|
611 | |||
612 | 1 | $meta = new MetaData((array) $results_object->_metadata); |
|
613 | |||
614 | 1 | $collection = new EngagementHistory($results_object->records); |
|
615 | 1 | $collection->metaData = $meta; |
|
616 | |||
617 | 1 | return $collection; |
|
618 | |||
619 | } |
||
620 | |||
621 | /** |
||
622 | * status function gets status of the account. |
||
623 | * |
||
624 | * @access public |
||
625 | * @return Models\AccountStatus |
||
626 | */ |
||
627 | 1 | public function status() |
|
628 | { |
||
629 | 1 | $url = "https://status.liveperson.com/json?site={$this->account}"; |
|
630 | |||
631 | 1 | $response = $this->request->V1($url, 'GET'); |
|
632 | |||
633 | 1 | return new AccountStatus((array) $response); |
|
634 | } |
||
635 | } |