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