Access Keys:
Skip to content (Access Key - 0)
Live Chat

Get Chat Visitors and Operators Information in ASP.NET Application with Banckle.Chat for Cloud API

Skip to end of metadata
Go to start of metadata
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Get Chat Visitors and Operators Information in ASP.NET Application with Banckle.Chat for Cloud API

This article explains how to develop a simple application to get Banckle Chat Visitors’ information using Banckle.Chat for Cloud API. Our basic purpose of this article is to explain how easily anyone can work with Banckle Cloud APIs in their ASP.NET applications.

Contents

Prerequisites
Application Details
Get Authorized Token
Create New Session
Get Online Visitors' Information
Get Queued Visitors' Information
Get Operators' Information
Download Details

Prerequisites

In order to access the cloud API for Banckle.Chat, you must have:

  1. A valid Banckle Account.
  2. A valid Banckle API key.
  3. A Valid Banckle Authorized Token. https://apps.banckle.com/api/v2/doc/#!/auth/login_POST
  4. Permission to access Banckle.Chat API service.

You can easily access Banckle.Chat API by using our Swagger API Explorer.

Application Details

This application gives a working solution of how to get the visitors and operators information using Banckle.Chat for Cloud API. The application is developed using ASP.NET and we will get the details regarding online visitors, visitors in queue and details of operators by using a valid API Key and Authorized Token. Following is the screenshot of the simple GUI of the application.

Get Authorized Token

As per the prerequisites, we need to provide a valid key and to get the authorization token we will pass the email address and password as shown in the above picture to work with Banckle.Chat for Cloud API.

Following method is used to get the authorized token based on the email address and password:

protected void Get_Authorized_Token()
{

    // Create JSON object to be sent
    JObject request_json = new JObject();
    request_json.Add("userEmail", new JValue(txtEmail.Text));
    request_json.Add("password", new JValue(txtPassword.Text));
    string request_body = request_json.ToString();

    string request_url = "https://apps.banckle.com/api/v2/auth/token";

    // Create request object
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(request_url));
    // Add custom headers for authentication
    request.Headers.Add("banckle-client-api-key", api_key);
    // request.Headers.Add("X-Resource", session_resource);
    // Set request parameters
    request.Method = "POST";
    request.ContentType = "application/json";
    request.ContentLength = request_body.Length;
    // Add the required JSON to request object
    StreamWriter request_writer = new StreamWriter(request.GetRequestStream());
    request_writer.Write(request_body);
    request_writer.Close();

    // Retrieve response object
    WebResponse response = request.GetResponse();
    Stream response_stream = response.GetResponseStream();
    // Fetch response as string
    string response_body = new StreamReader(response_stream).ReadToEnd();
    // Close response stream to release resources
    response_stream.Close();

    // Parse response as JSON object
    JObject response_json = JObject.Parse(response_body);

    auth_token = response_json["authorization"]["token"].ToString();

}

Create New Session

After providing a valid key and getting an authorized token, a new session is created which is used to get the information from Cloud. We need to pass a valid token to get the session information. Following method is used to get the session resource:

protected string Generate_Session()
{

    string request_url = "http://chat.banckle.com/v3/connect?token=" + auth_token;

    // Create request object
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(request_url));
    // Add custom headers for authentication
    request.Headers.Add("banckle-client-api-key", api_key);

    // Retrieve response object
    WebResponse response = request.GetResponse();
    Stream response_stream = response.GetResponseStream();
    // Fetch response as string
    string response_body = new StreamReader(response_stream).ReadToEnd();
    // Close response stream to release resources
    response_stream.Close();

    // Parse response as JSON array
    JObject session = JObject.Parse(response_body);
    // Fetch session resource from JSON response
    string resource = session["return"]["resource"].ToString();
    return resource;

}

Get Online Visitors' Information

After getting the session resource, now we will get the details of the online visitors. Following method is used to get the visitors’ information using the Cloud API.

