|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LivePersonInc\LiveEngageLaravel; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use GuzzleHttp\HandlerStack; |
|
8
|
|
|
use GuzzleHttp\Subscriber\Oauth\Oauth1; |
|
9
|
|
|
use LivePersonInc\LiveEngageLaravel\Models\Info; |
|
10
|
|
|
use LivePersonInc\LiveEngageLaravel\Models\MetaData; |
|
11
|
|
|
use LivePersonInc\LiveEngageLaravel\Models\MessagingInfo; |
|
12
|
|
|
use LivePersonInc\LiveEngageLaravel\Models\Payload; |
|
13
|
|
|
use LivePersonInc\LiveEngageLaravel\Models\Visitor; |
|
14
|
|
|
use LivePersonInc\LiveEngageLaravel\Models\Agent; |
|
15
|
|
|
use LivePersonInc\LiveEngageLaravel\Models\Campaign; |
|
16
|
|
|
use LivePersonInc\LiveEngageLaravel\Models\Engagement; |
|
17
|
|
|
use LivePersonInc\LiveEngageLaravel\Models\Conversation; |
|
18
|
|
|
use LivePersonInc\LiveEngageLaravel\Collections\EngagementHistory; |
|
19
|
|
|
use LivePersonInc\LiveEngageLaravel\Collections\AgentParticipants; |
|
20
|
|
|
use LivePersonInc\LiveEngageLaravel\Exceptions\LiveEngageException; |
|
21
|
|
|
use LivePersonInc\LiveEngageLaravel\Collections\ConversationHistory; |
|
22
|
|
|
|
|
23
|
|
|
class LiveEngageLaravel |
|
24
|
|
|
{ |
|
25
|
|
|
private $account = false; |
|
26
|
|
|
private $results = []; |
|
27
|
|
|
private $skills = []; |
|
28
|
|
|
private $next = false; |
|
29
|
|
|
private $prev = false; |
|
30
|
|
|
private $start; |
|
31
|
|
|
private $end; |
|
32
|
|
|
private $config = 'services.liveperson.default'; |
|
33
|
|
|
private $version = '1.0'; |
|
34
|
|
|
private $history_limit = 50; |
|
35
|
|
|
private $history = false; |
|
36
|
|
|
private $context = 'interactionHistoryRecords'; |
|
37
|
|
|
private $interactive = true; |
|
38
|
|
|
private $ended = true; |
|
39
|
|
|
private $bearer = false; |
|
40
|
|
|
|
|
41
|
|
|
private $domain = false; |
|
42
|
|
|
|
|
43
|
|
|
private $retry_limit = 5; |
|
44
|
|
|
private $retry_counter = 0; |
|
45
|
|
|
|
|
46
|
5 |
|
public function __get($attribute) |
|
47
|
|
|
{ |
|
48
|
5 |
|
return $this->$attribute; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
7 |
|
public function __construct() |
|
52
|
|
|
{ |
|
53
|
7 |
|
$this->account = config("{$this->config}.account"); |
|
54
|
|
|
//$this->domain = config("{$this->config}.domain"); |
|
55
|
7 |
|
$this->version = config("{$this->config}.version") ?: $this->version; |
|
56
|
7 |
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
public function key($key = 'default') |
|
59
|
|
|
{ |
|
60
|
1 |
|
$this->config = "services.liveperson.$key"; |
|
61
|
|
|
|
|
62
|
1 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function nonInteractive() |
|
66
|
|
|
{ |
|
67
|
1 |
|
$this->interactive = false; |
|
68
|
1 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function active() |
|
72
|
|
|
{ |
|
73
|
1 |
|
$this->ended = false; |
|
74
|
1 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
public function limit($limit) |
|
78
|
|
|
{ |
|
79
|
1 |
|
$this->history_limit = $limit; |
|
80
|
|
|
|
|
81
|
1 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
public function skills($skills) |
|
85
|
|
|
{ |
|
86
|
1 |
|
$this->skills = $skills; |
|
87
|
|
|
|
|
88
|
1 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
public function retry($limit) |
|
92
|
|
|
{ |
|
93
|
1 |
|
$this->retry_limit = $limit; |
|
94
|
|
|
|
|
95
|
1 |
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
public function account($accountid) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$this->account = $accountid; |
|
101
|
|
|
|
|
102
|
1 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
public function domain($service) |
|
106
|
|
|
{ |
|
107
|
1 |
|
$response = $this->requestV1("https://api.liveperson.net/api/account/{$this->account}/service/{$service}/baseURI.json?version={$this->version}", 'GET'); |
|
108
|
|
|
|
|
109
|
1 |
|
$this->domain = $response->baseURI; |
|
110
|
|
|
|
|
111
|
1 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function visitor($visitorID, $sessionID, $setData = false) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->domain('smt'); |
|
117
|
|
|
|
|
118
|
|
|
if ($setData) { |
|
119
|
|
|
$url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/events?v=1&sid={$sessionID}"; |
|
120
|
|
|
|
|
121
|
|
|
return $this->requestV1($url, 'POST', $setData); |
|
122
|
|
|
} else { |
|
123
|
|
|
$url = "https://{$this->domain}/api/account/{$this->account}/monitoring/visitors/{$visitorID}/visits/current/state?v=1&sid={$sessionID}"; |
|
124
|
|
|
|
|
125
|
|
|
return $this->requestV1($url, 'GET'); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
1 |
|
final public function retrieveHistory(Carbon $start, Carbon $end, $url = false) |
|
130
|
|
|
{ |
|
131
|
1 |
|
$this->domain('engHistDomain'); |
|
132
|
|
|
|
|
133
|
1 |
|
$url = $url ?: "https://{$this->domain}/interaction_history/api/account/{$this->account}/interactions/search?limit={$this->history_limit}&offset=0"; |
|
134
|
|
|
|
|
135
|
1 |
|
$start_str = $start->toW3cString(); |
|
136
|
1 |
|
$end_str = $end->toW3cString(); |
|
137
|
|
|
|
|
138
|
1 |
|
$data = new Payload([ |
|
139
|
1 |
|
'interactive' => $this->interactive, |
|
140
|
1 |
|
'ended' => $this->ended, |
|
141
|
|
|
'start' => [ |
|
142
|
1 |
|
'from' => strtotime($start_str) . '000', |
|
143
|
1 |
|
'to' => strtotime($end_str) . '000', |
|
144
|
|
|
], |
|
145
|
1 |
|
'skillIds' => $this->skills |
|
146
|
|
|
]); |
|
147
|
|
|
|
|
148
|
1 |
|
return $this->requestV1($url, 'POST', $data); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
1 |
|
final public function retrieveMsgHistory(Carbon $start, Carbon $end, $url = false) |
|
152
|
|
|
{ |
|
153
|
1 |
|
$this->domain('msgHist'); |
|
154
|
|
|
|
|
155
|
1 |
|
$url = $url ?: "https://{$this->domain}/messaging_history/api/account/{$this->account}/conversations/search?limit={$this->history_limit}&offset=0&sort=start:desc"; |
|
156
|
|
|
|
|
157
|
1 |
|
$start_str = $start->toW3cString(); |
|
158
|
1 |
|
$end_str = $end->toW3cString(); |
|
159
|
|
|
|
|
160
|
1 |
|
$data = new Payload([ |
|
161
|
1 |
|
'status' => $this->ended ? ['CLOSE'] : ['OPEN', 'CLOSE'], |
|
162
|
|
|
'start' => [ |
|
163
|
1 |
|
'from' => strtotime($start_str) . '000', |
|
164
|
1 |
|
'to' => strtotime($end_str) . '000', |
|
165
|
|
|
], |
|
166
|
1 |
|
'skillIds' => $this->skills |
|
167
|
|
|
]); |
|
168
|
|
|
|
|
169
|
1 |
|
return $this->requestV1($url, 'POST', $data); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
1 |
|
public function getAgentStatus($skills) |
|
173
|
|
|
{ |
|
174
|
1 |
|
$skills = is_array($skills) ? $skills : [$skills]; |
|
175
|
|
|
|
|
176
|
1 |
|
$this->domain('msgHist'); |
|
177
|
|
|
|
|
178
|
1 |
|
$url = "https://{$this->domain}/messaging_history/api/account/{$this->account}/agent-view/status"; |
|
179
|
|
|
|
|
180
|
1 |
|
$data = ['skillIds' => $skills]; |
|
181
|
|
|
|
|
182
|
1 |
|
$response = $this->requestV1($url, 'POST', $data); |
|
183
|
1 |
|
$collection = new AgentParticipants($response->agentStatusRecords); |
|
184
|
1 |
|
$collection->metaData = new MetaData((array) $response->_metadata); |
|
185
|
|
|
|
|
186
|
1 |
|
return $collection; |
|
187
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
1 |
|
public function messagingHistory(Carbon $start = null, Carbon $end = null) |
|
191
|
|
|
{ |
|
192
|
1 |
|
$this->retry_counter = 0; |
|
193
|
|
|
|
|
194
|
1 |
|
$start = $start ?: (new Carbon())->today(); |
|
195
|
1 |
|
$end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59); |
|
196
|
|
|
|
|
197
|
1 |
|
$results_object = $this->retrieveMsgHistory($start, $end); |
|
198
|
|
|
|
|
199
|
1 |
|
if (!$results_object) { |
|
200
|
|
|
return false; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
1 |
|
$results_object->_metadata->start = $start; |
|
204
|
1 |
|
$results_object->_metadata->end = $end; |
|
205
|
|
|
|
|
206
|
1 |
|
$meta = new MetaData((array) $results_object->_metadata); |
|
207
|
|
|
|
|
208
|
1 |
|
$collection = new ConversationHistory($results_object->conversationHistoryRecords); |
|
209
|
1 |
|
$collection->metaData = $meta; |
|
210
|
|
|
|
|
211
|
1 |
|
return $collection; |
|
212
|
|
|
|
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
1 |
|
public function history(Carbon $start = null, Carbon $end = null) |
|
216
|
|
|
{ |
|
217
|
1 |
|
$this->retry_counter = 0; |
|
218
|
|
|
|
|
219
|
1 |
|
$start = $start ?: (new Carbon())->today(); |
|
220
|
1 |
|
$end = $end ?: (new Carbon())->today()->addHours(23)->addMinutes(59); |
|
221
|
|
|
|
|
222
|
1 |
|
$results_object = $this->retrieveHistory($start, $end); |
|
223
|
|
|
|
|
224
|
1 |
|
if (!$results_object) { |
|
225
|
|
|
return false; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
1 |
|
$results_object->_metadata->start = $start; |
|
229
|
1 |
|
$results_object->_metadata->end = $end; |
|
230
|
|
|
|
|
231
|
1 |
|
$meta = new MetaData((array) $results_object->_metadata); |
|
232
|
|
|
|
|
233
|
1 |
|
$collection = new EngagementHistory($results_object->interactionHistoryRecords); |
|
234
|
1 |
|
$collection->metaData = $meta; |
|
235
|
|
|
|
|
236
|
1 |
|
return $collection; |
|
237
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
public function login() |
|
241
|
|
|
{ |
|
242
|
|
|
$this->domain('agentVep'); |
|
243
|
|
|
|
|
244
|
|
|
$consumer_key = config("{$this->config}.key"); |
|
245
|
|
|
$consumer_secret = config("{$this->config}.secret"); |
|
246
|
|
|
$token = config("{$this->config}.token"); |
|
247
|
|
|
$secret = config("{$this->config}.token_secret"); |
|
248
|
|
|
$username = config("{$this->config}.user_name"); |
|
249
|
|
|
|
|
250
|
|
|
$auth = [ |
|
251
|
|
|
'username' => $username, |
|
252
|
|
|
'appKey' => $consumer_key, |
|
253
|
|
|
'secret' => $consumer_secret, |
|
254
|
|
|
'accessToken' => $token, |
|
255
|
|
|
'accessTokenSecret' => $secret, |
|
256
|
|
|
]; |
|
257
|
|
|
|
|
258
|
|
|
$url = "https://{$this->domain}/api/account/{$this->account}/login?v=1.3"; |
|
259
|
|
|
|
|
260
|
|
|
$response = $this->requestV1($url, 'POST', $auth); |
|
261
|
|
|
|
|
262
|
|
|
$this->bearer = $response->bearer; |
|
263
|
|
|
|
|
264
|
|
|
return $this; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
private function requestV2($url, $method, $payload = false) |
|
|
|
|
|
|
268
|
|
|
{ |
|
269
|
|
|
$this->login(); |
|
270
|
|
|
|
|
271
|
|
|
$client = new Client(); |
|
272
|
|
|
$args = [ |
|
273
|
|
|
'headers' => [ |
|
274
|
|
|
'content-type' => 'application/json', |
|
275
|
|
|
], |
|
276
|
|
|
'body' => json_encode($payload) |
|
277
|
|
|
]; |
|
278
|
|
|
|
|
279
|
|
|
try { |
|
280
|
|
|
$res = $client->request($method, $url, $args); |
|
281
|
|
|
} catch (\Exception $e) { |
|
282
|
|
|
throw $e; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
return json_decode($res->getBody()); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
2 |
|
private function requestClient() |
|
289
|
|
|
{ |
|
290
|
2 |
|
$consumer_key = config("{$this->config}.key"); |
|
291
|
2 |
|
$consumer_secret = config("{$this->config}.secret"); |
|
292
|
2 |
|
$token = config("{$this->config}.token"); |
|
293
|
2 |
|
$secret = config("{$this->config}.token_secret"); |
|
294
|
|
|
|
|
295
|
2 |
|
$stack = HandlerStack::create(); |
|
296
|
2 |
|
$auth = new Oauth1([ |
|
297
|
2 |
|
'consumer_key' => $consumer_key, |
|
298
|
2 |
|
'consumer_secret' => $consumer_secret, |
|
299
|
2 |
|
'token' => $token, |
|
300
|
2 |
|
'token_secret' => $secret, |
|
301
|
2 |
|
'signature_method'=> Oauth1::SIGNATURE_METHOD_HMAC, |
|
302
|
|
|
]); |
|
303
|
2 |
|
$stack->push($auth); |
|
304
|
|
|
|
|
305
|
2 |
|
$client = new Client([ |
|
306
|
2 |
|
'handler' => $stack, |
|
307
|
|
|
]); |
|
308
|
|
|
|
|
309
|
2 |
|
return $client; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
1 |
|
private function requestV1($url, $method, $payload = []) |
|
313
|
|
|
{ |
|
314
|
1 |
|
$client = $this->requestClient(); |
|
315
|
|
|
|
|
316
|
|
|
$args = [ |
|
317
|
1 |
|
'auth' => 'oauth', |
|
318
|
|
|
'headers' => [ |
|
319
|
|
|
'content-type' => 'application/json', |
|
320
|
|
|
], |
|
321
|
1 |
|
'body' => json_encode($payload) |
|
322
|
|
|
]; |
|
323
|
|
|
|
|
324
|
|
|
try { |
|
325
|
1 |
|
$res = $client->request($method, $url, $args); |
|
326
|
1 |
|
$response = json_decode($res->getBody()); |
|
327
|
|
|
} catch (\Exception $e) { |
|
328
|
|
|
if ($this->retry_counter < $this->retry_limit || $this->retry_limit == -1) { |
|
329
|
|
|
usleep(1500); |
|
330
|
|
|
$this->retry_counter++; |
|
331
|
|
|
$response = $this->requestV1($url, $payload); |
|
332
|
|
|
} else { |
|
333
|
|
|
throw $e; //new LiveEngageException("Retry limit has been exceeded ($this->retry_limit)", 100); |
|
334
|
|
|
} |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
1 |
|
return $response; |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.