Ejemplo n.º 1
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");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// For a given <c>ChatSpec</c>, return <c>true</c> if agents are available for chat. <c>false</c> otherwise.
        /// </summary>
        /// <param name="chatSpec">Reference to the chat to end</param>
        /// <returns><c>true</c> if agents are available for chat. <c>false</c> otherwise</returns>
        public async Task <bool> GetAvailability(ChatSpec chatSpec)
        {
            BaseUri baseUri = await GetBaseUriAsync();

            var uri = $"https://{baseUri.baseURI}/api/account/{sdkConfiguration.AccountNumber}/chat/availability?v=1&appKey={sdkConfiguration.AppKey}&skill={chatSpec.Skill}";

            HttpResponseMessage response = await client.GetAsync(uri);

            LPAvailability availability = null;

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

                availability = JsonConvert.DeserializeObject <LPAvailability>(content);
                response.Dispose();
                return(availability.Availability);
            }
            else
            {
                response.Dispose();
                throw new ChatException($"Failed to get agent availability, received HTTP response code {response.StatusCode} from chat server");
            }
        }