This page contains code snippets that demonstrate various Unified API requests that can be implemented in your App.
Requirements
At least Perl 5.8.8, JSON, HTTP::Request::Common, LWP::UserAgent, LWP::Protocol::https modules
Request Access Token for a Trusted App
#!/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.
#!/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 Call
Unified API can be tested using the Sandbox numbers. The example below uses the Create Simple PhoneCalls request to make the call.
#!/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.
#!/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
The example below demonstrates how you can use Unified API to park a party of an ongoing phone call. It uses the Park PhoneCalls request.
#!/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
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 Transfer PhoneCalls request.
#!/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
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 wav format and uses the StartRecording PhoneCalls request.
#!/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
The example below demonstrates how you can use Unified API to log in an agent to a queue. It uses the Update QueueAgents request.
#!/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 = '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
The example below demonstrates how you can use Unified API to list the status of an extension. It uses the List Presence request.
#!/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 ));
Except where otherwise noted, content in this space is licensed under a Creative Commons Attribution 4.0 International.