You are on page 1of 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 = "havlat82@centrum.cz: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 = "client@email.com";
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