Excerpt |
---|
This page contains code snippets that demonstrate various Unified API requests that can be implemented in your App using PERL. |
Requirements
At least Perl 5.8.8, JSON, HTTP::Request::Common, LWP::UserAgent, LWP::Protocol::https modules
Request Access Token for a Trusted App
PERL 5.10 or higher and modules perl-Crypt-SSLeay
, perl-Net-SSLeay
, perl-libwww-perl
, perl-JSON
are required.
Note |
---|
title | The code is on GitHub |
---|
|
Download here. Do not hesitate to contribute in order to make this example better - we welcome Pull Requests! |
Request access token for a trusted app
Download the code:
Code Block |
---|
|
curl "https://raw.githubusercontent.com/4psa/uapi-example-perl/master/01_req_token.pl" -o 01_req_token.pl -L |
Make a Sandbox call
Code Block |
---|
#!/usr/bin/perl
use strict;
use warnings;
use HTTP::Request::Common;
use LWP::UserAgent;
use JSON;
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
my $VN_SERVER_ADDRESS = 'CHANGEME';
my $APP_KEY = "CHANGEME";
my $APP_SECRET = "CHANGEME";
my $REDIRECT_URI = "CHANGEME";
my $TYPE = "unifiedapi";
my $uri = 'https://'.$VN_SERVER_ADDRESS.'/oauth/token.php';
my $req = HTTP::Request->new( 'POST' => $uri );
$req->header( 'Content-type' => 'application/x-www-form-urlencoded');
$req->content("&client_id=".$APP_KEY."&client_secret=".$APP_SECRET."&grant_type=client_credentials&type=".$TYPE."&redirect_uri=".$REDIRECT_URI);
my $lwp = LWP::UserAgent->new;
my $response = $lwp->request($req);
my $results = JSON->new;
print $results->pretty->encode($results->decode($response->decoded_content ));
|
Make a Call Between Two Public Numbers
The example below demonstrates how you can use Unified API to connect two public numbers, not registered with VoipNow. It uses the Create Simple PhoneCalls request.
Code Block |
---|
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use HTTP::Request::Common;
use LWP::UserAgent;
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
my $VN_SERVER_ADDRESS = 'CHANGEME';
my $API_ACCESS_TOKEN = 'CHANGEME';
my $EXTENSION_NUMBER = 'CHANGEME';
my $PUBLIC_NUMBER1 = 'CHANGEME';
my $PUBLIC_NUMBER2 = 'CHANGEME';
my $arr = {
'extension' => $EXTENSION_NUMBER,
'phoneCallView' => [{
'source',$PUBLIC_NUMBER1,
'destination',$PUBLIC_NUMBER2
}]
};
my $json = JSON->new->allow_nonref;
my $json_text = $json->encode($arr);
my $uri = 'https://'.$VN_SERVER_ADDRESS.'/uapi/phoneCalls/@me/simple';
my $req = HTTP::Request->new( 'POST' => $uri );
$req->header( 'Content-type' => 'application/json', 'Authorization' => 'Bearer ' . $API_ACCESS_TOKEN);
$req->content( $json_text );
my $lwp = LWP::UserAgent->new;
my $response = $lwp->request($req);
my $results = JSON->new;
print $results->pretty->encode($results->decode($response->decoded_content )); |
Make a Sandbox CallUnified API can be tested using the Sandbox numbers. The example below uses the Create Simple PhoneCalls request to make the call.
Code Block |
---|
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use HTTP::Request::Common;
use LWP::UserAgent;
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
my $VN_SERVER_ADDRESS = 'CHANGEME';
my $API_ACCESS_TOKEN = 'CHANGEME';
my $EXTENSION_NUMBER = 'CHANGEME';
my $SANDBOX_NUMBER = 'CHANGEME';
my $arr = {
'extension' => $EXTENSION_NUMBER,
'phoneCallView' => [{
'source',$EXTENSION_NUMBER,
'destination',$SANDBOX_NUMBER
}]
};
my $json = JSON->new->allow_nonref;
my $json_text = $json->encode($arr);
my $uri = 'https://'.$VN_SERVER_ADDRESS.'/uapi/phoneCalls/@me/simple';
my $req = HTTP::Request->new( 'POST' => $uri );
$req->header( 'Content-type' => 'application/json', 'Authorization' => 'Bearer ' . $API_ACCESS_TOKEN);
$req->content( $json_text );
my $lwp = LWP::UserAgent->new;
my $response = $lwp->request($req);
my $results = JSON->new;
print $results->pretty->encode($results->decode($response->decoded_content )); |
Barge in on an Existing Phone Call
The example below demonstrates how you can use Unified API to barge in on an ongoing phone call. It uses the BargeIn PhoneCalls request.
Code Block |
---|
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use HTTP::Request::Common;
use LWP::UserAgent;
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
my $VN_SERVER_ADDRESS = 'CHANGEME';
my $API_ACCESS_TOKEN = 'CHANGEME';
my $EXTENSION_NUMBER = 'CHANGEME';
my $EXTENSION_TO_BARGIN = 'CHANGEME';
my $CALLID = 'CHANGEME';
my $CALLERID = 'CHANGEME';
my $WFP = 'CHANGEME';
my $PHONECALLVIEW_ID = 'CHANGEME';
my $arr = {
'action' => "BargeIn",
'sendCallTo' => $EXTENSION_NUMBER,
'callerId' => $CALLERID,
'waitForPickup'=> $WFP,
'phoneCallViewId' => $PHONECALLVIEW_ID
};
my $json = JSON->new->allow_nonref;
my $json_text = $json->encode($arr);
my $uri = 'https://'.$VN_SERVER_ADDRESS.'/uapi/phoneCalls/@me/'.$EXTENSION_TO_BARGIN .'/'.$CALLID;
my $req = HTTP::Request->new( 'PUT' => $uri );
$req->header( 'Content-type' => 'application/json', 'Authorization' => 'Bearer ' . $API_ACCESS_TOKEN);
$req->content( $json_text );
my $lwp = LWP::UserAgent->new;
my $response = $lwp->request($req);
my $results = JSON->new;
print $results->pretty->encode($results->decode($response->decoded_content )); |
Park a Phone Call curl "https://raw.githubusercontent.com/4psa/uapi-example-perl/master/02_sandbox_call.pl" -o 02_sandbox_call.pl -L |
Park a phone call
The example below demonstrates how you can use Unified API to park a party of an ongoing phone call. It uses the the Park PhoneCalls request.
Code Block |
---|
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use HTTP::Request::Common;
use LWP::UserAgent;
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
my $VN_SERVER_ADDRESS = 'CHANGEME';
my $API_ACCESS_TOKEN = 'CHANGEME';
my $EXTENSION_NUMBER = 'CHANGEME';
my $CALLID = "CHANGEME";
my $PHONEVIEWCALL_ID = 'CHANGEME';
my $arr = {
'action' => 'Park',
'maxInParking'=>'30',
'phoneCallViewId' => $PHONEVIEWCALL_ID,
};
my $json = JSON->new->allow_nonref;
my $json_text = $json->encode($arr);
my $uri = 'https://'.$VN_SERVER_ADDRESS.'/uapi/phoneCalls/@me/'.$EXTENSION_NUMBER.'/'.$CALLID;
my $req = HTTP::Request->new( 'PUT' => $uri );
$req->header( 'Content-type' => 'application/json', 'Authorization' => 'Bearer ' . $API_ACCESS_TOKEN);
$req->content( $json_text );
my $lwp = LWP::UserAgent->new;
my $response = $lwp->request($req);
my $results = JSON->new;
print $results->pretty->encode($results->decode($response->decoded_content )); |
Transfer a Call to a Public Number curl "https://raw.githubusercontent.com/4psa/uapi-example-perl/master/03_park_call.pl" -o 03_park_call.pl -L |
Transfer a call to a public number
The example below demonstrates how you can use Unified API to transfer a call to a public number, not registered with VoipNow. It uses the the Transfer PhoneCalls request.
Code Block |
---|
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use HTTP::Request::Common;
use LWP::UserAgent;
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
my $VN_SERVER_ADDRESS = 'CHANGEME';
my $API_ACCESS_TOKEN = 'CHANGEME';
my $PUBLIC_PHONE_NUMBER = 'CHANGEME';
my $PHONECALLVIEW_ID = 'CHANGEME';
my $CALLID = "CHANGEME";
my $arr = {
'action' => "Transfer",
'sendCallTo' => $PUBLIC_PHONE_NUMBER,
'phoneCallViewId' => $PHONECALLVIEW_ID
};
my $json = JSON->new->allow_nonref;
my $json_text = $json->encode($arr);
my $uri = 'https://'.$VN_SERVER_ADDRESS.'/uapi/phoneCalls/@me/@self/'.$CALLID;
my $req = HTTP::Request->new( 'PUT' => $uri );
$req->header( 'Content-type' => 'application/json', 'Authorization' => 'Bearer ' . $API_ACCESS_TOKEN);
$req->content( $json_text );
my $lwp = LWP::UserAgent->new;
my $response = $lwp->request($req);
my $results = JSON->new;
print $results->pretty->encode($results->decode($response->decoded_content )); |
Record an Ongoing Conversation curl "https://raw.githubusercontent.com/4psa/uapi-example-perl/master/04_transfer_call.pl" -o 04_transfer_call.pl -L |
Record an ongoing conversation
The example below demonstrates how you can use Unified API to record an ongoing conversation. This action is possible for phone numbers that are registered with VoipNow. The recording is saved in the the wav format and uses the StartRecording PhoneCalls request.
Code Block |
---|
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use HTTP::Request::Common;
use LWP::UserAgent;
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
my $VN_SERVER_ADDRESS = 'CHANGEME';
my $API_ACCESS_TOKEN = 'CHANGEME';
my $EXTENSION_NUMBER = "CHANGEME";
my $CALLID = "CHANGEME";
my $PHONECALLVIEW_ID = 'CHANGEME';
my $arr = {
'action' => 'StartRecording',
'format' => 'wav',
'phoneCallViewId' => $PHONECALLVIEW_ID,
};
my $json = JSON->new->allow_nonref;
my $json_text = $json->encode($arr);
my $uri = 'https://'.$VN_SERVER_ADDRESS.'/uapi/phoneCalls/@me/'.$EXTENSION_NUMBER.'/'.$CALLID;
my $req = HTTP::Request->new( 'PUT' => $uri );
$req->header( 'Content-type' => 'application/json', 'Authorization' => 'Bearer ' . $API_ACCESS_TOKEN);
$req->content( $json_text );
my $lwp = LWP::UserAgent->new;
my $response = $lwp->request($req);
my $results = JSON->new;
print $results->pretty->encode($results->decode($response->decoded_content )); |
Log in an Agent to a Queue curl "https://raw.githubusercontent.com/4psa/uapi-example-perl/master/05_record_call.pl" -o 05_record_call.pl -L |
Log in an agent to a queue
The example below demonstrates how you can use Unified API to log in an agent to a queue. It uses the Update QueueAgents request.
Code Block |
---|
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use HTTP::Request::Common;
use LWP::UserAgent;
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
my $VN_SERVER_ADDRESS = 'CHANGEME';
my $API_ACCESS_TOKEN = 'CHANGEME';
my $QUEUE_NUMBER = 'CHANGEME';
my $AGENT_NUMBER = 'CHANGEME';
my $arr = {
'status' => 1,
};
my $json = JSON->new->allow_nonref;
my $json_text = $json->encode($arr);
my $uri = 'curl "https://'.$VN_SERVER_ADDRESS.'/uapi/extensions/@me/'.$QUEUE_NUMBER.'/queue/agents/'.$AGENT_NUMBER;
my $req = HTTP::Request->new( 'PUT' => $uri );
$req->header( 'Content-type' => 'application/json', 'Authorization' => 'Bearer ' . $API_ACCESS_TOKEN);
$req->content( $json_text );
my $lwp = LWP::UserAgent->new;
my $response = $lwp->request($req);
my $results = JSON->new;
print $results->pretty->encode($results->decode($response->decoded_content )); |
List the Registration Status of an Extension raw.githubusercontent.com/4psa/uapi-example-perl/master/06_agent_login.pl" -o 06_agent_login.pl -L |
List the registration status of an extension
The example below demonstrates how you can use Unified API to list the status of an extension. It uses the List Presence request.
Code Block |
---|
curl "https://raw.githubusercontent.com/4psa/uapi-example-perl/master/07_list_registration.pl" -o 07_list_registration.pl -L |
Barge in on an existing phone call
The example below demonstrates how you can use Unified API to barge in on an ongoing phone call. It uses the BargeIn PhoneCalls request.
Code Block |
---|
curl "https://raw.githubusercontent.com/4psa/uapi-example-perl/master/08_barge_in.pl" -o 08_barge_in.pl -L#!/usr/bin/perl
use strict;
use warnings;
use HTTP::Request::Common;
use LWP::UserAgent;
use JSON;
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
my $VN_SERVER_ADDRESS = 'CHANGEME';
my $API_ACCESS_TOKEN = 'CHANGEME';
my $EXTENSION_NUMBER = 'CHANGEME';
my $uri = 'https://'.$VN_SERVER_ADDRESS.'/uapi/extensions/@me/'.$EXTENSION_NUMBER.'/presence';
my $req = HTTP::Request->new( 'GET' => $uri );
$req->header( 'Content-type' => 'application/json', 'Authorization' => 'Bearer ' . $API_ACCESS_TOKEN);
my $lwp = LWP::UserAgent->new;
my $response = $lwp->request($req);
my $results = JSON->new;
print $results->pretty->encode($results->decode($response->decoded_content )); |