-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIssabelPbxApi.php
204 lines (161 loc) · 4.9 KB
/
IssabelPbxApi.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
<?php
namespace App\Models;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Foundation\Application;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Http;
class IssabelPbxApi
{
public $USERNAME = "";
public $PASSWORD = "";
public $SERVER_URL = "";
/**
* @param $server
* @param $username
* @param $password
*/
public function __construct($server, $username, $password){
$this->SERVER_URL = $server;
$this->USERNAME = $username;
$this->PASSWORD = $password;
}
/**
* @param $server
* @param $username
* @param $password
* @return self
*/
public static function connect($server, $username, $password){
return new self($server, $username, $password);
}
/**
* @return mixed
* @throws ConnectionException
*/
public function getToken(){
$response = Http::attach('username', $this->USERNAME)
->attach('password', $this->PASSWORD)
->post(sprintf('%s/pbxapi/authenticate', $this->SERVER_URL));
if ($response->failed())
throw new \Exception("PBX AUTH API Error: " . $response->body());
return $response->json()['access_token'];
}
/**
* @param $method
* @param $endpoint
* @param $data
* @return mixed
* @throws ConnectionException
*/
private function request($method, $endpoint, $data = []){
$getToken = $this->getToken();
$response = Http::withToken($getToken)
->$method(sprintf("%s/pbxapi/%s", $this->SERVER_URL, $endpoint), $data);
if ($response->failed())
throw new \Exception("PBX API Error: " . $response->body());
return $response;
}
/**
* @return mixed
* @throws ConnectionException
*/
public function extensions(){
return $this->request('get', 'extensions')->json();
}
/**
* @param $extension
* @return mixed
* @throws ConnectionException
*/
public function extension($extension){
return $this->request('get', "extensions/$extension")->json();
}
/**
* @return mixed
* @throws ConnectionException
*/
public function channels(){
return $this->request('get', "v2apiservice", [
"action" => "channels"
])->json();
}
/**
* @param $startDate
* @param $endDate
* @param $filter
* @return mixed
* @throws ConnectionException
*/
public function cdr($startDate = null, $endDate = null, $filter = "all"){
if ($startDate === null)
$startDate = date("Y-m-d", strtotime("-2 days"));
if ($endDate === null)
$endDate = date("Y-m-d");
return $this->request('get',
"v2apiservice",
[
"action" => "cdr",
"start_date" => $startDate,
"end_date" => $endDate,
"extension" => $filter
])->json();
}
/**
* @param $cdrFile
* @return ResponseFactory|Application|Response
* @throws ConnectionException
*/
public function cdrPlayer($cdrFile = null){
if ($cdrFile === null)
throw new \Exception("File not found");
$getFile = $this->request('get',
"v2apiservice",
[
"action" => "player",
"file" => $cdrFile
]);
return response($getFile->body(), 200)
->header('Content-Type', 'audio/mpeg')
->header('Content-Disposition', 'inline; filename="' . basename($cdrFile) . '"');
}
/**
* @param $channel
* @param $extension
* @param $callerID
* @param $context
* @param $timeout
* @return mixed
* @throws ConnectionException
*/
public function originate($channel = "SIP/XXXXXX", $extension = "SIP/XXXXXXX", $callerID = "Title", $context = "from-internal", $timeout = "30000"){
return $this->request('get',
"manager/originate",
[
'channel' => $channel,
'extension' => $extension,
'callerid' => $callerID,
'context' => $context,
'priority' => 1,
'timeout' => $timeout,
])->json();
}
/**
* @param $channel
* @param $extension
* @param $listenMode
* @param $callerID
* @return mixed
* @throws ConnectionException
*/
public function spyCall($channel = "SIP/XXXXXX", $extension = "SIP/XXXXXXX", $listenMode = "q", $callerID = "Dinleme"){
return $this->request('get',
"manager/originate",
[
'channel' => $channel,
'application' => 'ChanSpy',
'data' => "$extension,$listenMode",
'callerid' => $callerID,
])->json();
}
}