public async Task <IActionResult> Index([FromBody] PushTopic PushTopic)
        {
            try {
                HttpContent            HttpContent            = null;
                AuthenticationResponse AuthenticationResponse = await this.Authenticate(PushTopic);

                if (AuthenticationResponse.access_token == null)
                {
                    return(StatusCode(401, "Authentication with Salesforce failed!"));
                }
                else
                {
                    Payload Payload = new Payload();
                    Payload.config = new RoutingConfig(PushTopic.Config.appId, PushTopic.Config.davinciAccountId, PushTopic.Config.davinciProfileId, PushTopic.Config.davinciUsername, PushTopic.Config.davinciPassword);
                    if (PushTopic.EventType == "presence")
                    {
                        HttpContent = await this.ProcessPresence(Payload, AuthenticationResponse, PushTopic);
                    }
                    else if (PushTopic.EventType == "pending_service_routing")
                    {
                        HttpContent = await this.ProcessPendingWork(Payload, AuthenticationResponse, PushTopic);
                    }
                    else if (PushTopic.EventType == "agent_work")
                    {
                        HttpContent = await this.ProcessAgentWork(Payload, AuthenticationResponse, PushTopic);
                    }
                    var response = httpClient.PostAsync(appRoutingConfig.cloudRoutingUri, HttpContent);
                }
            } catch (Exception ex) {
                return(StatusCode(500, ex.Message));
            }
            return(Ok());
        }
        public async Task <AuthenticationResponse> Authenticate(PushTopic PushTopic)
        {
            try {
                HttpRequestMessage Authenticate =
                    new HttpRequestMessage(HttpMethod.Post, "https://login.salesforce.com/services/oauth2/token");
                var AuthString  = "grant_type=password&client_id=" + PushTopic.Config.ClientId + "&client_secret=" + PushTopic.Config.ClientSecret + "&username="******"&password="******"application/x-www-form-urlencoded");
                Authenticate.Content = httpContent;
                HttpResponseMessage AuthenticateResponse =
                    await httpClient.SendAsync(Authenticate);

                AuthenticationResponse Auth = JsonConvert.DeserializeObject <AuthenticationResponse> (
                    await AuthenticateResponse.Content.ReadAsStringAsync());
                return(Auth);
            } catch (Exception ex) {
                throw ex;
            }
        }
        public async Task <HttpContent> ProcessAgentWork(Payload Payload, AuthenticationResponse AuthenticationResponse, PushTopic PushTopic)
        {
            SalesforceObjects.AgentWork AgentWork = JsonConvert.DeserializeObject <SalesforceObjects.AgentWork> (JsonConvert.SerializeObject(PushTopic.SObject));
            string SObjectPrefix = AgentWork.WorkItemId.Substring(0, 3);
            Dictionary <string, string> PrefixList = await this.RetrieveSObjects(AuthenticationResponse);

            string   SObjectType = PrefixList.Where(Prefix => Prefix.Key == SObjectPrefix).FirstOrDefault().Value;
            WorkItem WorkItem    = new WorkItem(AgentWork.WorkItemId, SObjectType);

            PicklistValue[] Statuses = await this.RetrieveStatuses(AuthenticationResponse);

            string status = Statuses[AgentWork.Status].Label;
            User   User   = await RetrieveUser(AuthenticationResponse, AgentWork.UserId);

            string          UserName        = User.Name;
            AgentWorkStatus AgentWorkStatus = this.GetAgentStatus(status);
            AgentWork       _AgentWork      = new AgentWork(AgentWork.Id, AgentWork.CreatedDate.ToString(), AgentWork.CreatedDate.ToString(), "update", AgentWorkStatus, WorkItem, UserName, AgentWork.UserId);

            Payload.agentWork = _AgentWork;
            string Content = JsonConvert.SerializeObject(Payload);

            return(new StringContent(Content, Encoding.UTF8, "application/json"));
        }
        public async Task <HttpContent> ProcessPendingWork(Payload Payload, AuthenticationResponse AuthenticationResponse, PushTopic PushTopic)
        {
            SalesforceObjects.PendingServiceRouting PendingServiceRouting = JsonConvert.DeserializeObject <SalesforceObjects.PendingServiceRouting> (JsonConvert.SerializeObject(PushTopic.SObject));
            string SObjectPrefix = PendingServiceRouting.WorkItemId.Substring(0, 3);
            Dictionary <string, string> PrefixList = await this.RetrieveSObjects(AuthenticationResponse);

            string      SObjectType = PrefixList.Where(Prefix => Prefix.Key == SObjectPrefix).FirstOrDefault().Value;
            WorkItem    WorkItem    = new WorkItem(PendingServiceRouting.WorkItemId, SObjectType);
            PendingWork PendingWork = new PendingWork(PendingServiceRouting.Id, PendingServiceRouting.CreatedDate.ToString(), PendingServiceRouting.LastModifiedDate.ToString(), "create", WorkItem);

            Payload.pendingWork = PendingWork;
            string Content = JsonConvert.SerializeObject(Payload);

            return(new StringContent(Content, Encoding.UTF8, "application/json"));
        }
        public async Task <HttpContent> ProcessPresence(Payload Payload, AuthenticationResponse AuthenticationResponse, PushTopic PushTopic)
        {
            SalesforceObjects.Presence Presence = JsonConvert.DeserializeObject <SalesforceObjects.Presence> (JsonConvert.SerializeObject(PushTopic.SObject));
            SalesforceObjects.ServicePresenceStatus ServicePresenceStatus = await this.RetrievePresenceName(AuthenticationResponse, Presence);

            User User = await this.RetrieveUser(AuthenticationResponse, Presence.UserId);

            Payload.presence = new Presence(ServicePresenceStatus.MasterLabel, User.Name, Presence.UserId);
            string Content = JsonConvert.SerializeObject(Payload);

            return(new StringContent(Content, Encoding.UTF8, "application/json"));
        }