Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

This page contains code snippets that demonstrate various Unified API requests that can be implemented in your App. Download.

Table of Contents
maxLevel2

Requirements

Python 2.7.3+ with SSL support is required.

Request Access Token for a Trusted App

Code Block
languagepy
import httplib
import json

# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS = 'CHANGEME'
APP_KEY = 'CHANGEME'
APP_SECRET = 'CHANGEME'
REDIRECT_URI = 'https://' + VN_SERVER_ADDRESS + '/ouath/token.php'
TYPE = 'unifiedapi'
URI = 'https://' + VN_SERVER_ADDRESS + '/oauth/token.php' 

# Setup the requests parameters
headers = {"Content-type": "application/x-www-form-urlencoded"}
content = "&client_id=" + APP_KEY + "&client_secret=" + APP_SECRET + "&grant_type=client_credentials&type=" + TYPE + "&redirect_uri=" + REDIRECT_URI
conn = httplib.HTTPSConnection(VN_SERVER_ADDRESS)
conn.request("POST",URI, content, headers)

# Parses the JSON response
response = conn.getresponse()
print json.dumps(json.loads(response.read()), indent=4)

This request will have the following output:

Code Block
languagepy
{
    "access_token": "1|W-rhVICLEV-NsP3AAEN-mNCDEBHz-7bf|1|1402415213|x-5nQMXISV_XGTS6Yg2_kOB2TNnH_S0d",
    "token_type": "Bearer",
    "expires_in": 7200
}

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
languagepython
import httplib
import json

# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS   = 'CHANGEME'
API_ACCESS_TOKEN    = 'CHANGEME'
EXTENSION_NUMBER    = 'CHANGEME'
PUBLIC_NUMBER1      = 'CHANGEME'
PUBLIC_NUMBER2      = 'CHANGEME'

# Setup the requests parameters
data = {
    "extension": EXTENSION_NUMBER,
    "phoneCallView" : [{
        "source" : PUBLIC_NUMBER1,
        "destination": PUBLIC_NUMBER2            
    }]
}

headers = {"Content-type": "application/json", "Authorization": "Bearer " + API_ACCESS_TOKEN}

# Makes a HTTP POST request using SSL
conn = httplib.HTTPSConnection(VN_SERVER_ADDRESS)
conn.request("POST", "/uapi/phoneCalls/@me/simple", json.dumps(data), headers)

# Parses the JSON response
response = conn.getresponse()
print json.dumps(json.loads(response.read()), indent=4) 

Make a Sandbox Call

Code Block
languagepython
import httplib
import json

# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS 	= 'CHANGEME'
API_ACCESS_TOKEN 	= 'CHANGEME'
EXTENSION_NUMBER    = 'CHANGEME'
SANDBOX_NUMBER 		= 'CHANGEME'

# Setup the requests parameters
data = {
	"extension": EXTENSION_NUMBER,
	"phoneCallView" : [{
		"source" : EXTENSION_NUMBER,
		"destination": SANDBOX_NUMBER			
	}]
}
headers = {"Content-type": "application/json", "Authorization": "Bearer " + API_ACCESS_TOKEN}

# Makes a HTTP POST request using SSL
conn = httplib.HTTPSConnection(VN_SERVER_ADDRESS)
conn.request("POST", "/uapi/phoneCalls/@me/simple", json.dumps(data), headers)

# Parses the JSON response
response = json.loads(conn.getresponse().read())
print json.dumps(response, indent=4)

Park a Phone Call

Anchor
demo-ph-park
demo-ph-park

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.

Code Block
languagepython
'''
4PSA VoipNow UnifiedAPI Example: Phone Call Parking
This script will transfer a public phone call.
@version 1.0.0
@license released under GNU General Public License
@copyright (c) 2013 4PSA. (www.4psa.com). All rights reserved.
@link http://wiki.4psa.com
'''
import httplib
import json
# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS       = 'CHANGEME'
API_ACCESS_TOKEN        = 'CHANGEME'
PHONECALL_ID            = 'CHANGEME'
PHONECALLVIEW_ID        = 'CHANGEME'
# Setup the requests parameters
data = {
    'action': 'Park',
    'phoneCallViewId': PHONECALLVIEW_ID
}
headers = {"Content-type": "application/json", "Authorization": "Bearer " + API_ACCESS_TOKEN}
# Makes a HTTP PUT request using SSL
conn = httplib.HTTPSConnection(VN_SERVER_ADDRESS)
conn.request("PUT", "/unifiedapi/phoneCalls/@me/@self/"+PHONECALL_ID, json.dumps(data), headers)
# Parses the JSON response
response = conn.getresponse()
print json.dumps(json.loads(response.read()), indent=4)

