Excerpt |
---|
This page describes FacebookCallMe, a small App that allows anyone to place a call through a Facebook App. Download. |
Table of Contents | ||||
---|---|---|---|---|
|
Note |
---|
This demonstrative code SHOULD NOT be used in production. It is designed to simply show how an App interacts with UnifiedAPI. From this perspective, validations and error-checks aiming to demonstrate the most common mistakes are minimal and can be done easily. |
Overview
FacebookCallMe is an App written in PHP which allows anyone to place a call with a Facebook App using the UnifiedAPI of VoipNow 3.
It uses the PhoneCall resource exposed by UnifiedAPI and the following requests:
- Create PhoneCall - Creates phone calls using any given source and destination
- List PhoneCall - Fetches the list of phone calls that are in the system at a particular point in time
Installation
In order to be able to use the FacebookCallMe App, your system needs PHP 5.2 or higher and a Web server (Apache2, nginx, lighttpd, etc). Also, your VoipNow server must be at least version 3.0.0.
STEP 1: Download the files.
Download the archive, extract the files, and copy them to a directory that can be accessed over the Web (where your web server resides).
STEP 2: Proceed to allowing permissions and ownership rights.
You also need to give proper permissions and ownership to the files of the plugin - the plugin needs to be owned by the web server user and group.
For example, if we use httpd
as a webserver:
Code Block |
---|
chmod -R 755 <PATH_TO_FacebookCallMe> chwon -R httpd:httpd <PATH_TO_FacebookCallMe> |
STEP 3: Configure the App.
To be able to use the App, you must configure it. To do so, open the file config.php
in the <PATH_TO_FacebookCallMe>/interface/plib/config.php
and change the following settings:
Name | Description |
---|---|
VN_SERVER_IP | The IP or hostname of your VoipNow server; |
VN_EXTENSION | The extended number of a phone terminal extension. |
REDIRECT_URI | URL where Facebook redirects users when after logging in with facebook. Should be the URL where your index php is. Should be the same as in the App settings on facebook. |
OAUTH_APP_ID | OAuth App ID or key. A 32 char-long string used by the App to identify itself with the system. Visit this page to see how the OAuth APP ID can be obtained. |
OAUTH_APP_SECRET | OAuth App Secret. A 32 char-long secret used by the system to establish ownership of the App ID or key. Based on this pair, the system will recognize the new App and will allow it to generate an access token. Visit this page to see how the Secret can be obtained. |
APP_ID | The application ID obtained from Facebook. You cannot obtain it unless you have registered the app with Facebook. Click here for further details on how to make a Facebook App. |
APP_SECRET | The application secret obtained from Facebook. You cannot obtain it unless you have registered the app with Facebook. Click here for further details on how to make a Facebook App. |
Note |
---|
Please note that the newly created app will not work properly unless you check the App is trusted option in the Add App form. |
Behavior
User Interface
As soon as the setup process is complete, you can access the interface through the Facebook App. To do so, simply visit https://<IP>/<PATH_TO_FacebookCallMe>/interface/
. The page displays an input field with a button.
Enter a valid phone number and press call. The status of the call is displayed in this box. Errors are displayed as well. For a list of error messages, check the UnifiedAPI manual.
Internal Flow
Place a Phone Call
When the Call button is pressed, a Create PhoneCalls request is made through UnifiedAPI.
Code Block |
---|
$reqUrl = 'https://'.$config['VN_SERVER_IP'].'/uapi/phoneCalls/@me/simple'; $request = new cURLRequest(); $request->setMethod(cURLRequest::METHOD_POST); $request->setHeaders(array( 'Content-type' => 'application/json', 'Authorization' => $config['OAUTH_ACCESS_TOKEN'] )); $jsonRequest = array( 'extension' => $config['VN_EXTENSION'], 'phoneCallView' => array(array( 'source' => array($config['VN_EXTENSION']), 'destination' => $phoneNumber)) ); $request->setBody(json_encode($jsonRequest)); $response = $request->sendRequest($reqUrl); |
Once the request has been made, if no error occurred, a JSON encoded response is returned. It contains a link to the newly created call. Check the example at the Create PhoneCalls page.
The HTTP Request to make a call looks as shown below:
Code Block |
---|
POST /uapi/phoneCalls/@me/simple HTTP/1.1 HOST x.x.x.x Content Type: application/json Authorization: <OAUTH_ACCESS_TOKEN> { "extension":"<VN_EXTENSION>", "phoneCallView":[ { "source":["<VN_EXTENSION>"], "destination":["8887777"] "callerId":"Facebook CallMe <8887777>" } ] } |
List the Phone Call
Every 5 seconds, the javascript (lib.js) makes a request for information about the call. Such information is obtained through a UnifiedAPI request using the List PhoneCalls resource.
Code Block |
---|
$request = new cURLRequest(); $request->setMethod(cURLRequest::METHOD_GET); $request->setHeaders(array( 'Content-type' => 'application/json', 'Authorization' => $config['OAUTH_ACCESS_TOKEN'] )); $response = $request->sendRequest($url); |
The HTTP Request to retrieve a call looks as shown below:
Code Block |
---|
GET /uapi/phoneCalls/@me/<VN_EXTENSION>/<phoneCallID> HOST x.x.x.x Content Type: application/json Authorization: <OAUTH_ACCESS_TOKEN> |
4psarelated | ||
---|---|---|
| ||
Except where otherwise noted, content in this space is licensed under a Creative Commons Attribution 4.0 International.