@@ -60,45 +60,54 @@ public function usersTable($area): JsonResponse
6060 return response ()->json ($ data );
6161 }
6262
63- public function userData ($ extension ): JsonResponse
64- {
65- $ response = Http::get ("http://10.57.251.181:3005/extension/info?ext= {$ extension }" );
66- if (!$ response ->successful ()) {
67- return response ()->json (['error ' => 'No se pudo obtener los datos ' ], 500 );
68- }
63+ public function userData ($ extension ): JsonResponse
64+ {
65+ $ response = Http::get ("http://10.57.251.181:3005/extension/info?ext= {$ extension }" );
66+ if (!$ response ->successful ()) {
67+ return response ()->json (['error ' => 'No se pudo obtener los datos ' ], 500 );
68+ }
6969
70- $ data = $ response ->json ();
70+ $ data = $ response ->json ();
7171
72- $ texto = $ data ['member ' ] ?? $ data ['member2 ' ] ?? null ;
72+ $ texto = $ data ['member ' ] ?? $ data ['member2 ' ] ?? null ;
7373
74- if ($ texto ) {
75- // Extraer nombre
76- $ nombre = explode (' ' , $ texto )[0 ];
74+ if ($ texto ) {
75+ $ nombre = explode (' ' , $ texto )[0 ];
76+ preg_match_all ('/\((.*?)\)/ ' , $ texto , $ matches );
77+ $ estado = null ;
78+ $ pausa = null ;
7779
78- // Extraer estado (Busy, In call, etc.)
79- preg_match_all ('/\((.*?)\)/ ' , $ texto , $ matches );
80- $ estado = null ;
81- $ pausa = null ;
80+ foreach ($ matches [1 ] as $ match ) {
81+ if (str_contains ($ match , 'paused ' )) {
82+ $ pausa = $ match ;
83+ }
84+ if (in_array ($ match , ['Busy ' , 'On Hold ' , 'In call ' , 'Ringing ' , 'Not in use ' ])) {
85+ $ estado = $ match ;
86+ }
87+ }
8288
83- foreach ($ matches [1 ] as $ match ) {
89+ // Si no encontramos la pausa en el primer texto, revisamos member2
90+ if (!$ pausa && isset ($ data ['member2 ' ])) {
91+ preg_match_all ('/\((.*?)\)/ ' , $ data ['member2 ' ], $ matches2 );
92+ foreach ($ matches2 [1 ] as $ match ) {
8493 if (str_contains ($ match , 'paused ' )) {
85- $ pausa = $ match ; // ej: paused:ACW was 2108 secs ago
86- }
87- if (in_array ($ match , ['Busy ' , 'On Hold ' , 'In call ' , 'Ringing ' , 'Not in use ' ])) {
88- $ estado = $ match ;
94+ $ pausa = $ match ;
95+ break ;
8996 }
9097 }
91-
92- $ data ['member ' ] = [
93- 'nombre ' => $ nombre ,
94- 'estado ' => $ estado ,
95- 'pausa ' => $ pausa ,
96- ];
9798 }
9899
99- return response ()->json ($ data );
100+ $ data ['member ' ] = [
101+ 'nombre ' => $ nombre ,
102+ 'estado ' => $ estado ,
103+ 'pausa ' => $ pausa ,
104+ ];
100105 }
101106
107+ return response ()->json ($ data );
108+ }
109+
110+
102111
103112
104113
@@ -159,19 +168,87 @@ public function chanelHangup(Request $request)
159168 return response ()->json (['error ' => 'No se pudo colgar el canal ' ], 500 );
160169 }
161170
162- public function pausedExtension (Request $ request )
171+ public function pauseExtension (Request $ request ): JsonResponse
163172 {
164173 $ extension = $ request ->input ('extension ' );
165174
166- $ response = Http::post ('http://10.57.251.181:3000/channel/hangup ' , [
167- 'channel ' => $ extension
175+ if (!$ extension ) {
176+ return response ()->json (['error ' => 'Extensión no proporcionada ' ], 400 );
177+ }
178+
179+ $ interface = "SIP/ {$ extension }" ;
180+ $ queues = [];
181+ for ($ i = 1 ; $ i <= 120 ; $ i ++) {
182+ $ queues [] = 'Q ' . str_pad ($ i , 3 , '0 ' , STR_PAD_LEFT );
183+ }
184+ $ paused = 1 ;
185+ $ reason = 'ACW ' ;
186+
187+ $ response = Http::post ('http://10.57.251.181:3000/queue/pause ' , [
188+ 'queues ' => $ queues ,
189+ 'interface ' => $ interface ,
190+ 'paused ' => $ paused ,
191+ 'reason ' => $ reason ,
168192 ]);
169193
170194 if ($ response ->successful ()) {
171- return response ()->json (['message ' => 'Extension pausada correctamente ' ]);
195+ return response ()->json (['message ' => 'Agente pausado correctamente ' ]);
172196 }
173197
174- return response ()->json (['error ' => 'No se pudo colgar el canal ' ], 500 );
198+ return response ()->json (['error ' => 'No se pudo pausar al agente ' ], 500 );
199+ }
200+
201+
202+ public function unpauseExtension (Request $ request ): JsonResponse
203+ {
204+ $ extension = $ request ->input ('extension ' );
205+
206+ if (!$ extension ) {
207+ return response ()->json (['error ' => 'Extensión no proporcionada ' ], 400 );
208+ }
209+
210+ $ interface = "SIP/ {$ extension }" ;
211+ $ queues = [];
212+ for ($ i = 1 ; $ i <= 120 ; $ i ++) {
213+ $ queues [] = 'Q ' . str_pad ($ i , 3 , '0 ' , STR_PAD_LEFT );
214+ }
215+ $ paused = 0 ;
216+ $ reason = 'ACW ' ;
217+
218+ $ response = Http::post ('http://10.57.251.181:3000/queue/pause ' , [
219+ 'queues ' => $ queues ,
220+ 'interface ' => $ interface ,
221+ 'paused ' => $ paused ,
222+ 'reason ' => $ reason ,
223+ ]);
224+
225+ if ($ response ->successful ()) {
226+ return response ()->json (['message ' => 'Agente despausado correctamente ' ]);
227+ }
228+
229+ return response ()->json (['error ' => 'No se pudo pausar al agente ' ], 500 );
230+ }
231+
232+ public function channelTransfer (Request $ request ): JsonResponse
233+ {
234+ $ channel = $ request ->input ('channel ' );
235+ $ destiny = $ request ->input ('destiny ' );
236+
237+ if (!$ channel || !$ destiny ) {
238+ return response ()->json (['error ' => 'Extensión no proporcionada ' ], 400 );
239+ }
240+
241+
242+ $ response = Http::post ('http://10.57.251.181:3006/transferir ' , [
243+ 'canal ' => $ channel ,
244+ 'destino ' => $ destiny ,
245+ ]);
246+
247+ if ($ response ->successful ()) {
248+ return response ()->json (['message ' => 'Llamada transferida correctamente ' ]);
249+ }
250+
251+ return response ()->json (['error ' => 'No se pudo transferir la llamada ' ], 500 );
175252 }
176253
177254}
0 commit comments