Diferencia entre revisiones de «Servicios Web - Llamadas»
| Línea 61: | Línea 61: | ||
El resultado de esta invocación resulta en un json con un parámetro <code>error</code> que puede ser <code>true</code> o <code>false</code>. | El resultado de esta invocación resulta en un json con un parámetro <code>error</code> que puede ser <code>true</code> o <code>false</code>. | ||
| + | |||
| + | == Forma programática de incorporar llamadas == | ||
| + | |||
| + | Es posible modificar este script en perl para extraer las llamadas de la tabla cdr de asterisk e incorporarlas a NiMbox | ||
| + | |||
| + | <pre> | ||
| + | #!/usr/bin/perl | ||
| + | |||
| + | use strict; | ||
| + | use warnings; | ||
| + | |||
| + | use DBI; | ||
| + | use LWP::UserAgent; | ||
| + | use LWP::ConnCache; | ||
| + | use URI::Escape; | ||
| + | use JSON; | ||
| + | |||
| + | # configuration | ||
| + | |||
| + | my $ASTERISK_HOST = "192.168.128.96"; | ||
| + | my $ASTERISK_USER = "testing"; | ||
| + | my $ASTERISK_PASSWORD = "testing"; | ||
| + | |||
| + | my $NIMBOX_HOST = "192.168.1.98:8080"; | ||
| + | my $NIMBOX_USER = "admin"; | ||
| + | my $NIMBOX_PASSWORD = "admin"; | ||
| + | |||
| + | # do the query | ||
| + | |||
| + | my $start = time; | ||
| + | |||
| + | my $handle = DBI->connect( | ||
| + | "DBI:mysql:asterisk;host=$ASTERISK_HOST", | ||
| + | $ASTERISK_USER, | ||
| + | $ASTERISK_PASSWORD); | ||
| + | |||
| + | my $sql = <<EOSQL; | ||
| + | SELECT | ||
| + | DATE_FORMAT(calldate, '%Y-%m-%dT%TZ') AS initiated, | ||
| + | src AS source, | ||
| + | dst AS destination, | ||
| + | duration AS duration, | ||
| + | CONCAT('http://192.168.128.96/recordings/', channel) AS url | ||
| + | FROM | ||
| + | cdr | ||
| + | WHERE | ||
| + | calldate >= '2010-01-01' AND LENGTH(dst) > 7 | ||
| + | EOSQL | ||
| + | |||
| + | my $statement = $handle->prepare($sql); | ||
| + | $statement->execute(); | ||
| + | |||
| + | # send the results | ||
| + | |||
| + | my $agent = LWP::UserAgent->new(); | ||
| + | my $cache = LWP::ConnCache->new(); | ||
| + | $cache->total_capacity([1]); | ||
| + | $agent->conn_cache($cache); | ||
| + | $agent->credentials("$NIMBOX_HOST", "NiMbox", $NIMBOX_USER, $NIMBOX_PASSWORD); | ||
| + | |||
| + | while (my $r = $statement->fetchrow_hashref()) { | ||
| + | |||
| + | print "Processing $r->{initiated} from $r->{source} to $r->{destination}... "; | ||
| + | |||
| + | # prepare the url | ||
| + | |||
| + | my $url = "http://$NIMBOX_HOST/direct/items/phonecalls/create"; | ||
| + | $url = $url . "?initiated=$r->{initiated}"; | ||
| + | $url = $url . "&source=$r->{source}"; | ||
| + | $url = $url . "&destination=" . uri_escape(convertDestination($r->{'destination'})); | ||
| + | $url = $url . "&duration=$r->{duration}"; | ||
| + | $url = $url . "&url=" . uri_escape($r->{'url'}); | ||
| + | |||
| + | my $response = $agent->get($url); | ||
| + | my $json = decode_json($response->content); | ||
| + | if ($json->{'error'}) { | ||
| + | print "ERROR\n"; | ||
| + | } else { | ||
| + | print "OK\n"; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | print "execution time: " . (time - $start) . "\n"; | ||
| + | |||
| + | exit(0); | ||
| + | |||
| + | |||
| + | sub convertDestination { | ||
| + | |||
| + | my ($destination) = @_; | ||
| + | |||
| + | if ($destination =~ /^9/) { | ||
| + | $destination = substr($destination, 1); | ||
| + | if (length($destination) == 7) { | ||
| + | $destination = "0212" . $destination; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | return $destination; | ||
| + | |||
| + | } | ||
| + | </pre> | ||
[[Category:Servicios Web]] | [[Category:Servicios Web]] | ||
Revisión de 11:12 24 nov 2012
Contenido |
Incorporar una llamada - /direct/items/phonecalls/create
Para incorporar una nueva llamada hay que realizar una solicitud a https://host/direct/items/phonecalls/create con los siguientes parámetros:
- initiated
- fecha de la llamada en formato ISO8601
- source
- número que origina la llamada
- destination
- número al que se hizo la llamada
- duration (opcional)
- duración de la llamada en segundos
- url (opcional)
- link para mostrar junto con la llamada que puede llevar a mayor detalle de la misma fuera de la aplicación de NiMbox
Para incorporar una nueva llamada se puede utilziar:
wget --user=jlopez --password=comic --no-check-certificate -qO- \ "https://host/direct/items/phonecalls/create?initiated=2012-08-23T12:20:33&source=7222&destination=02122853348&duration=45&url=http..."
éxito
En caso de que todo funcione correctamente se recibirá un json parecido a este:
{
"error": false,
"contactId": 12334,
"phonecallId": 3241
}
- contactId
- es el código del contacto al que se le conectó la llamada
- phonecallId
- es el código de la llamada
error
Si algo no funcionó se recibirá un json parecido a este:
{
"error": true,
"errors": {
"destination": "no se consiguió el número de destino en la base de datos"
}
}
Eliminar llamadas - /direct/items/phonecalls/delete
Para eliminar llamadas hay que realizar una solicitud a http://host/direct/items/phonecalls/delete con los siguientes parámetros:
- from
- fecha desde que se debe eliminar, en formato ISO8601
- to
- fecha hasta que se debe debe eliminar, en formato ISO8601
Ambas fechas son exclusivas y no deben incorporar las horas.
Para eliminar llamadas se puede utilizar:
wget --user=jlopez --password=comic --no-check-certificate -qO- \ "http://host/direct/items/phonecalls/delete?from=2012-08-23&to=2012-08-25"
El resultado de esta invocación resulta en un json con un parámetro error que puede ser true o false.
Forma programática de incorporar llamadas
Es posible modificar este script en perl para extraer las llamadas de la tabla cdr de asterisk e incorporarlas a NiMbox
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use LWP::UserAgent;
use LWP::ConnCache;
use URI::Escape;
use JSON;
# configuration
my $ASTERISK_HOST = "192.168.128.96";
my $ASTERISK_USER = "testing";
my $ASTERISK_PASSWORD = "testing";
my $NIMBOX_HOST = "192.168.1.98:8080";
my $NIMBOX_USER = "admin";
my $NIMBOX_PASSWORD = "admin";
# do the query
my $start = time;
my $handle = DBI->connect(
"DBI:mysql:asterisk;host=$ASTERISK_HOST",
$ASTERISK_USER,
$ASTERISK_PASSWORD);
my $sql = <<EOSQL;
SELECT
DATE_FORMAT(calldate, '%Y-%m-%dT%TZ') AS initiated,
src AS source,
dst AS destination,
duration AS duration,
CONCAT('http://192.168.128.96/recordings/', channel) AS url
FROM
cdr
WHERE
calldate >= '2010-01-01' AND LENGTH(dst) > 7
EOSQL
my $statement = $handle->prepare($sql);
$statement->execute();
# send the results
my $agent = LWP::UserAgent->new();
my $cache = LWP::ConnCache->new();
$cache->total_capacity([1]);
$agent->conn_cache($cache);
$agent->credentials("$NIMBOX_HOST", "NiMbox", $NIMBOX_USER, $NIMBOX_PASSWORD);
while (my $r = $statement->fetchrow_hashref()) {
print "Processing $r->{initiated} from $r->{source} to $r->{destination}... ";
# prepare the url
my $url = "http://$NIMBOX_HOST/direct/items/phonecalls/create";
$url = $url . "?initiated=$r->{initiated}";
$url = $url . "&source=$r->{source}";
$url = $url . "&destination=" . uri_escape(convertDestination($r->{'destination'}));
$url = $url . "&duration=$r->{duration}";
$url = $url . "&url=" . uri_escape($r->{'url'});
my $response = $agent->get($url);
my $json = decode_json($response->content);
if ($json->{'error'}) {
print "ERROR\n";
} else {
print "OK\n";
}
}
print "execution time: " . (time - $start) . "\n";
exit(0);
sub convertDestination {
my ($destination) = @_;
if ($destination =~ /^9/) {
$destination = substr($destination, 1);
if (length($destination) == 7) {
$destination = "0212" . $destination;
}
}
return $destination;
}