protected void Get_Visitors()
 {
     string request_url = "https://chat.banckle.com/v3/visitors/browsing.js";

     // Create request object
     WebRequest request = HttpWebRequest.Create(new Uri(request_url));

     // Add custom headers for authentication
     request.Headers.Add("banckle-client-api-key", api_key);
     request.Headers.Add("X-Resource", session_resource);

     // Retrieve response object
     WebResponse response = request.GetResponse();
     Stream response_stream = response.GetResponseStream();
     // Fetch response as string
     string response_body = new StreamReader(response_stream).ReadToEnd();
     // Close response stream to release resources
     response_stream.Close();

     // Parse response as JSON object
     JObject response_json = JObject.Parse(response_body);

     VisitorsResponse vresp = JsonConvert.DeserializeObject<VisitorsResponse>(response_json.ToString());

     if (vresp.Return.Count > 0)
     {
         lblTotalV.Text = vresp.Return.Count.ToString();
         GridView1.DataSource = vresp.Return;
         GridView1.DataBind();
     }
 }

As you can see, we are getting the JSON response and then we are deserializing the response (using simple VisitorsResponse class) and binding the data with our first grid view control to display Online Visitors information.

Get Queued Visitors' Information

After getting the details of online visitors, now we will use the API call to get the visitors who are in queue. For that, we have used the following method:

protected void Get_Queued_Visitors()
        {

            string request_url = "https://chat.banckle.com/v3/visitors/queue.js";

            // Create request object
            WebRequest request = HttpWebRequest.Create(new Uri(request_url));
            // Add custom headers for authentication
            request.Headers.Add("banckle-client-api-key", api_key);
            request.Headers.Add("X-Resource", session_resource);

            // Retrieve response object
            WebResponse response = request.GetResponse();
            Stream response_stream = response.GetResponseStream();
            // Fetch response as string
            string response_body = new StreamReader(response_stream).ReadToEnd();
            // Close response stream to release resources
            response_stream.Close();

            // Parse response as JSON object
            JObject response_json = JObject.Parse(response_body);

            VisitorsResponse vresp = JsonConvert.DeserializeObject<VisitorsResponse>(response_json.ToString());

            if (vresp.Return.Count > 0)
            {
                lblTotalQ.Text = vresp.Return.Count.ToString();
                GridView2.DataSource = vresp.Return;
                GridView2.DataBind();
            }

        }

The above method will get the details of visitors in queue and will display in our second grid view control.

Get Operators' Information

Now, after getting the visitors’ information, we will get the information of operators. For that we will use the below method:

public void Get_Operator_Info()
{
    string online_only = "false";
    string request_url = "https://chat.banckle.com/v3/operators.js?online=" + online_only;

    // Create request object
    WebRequest request = HttpWebRequest.Create(new Uri(request_url));
    // Add custom headers for authentication
    request.Headers.Add("banckle-client-api-key", api_key);
    request.Headers.Add("X-Resource", session_resource);

    // Retrieve response object
    WebResponse response = request.GetResponse();
    Stream response_stream = response.GetResponseStream();
    // Fetch response as string
    string response_body = new StreamReader(response_stream).ReadToEnd();
    // Close response stream to release resources
    response_stream.Close();

    // Parse response as JSON object
    JObject response_json = JObject.Parse(response_body);

    OperatorsResponse vresp = JsonConvert.DeserializeObject<OperatorsResponse>(response_json.ToString());

    if (vresp.Return.Count > 0)
    {
        lblTotalOp.Text = vresp.Return.Count.ToString();
        GridView3.DataSource = vresp.Return;
        GridView3.DataBind();
    }
}

In the above method we get the information for both online and offline operators. We have used the OperatorsResponse class to get the deserialized response of JSON and bound the data with our third grid view control.

That's it, by using these simple Banckle.Chat for Cloud API calls; we are able to get the detailed information of visitors and operators. We have only displayed a limited information regarding visitors to keep the application simple. You can easily modify the VisitorsResponse class to get more detailed information e.g. City, State, Time Zone, Last Accessed time etc. as required.

Download Information

Complete source code and the working sample is available at Codeplex. You can simply download the application; provide a valid token and API Key and press "Get Info" button to get the information regarding your visitors and operators.

Adaptavist Theme Builder (4.2.0) Powered by Atlassian Confluence 3.3, the Enterprise Wiki