Esempio n. 1
0
        /// <summary>
        /// End a chat.
        /// </summary>
        /// <param name="chatContext">Reference to the chat to end</param>
        /// <returns>True if chat successfully ended</returns>
        public async Task <bool> EndChat(ChatContext chatContext)
        {
            string path          = $"{chatContext.SessionUrl}/events.json?v=1&appKey={sdkConfiguration.AppKey}";
            var    stringPayload = await Task.Run(() => JsonConvert.SerializeObject(new LPEnd()));

            var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync(path, httpContent);

            return(response.IsSuccessStatusCode);
        }
Esempio n. 2
0
        /// <summary>
        /// Request a new chat, routing to an agent based on the criteria specified in the provided <c>ChatSpec</c>.
        /// A <c>ChatContext</c> will be returned, this is used to reference the chat when making subsequent calls to the SDK.
        /// Live Assist will queue the chat request until a suitable agent is available, if Live Assist does not recognise
        /// the criteria specified by the <c>ChatSpec</c>, or a server error occurs, a <c>ChatException</c> will be thrown
        /// </summary>
        /// <param name="chatSpec"> Routing criteria and chat configuration</param>
        /// <returns>A <c>ChatContext</c> referencing the requested chat</returns>
        public async Task <ChatContext> RequestChat(ChatSpec chatSpec)
        {
            // TODO wrap in a try catch ?
            // TODO refactor out the posing of the chatRequest
            BaseUri baseUri = await GetBaseUriAsync();

            String uri = "https://" + baseUri.baseURI + "/api/account/" +
                         sdkConfiguration.AccountNumber + "/chat/request.json?v=1&appKey=" + sdkConfiguration.AppKey;

            ChatRequest chatRequest   = new ChatRequest(chatSpec);
            var         stringPayload = await Task.Run(() => JsonConvert.SerializeObject(chatRequest));

            var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync(uri, httpContent);

            if (response.StatusCode == HttpStatusCode.Created)
            {
                Uri         sessionUri   = response.Headers.Location;
                string      nextEventUri = $"{sessionUri}.json?v=1&appKey={sdkConfiguration.AppKey}";
                ChatContext chatContext  = new ChatContext(sessionUri.AbsoluteUri, nextEventUri);

                if (chatSpec.VisitorName != null && chatSpec.VisitorName.Length > 0)
                {
                    await SetVisitorName(chatSpec.VisitorName, chatContext);
                }

                if (chatSpec.ContextData != null)
                {
                    var info = await GetInfo(chatContext);

                    string context = chatSpec.ContextData(info.Info.rtSessionId.ToString());
                    await PostContext(context, chatContext);
                }

                return(chatContext);
            }
            else
            {
                throw new ChatException($"Failed to start chat, received HTTP response code {response.StatusCode} from chat server");
            }
        }
Esempio n. 3
0
        private async Task SetVisitorName(string visitorName, ChatContext chatContext)
        {
            string path = $"{chatContext.SessionUrl}/info/visitorName.json?v=1&appKey={sdkConfiguration.AppKey}";

            var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(new LPVisitorName()
            {
                VisitorName = visitorName
            }));

            var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");

            httpContent.Headers.Add("X-HTTP-Method-Override", "PUT");
            HttpResponseMessage response = await client.PostAsync(path, httpContent);

            if (!response.IsSuccessStatusCode)
            {
                response.Dispose();
                throw new ChatException($"Failed to set visitor name, received HTTP response code {response.StatusCode} from chat server");
            }
            response.Dispose();
        }
Esempio n. 4
0
        private async Task <LPInfo> GetInfo(ChatContext chatContext)
        {
            string path = $"{chatContext.SessionUrl}/info.json?v=1&appKey={sdkConfiguration.AppKey}";
            HttpResponseMessage response = await client.GetAsync(path);

            LPInfo info = null;

            if (response.IsSuccessStatusCode)
            {
                string content = await response.Content.ReadAsStringAsync();

                info = JsonConvert.DeserializeObject <LPInfo>(content);
                response.Dispose();
                return(info);
            }
            else
            {
                response.Dispose();
                throw new ChatException($"Failed to get chat info, received HTTP response code {response.StatusCode} from chat server");
            }
        }
Esempio n. 5
0
        private async Task PostContext(string context, ChatContext chatContext)
        {
            string path = $"https://{sdkConfiguration.ContextDataHost}:{sdkConfiguration.ContextDataPort}{sdkConfiguration.ContextDataPath}";

            var contextRequest = new CXContext()
            {
                AccountId   = sdkConfiguration.AccountNumber,
                ContextData = context
            };

            var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(contextRequest));

            var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync(path, httpContent);

            if (!response.IsSuccessStatusCode)
            {
                response.Dispose();
                throw new ChatException($"Failed to post contextData, received HTTP response code {response.StatusCode} from context server");
            }
            response.Dispose();
        }