1. Presentation
We provide an Application Programming Interface (API) for developers to interact with our system.
This is the official documentation to communicate with this API.
1.1 Authentication
In all requests to the API the credentials should be sent as an argument of the request.
There are requests that you should use the public key (publicKey) and others where the secret key (apiKey) should be used.
http://api.plugchat.in/?apiKey=apiKey
http://api.plugchat.in/?publicKey=publicKey
The secret key (apiKey) and public key (publicKey) are available inside the Administration: Users Integration -> Integrate my Users
1.2 Response Format
The response of all requests will be in JSON format by default.
{ "code": 200, "txt": "OK", data: ... }
{ "code": 300, "txt": "Invalid apiKey", data: null }
{ "code": 301, "txt": "Malformed request", data: null }
{ "code": 400, "txt": "Missing parameter: user", data: null }
...
1.3 JSONP (Callback)
To receive the request in the JSONP format, add the callback parameter: ?callback=callback_method
- jQuery(document).ready(function(){
- var publicKey = 'publicKey';
- jQuery.getJSON('http://api.plugchat.in/?publicKey=' + encodeURIComponent(publicKey) + '&callback=?', function(data) {
- alert(data.code + ': ' + data.txt);
- });
- });
2. Integrating Users
In this section we explain how to integrate your site's users with the chat bar.
2.1 Checking User
The first step is to identify the user and check if it is already connected to the chat bar.
To do this identification is simple: check the cookies 'plugChatUserSessionId' and 'plugChatUserStatus'.
That's right, a cookie will be stored in the user with its unique identification and its connected status. It's not necessary any API requests here.
Cookie
plugChatUserSessionId
Return
null - do not have a valid user inside bar, or the bar is not open
userSessionId - unique user identification
Cookie
plugChatUserStatus
Return
null - do not have a valid user inside bar, or the bar is not open
1 - guest user
2 - user connected with facebook
3 - user connected with twitter
4 - user connected through API (from your site)
- $plugChatUserStatus = (isset($_COOKIE['plugChatUserStatus']) ? $_COOKIE['plugChatUserStatus'] : false;
- if ( !$plugChatUserStatus )
- // do not have a valid user inside bar, or the bar is not open
- } else {
- if ( $plugChatUserStatus == 1 ) {
- // guest user
- } else if ( $plugChatUserStatus == 4 ) {
- // user connected through API (from your site)
- } else {
- // user logged from external site (facebook, twitter, etc)
- }
- }
2.2 Login - ?action=login
Makes the user login in the chat bar.
Note: This step is necessary if a user is logged in your site and is not logged in the chat bar.
Request
http://api.plugchat.in/?action=login
Parameters
apiKey: {apiKey}
user: {cookie: plugChatUserSessionId}
nick: User's nick
avatar: User's avatar (URL) (Optional)
Example
http://api.plugchat.in/?action=login&apiKey=apiKey&user=plugChatUserSessionId&nick=James&avatar=http://static.plugchat.in/images/avatar.png
- $apiKey = 'apiKey';
- $userSessionId = (isset($_COOKIE['plugChatUserSessionId']) ? $_COOKIE['plugChatUserSessionId'] : false;
- $nick = 'Tiago Fischer';
- $avatar = 'http://static.plugchat.in/images/avatar.png';
- $url = 'http://api.plugchat.in/?action=login&apiKey=' . urlencode($apiKey) . '&user=' . urlencode($userSessionId) . '&nick=' . urlencode($nick) . '&avatar=' . urlencode($avatar);
- $answer = json_decode(file_get_contents($url), true);
- if ($answer['code'] != 200) {
- // ERROR!
- print_r($answer);
- } else {
- // OK!
- }
2.3 Logout - ?action=logout
Makes the user logout in the chat bar.
Note: This step is necessary if a user is not logged in your site and logged in the chat bar.
Request
http://api.plugchat.in/?action=logout
Parameters
apiKey: {apiKey}
user: {cookie: plugChatUserSessionId}
Example
http://api.plugchat.in/?action=logout&apiKey=apiKey&user=plugChatUserSessionId
- $apiKey = 'apiKey';
- $userSessionId = (isset($_COOKIE['plugChatUserSessionId']) ? $_COOKIE['plugChatUserSessionId'] : false;
- $url = 'http://api.plugchat.in/?action=logout&apiKey=' . urlencode($apiKey) . '&user=' . urlencode($userSessionId);
- $answer = json_decode(file_get_contents($url), true);
- if ($answer['code'] != 200) {
- // ERROR!
- print_r($answer);
- } else {
- // OK!
- }
2.4 Full Example
Here we show a full example of the code that must be placed on your site in order to integrate its user with the chat bar.
This code can be placed on all pages requests (recommended), because it performs the actions (login and logout) only when necessary.
Or it can be placed together with the login and logout actions from your site. (not recommended, because it will not work for users already authenticated on your site)
- $apiKey = 'apiKey';
- // identifies whether or not the user is logged in your site (replace with your code)
- $logged = true/false;
- // gets user information through cookies
- $userSessionId = isset($_COOKIE['plugChatUserSessionId']) ? $_COOKIE['plugChatUserSessionId'] : false;
- $userStatus = isset($_COOKIE['plugChatUserStatus']) ? $_COOKIE['plugChatUserStatus'] : false;
- if ( $logged ) {
- // the user is logged in your site! so let's check if it is not logged in the chat bar
- if ( $userStatus && $userStatus != 4 ) {
- // Login - ?action=login
- $nick = 'Tiago Fischer';
- $avatar = 'http://static.plugchat.in/images/avatar.png';
- $url = 'http://api.plugchat.in/?action=login&apiKey=' . urlencode($apiKey) . '&user=' . urlencode($userSessionId) . '&nick=' . urlencode($nick) . '&avatar=' . urlencode($avatar);
- $answer = json_decode(file_get_contents($url), true);
- if ($answer['code'] != 200) {
- // ERROR!
- //print_r($answer);
- } else {
- // OK!
- }
- }
- } else {
- // the user is not logged in your site! so let's check if it is logged in the chat bar
- if ( $userStatus && $userStatus == 4 ) {
- // Logout - ?action=logout
- $url = 'http://api.plugchat.in/?action=logout&apiKey=' . urlencode($apiKey) . '&user=' . urlencode($userSessionId);
- $answer = json_decode(file_get_contents($url), true);
- if ($answer['code'] != 200) {
- // ERROR!
- //print_r($answer);
- } else {
- // OK!
- }
- }
- }
3. Data
In this section is shown how to request data about your bar.
3.1 Connected users
Return the number of connected users.
Request
http://api.plugchat.in/?action=getonline
Parameters
publicKey: {publicKey}
Example
http://api.plugchat.in/?action=getonline&publicKey=publicKey
- jQuery(document).ready(function(){
- var publicKey = 'publicKey';
- jQuery.getJSON('http://api.plugchat.in/?publicKey=' + encodeURIComponent(publicKey) + '&callback=?', function(data) {
- if (data.code == 200) {
- jQuery('#getonline').html(data.data);
- }
- });
- });
4. Libraries
List of applications that already have implementations using this API.