Transfer a Call to a Public Number

Anchor
demo-ph-xfer
demo-ph-xfer

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.

Code Block
languagepython
import httplib
import json

# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS            = 'CHANGEME'
API_ACCESS_TOKEN             = 'CHANGEME'
PHONECALL_ID                 = 'CHANGEME'
PHONECALLVIEW_ID             = 'CHANGEME'
TRANSFER_DESTINATION_NUMBER  = 'CHANGEME'

# Setup the requests parameters
data = {
    'action': 'Transfer',
    'sendCallTo': TRANSFER_DESTINATION_NUMBER,
    'phoneCallViewId': PHONECALLVIEW_ID
}
headers = {"Content-type": "application/json", "Authorization": "Bearer " + API_ACCESS_TOKEN}

# Makes a HTTP PUT request using SSL
conn = httplib.HTTPSConnection(VN_SERVER_ADDRESS)
conn.request("PUT", "/uapi/phoneCalls/@me/@self/"+PHONECALL_ID, json.dumps(data), headers)

# Parses the JSON response
response = conn.getresponse()
print json.dumps(json.loads(response.read()), indent=4) 

Record an Ongoing Conversation

Anchor
demo-ph-rec
demo-ph-rec

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.

Code Block
languagepython
import httplib
import json

# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS       = 'CHANGEME'
API_ACCESS_TOKEN        = 'CHANGEME'
PHONECALL_ID            = 'CHANGEME'
PHONECALLVIEW_ID        = 'CHANGEME'

# Setup the requests parameters
data = {
    'action': 'StartRecording',
    'format': 'wav',
    'phoneCallViewId': PHONECALLVIEW_ID
}
headers = {"Content-type": "application/json", "Authorization": "Bearer " + API_ACCESS_TOKEN}

# Makes a HTTP PUT request using SSL
conn = httplib.HTTPSConnection(VN_SERVER_ADDRESS)
conn.request("PUT", "/uapi/phoneCalls/@me/@self/"+PHONECALL_ID, json.dumps(data), headers)

# Parses the JSON response
response = conn.getresponse()
print json.dumps(json.loads(response.read()), indent=4)

Log in an Agent to a Queue

Anchor
demo-queue-ag
demo-queue-ag

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
languagepython
import httplib
import json

# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS       = 'CHANGEME'
API_ACCESS_TOKEN        = 'CHANGEME'
QUEUE_NUMBER            = 'CHANGEME'
AGENT_NUMBER            = 'CHANGEME'

# Setup the requests parameters
data = {'status': '1'}
headers = {"Content-type": "application/json", "Authorization": "Bearer " + API_ACCESS_TOKEN}

# Makes a HTTP PUT request using SSL
conn = httplib.HTTPSConnection(VN_SERVER_ADDRESS)
conn.request("PUT", "/uapi/extensions/@me/"+QUEUE_NUMBER+"/queue/agents/"+AGENT_NUMBER, json.dumps(data), headers)

# Parses the JSON response
response = conn.getresponse()
print json.dumps(json.loads(response.read()), indent=4)

List the Registration Status of an Extension

Anchor
demo-ext-pres
demo-ext-pres

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
languagepython
import httplib
import json

# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS       = 'CHANGEME'
API_ACCESS_TOKEN        = 'CHANGEME'
EXTENSION_NUMBER        = 'CHANGEME'

# Setup the requests parameters
headers = {"Content-type": "application/json", "Authorization": "Bearer " + API_ACCESS_TOKEN}

# Makes a HTTP GET request using SSL
conn = httplib.HTTPSConnection(VN_SERVER_ADDRESS)
conn.request("GET", "/uapi/extensions/@me/"+EXTENSION_NUMBER+"/presence", None, headers)

# Parses the JSON response
response = conn.getresponse()
print json.dumps(json.loads(response.read()), indent=4)

Except where otherwise noted, content in this space is licensed under a Creative Commons Attribution 4.0 International.