0% found this document useful (0 votes)
444 views

Live Chat Script

This document contains a C# script that implements a live chat client using the LiveChat API. It includes functions to start a chat session by returning a unique ID, get pending messages by periodically requesting updates, send messages to the agent, and close the chat. Classes are defined to deserialize the JSON responses into structured response formats. The functions construct HTTP requests using the visitor ID, credentials, and appropriate API endpoint to perform the desired chat action.

Uploaded by

TechnoAzrael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
444 views

Live Chat Script

This document contains a C# script that implements a live chat client using the LiveChat API. It includes functions to start a chat session by returning a unique ID, get pending messages by periodically requesting updates, send messages to the agent, and close the chat. Classes are defined to deserialize the JSON responses into structured response formats. The functions construct HTTP requests using the visitor ID, credentials, and appropriate API endpoint to perform the desired chat action.

Uploaded by

TechnoAzrael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

using UnityEngine;

using System;
using System.Collections;
using System.Collections.Generic;
public class LiveChatScript : MonoBehaviour {
/*
Start chat<POST
This function creates new chat for a visitor and returns unique session
ID, which is required to send, receive or close a given chat.
Get pending messages<GET
Returns messages and events in a pending chat. Important note: In order
to persist the chat session, you must send that request every few seconds.
Otherwise, the chat will be closed after ~30 seconds.
if (lastMessageID.Length > 0)
{
uri += string.Format("&last_message_id={0}", lastMessage
ID);
}
Send message<POST
Sends a new message as the visitor.
Close chat<POST
Ends the chat as a visitor.

Program flow
StartScene
button start chat
onClick ->vytvo se http dotaz start chat ->odpovdtrue vytvo se nov www
objekt s daty z odpovdi(listener pro get_pending_messages) a loadlevel(chatScene)
false chybo
v hlka
chatscene
inputfield inputMessage -> onValueChanged ulo se text
button buttonQuit -> onClick ->
button buttonSend -> onClick -> vytvo se http dotaz send_message >odpovdtrue pidej rectChatArea jinak chybov hlka
rectView rectChatArea ->v update http dotaz getpending.. a zprac
ovn
*/
//tdy pro JSON
//pro send_message a close_chat
[Serializable]
public class BasicResponseFormat
{
public bool success;
}
//bez securedSessionId nelze nic
[Serializable]
public class StartChatResponseFormat
{
public string secureSessionId;

public bool banned;


}
//podtda GetPendening...
[Serializable]
public class AgentDetailsFormat
{
public string avatar;
public string jobTitle;
public string name;
}
[Serializable]
public class GetPendingMessagesResponseFormat
{
public AgentDetailsFormat agent;
public int messageId;
public string text;
public long timeStamp;
public string messageType;
public string userType;
}
//Struktura hlaviky HTTP poadavku
Dictionary<string, string> headers = new Dictionary<string, string>();
public string message = "Ahoj";
private string url = "";
//pihlaovac daje
private string credentials = "[email protected]:6fe2ccd0525a09f954b276b9cf
d285c7";
//e-mail : API key
private string urlRoot = "@api.livechatinc.com";
private string licenseId = "licence_id=7082571";
//your LiveChat account number. (the __lc.license param value).
private long visitorsId = 5863759023;
//unique ID of the visitor.It should be generated randomly on your s
ide.
//If you already have an ID of your user in your database, it can be
used as the visitor_id.
private string secureSessionId = "";
//v hlavice odpovdi na poadavek start chat
//sekce API
private string visitors = "/visitors/";
//pkazy API
private string chatStart = "/chat/start";
private string chatGetPendingMessages = "/chat/get_pending_messages";
private string chatSendMessage = "/chat/send_message";
private string chatClose = "/chat/close";
//optional paramaters
//start chat>
public string visitorName = "Client";
public string visitorEmail = "[email protected]";
private int groupNum;
//get_pending_messages>
private int lastMessageId;
//the ID of the last received message. Start where u end

IEnumerator WaitForRequest(WWW www)


{
yield return www;
// check for errors
if (www.error == null)
{
Debug.Log("WWW Ok!");
Debug.Log("responseHeaders.Count" + www.responseHeaders.Count);
if (www.responseHeaders.Count > 0)
{
foreach (KeyValuePair<string, string> entry in www.responseHeade
rs)
{
Debug.Log(entry.Value + "=" + entry.Key);
}
}
}
else
{
Debug.Log("WWW Error: " + www.error);
}
}
// Konvertuje JSON etzec do promnn tdy BasicResponseFormat
BasicResponseFormat DecodeBasicChatResponse(string json)
{
BasicResponseFormat decodedResponse = new BasicResponseFormat();
decodedResponse = JsonUtility.FromJson<BasicResponseFormat>(json);
return decodedResponse;
}
// Konvertuje JSON etzec do promnn tdy StartChatResponseFormat
StartChatResponseFormat DecodeStartChatResponse(string json)
{
StartChatResponseFormat decodedResponse = new StartChatResponseFormat();
decodedResponse = JsonUtility.FromJson<StartChatResponseFormat>(json);
return decodedResponse;
}
// Konvertuje JSON etzec do promnn tdy GetPendingMessagesResponseFormat
GetPendingMessagesResponseFormat DecodeGetPendingMessagesResponse(string jso
n)
{
GetPendingMessagesResponseFormat decodedResponse = new GetPendingMessage
sResponseFormat();
decodedResponse = JsonUtility.FromJson<GetPendingMessagesResponseFormat>
(json);
return decodedResponse;
}
void SendStartChatRequest(string message)
{
headers.Add("Content-Type", "application/x-www-form-urlencoded");
//ADD your Form Elements wich you want to transmit
WWWForm form = new WWWForm();
form.AddField("X-API-Version", 2);

byte[] rawData = form.data;


string adress = "https://" + credentials + urlRoot + visitors + visitors
Id + chatStart + "?";
string parameters = licenseId + "&welcome_message='" + message + "'";
url = adress + parameters;
Debug.Log(url);
WWW www = new WWW(url, rawData, headers);
StartCoroutine(WaitForRequest(www));
}
void GetPendingMessages(string message)
{
headers.Add("Content-Type", "application/x-www-form-urlencoded");
//ADD your Form Elements wich you want to transmit
WWWForm form = new WWWForm();
form.AddField("X-API-Version", 2);
byte[] rawData = form.data;
string adress = "https://" + credentials + urlRoot + visitors + visitors
Id + chatStart + "?";
string parameters = licenseId + "&welcome_message='" + message + "'";
url = adress + parameters;
Debug.Log(url);
WWW www = new WWW(url, rawData, headers);
StartCoroutine(WaitForRequest(www));
}
void SendMessage(string message)
{
headers.Add("Content-Type", "application/x-www-form-urlencoded");
//ADD your Form Elements wich you want to transmit
WWWForm form = new WWWForm();
form.AddField("X-API-Version", 2);
byte[] rawData = form.data;
string adress = "https://" + credentials + urlRoot + visitors + visitors
Id + chatStart + "?";
string parameters = licenseId + "&welcome_message='" + message + "'";
url = adress + parameters;
Debug.Log(url);
WWW www = new WWW(url, rawData, headers);
StartCoroutine(WaitForRequest(www));
}
void CloseChat(string message)
{
headers.Add("Content-Type", "application/x-www-form-urlencoded");
//ADD your Form Elements wich you want to transmit
WWWForm form = new WWWForm();
form.AddField("X-API-Version", 2);
byte[] rawData = form.data;
string adress = "https://" + credentials + urlRoot + visitors + visitors
Id + chatStart + "?";
string parameters = licenseId + "&welcome_message='" + message + "'";
url = adress + parameters;

Debug.Log(url);
WWW www = new WWW(url, rawData, headers);
StartCoroutine(WaitForRequest(www));
}

// Use this for initialization


void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
}

You might also like