/// <summary>
        /// This method allows the user to share the flow app in salesforce with their friends.
        /// </summary>
        public MessageAPI PostNotification(INotifier notifier, IAuthenticatedWho authenticatedWho, String oauthToken, ServiceRequestAPI serviceRequest, String endpointUrl, String flowLink, ChatterPostedMessage chatterPostedMessage)
        {
            HttpResponseMessage      httpResponseMessage      = null;
            HttpClient               httpClient               = null;
            MediaTypeFormatter       jsonFormatter            = null;
            MultipartFormDataContent multipartFormDataContent = null;
            ChatterMessage           chatterMessage           = null;
            ChatterAttachmentLink    chatterAttachmentLink    = null;
            MessageAPI               message = null;
            String chatterBaseUrl            = null;
            String adminEmail = null;

            if (oauthToken == null ||
                oauthToken.Trim().Length == 0)
            {
                throw new ArgumentNullException("BadRequest", "OAuthToken cannot be null or blank.");
            }

            if (endpointUrl == null ||
                endpointUrl.Trim().Length == 0)
            {
                throw new ArgumentNullException("BadRequest", "EndpointUrl cannot be null or blank.");
            }

            // Grab the values necessary to post the message over to chatter
            chatterBaseUrl = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_CHATTER_BASE_URL, serviceRequest.configurationValues, true);
            adminEmail     = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_ADMIN_EMAIL, serviceRequest.configurationValues, true);

            // Now we can create the multipart form we're going to post over to salesforce
            multipartFormDataContent = new MultipartFormDataContent();

            if (flowLink != null &&
                flowLink.Trim().Length > 0)
            {
                // We also add the link to the app so the user has it
                chatterAttachmentLink = new ChatterAttachmentLink();
                chatterAttachmentLink.AttachmentType = "Link";
                chatterAttachmentLink.Url            = flowLink;
                chatterAttachmentLink.UrlName        = "Link to ManyWho Flow";

                chatterPostedMessage.Attachment = chatterAttachmentLink;
            }

            // We enclose the request in a for loop to handle http errors
            for (int i = 0; i < SalesforceHttpUtils.MAXIMUM_RETRIES; i++)
            {
                try
                {
                    // Create a new client object
                    httpClient = SalesforceHttpUtils.CreateHttpClient(oauthToken);

                    // Create a new json formatter so the request will be in the right format
                    jsonFormatter = new JsonMediaTypeFormatter();

                    // Use the JSON formatter to create the content of the chatter post
                    multipartFormDataContent.Add(new ObjectContent <ChatterPostedMessage>(chatterPostedMessage, jsonFormatter), "json");

                    // Post the message over to chatter
                    httpResponseMessage = httpClient.PostAsync(endpointUrl, multipartFormDataContent).Result;

                    // Check the status of the response and respond appropriately
                    if (httpResponseMessage.IsSuccessStatusCode)
                    {
                        // Grab the chatter message from the post
                        chatterMessage = httpResponseMessage.Content.ReadAsAsync <ChatterMessage>().Result;

                        // Convert it over to a manywho message
                        message = SalesforceSocialSingleton.GetInstance().ChatterMessageToMessageAPI(chatterBaseUrl, null, chatterMessage);

                        // We successfully executed the request, we can break out of the retry loop
                        break;
                    }
                    else
                    {
                        // Make sure we handle the lack of success properly
                        BaseHttpUtils.HandleUnsuccessfulHttpResponseMessage(notifier, authenticatedWho, i, httpResponseMessage, endpointUrl);
                    }
                }
                catch (Exception exception)
                {
                    // Make sure we handle the exception properly
                    BaseHttpUtils.HandleHttpException(notifier, authenticatedWho, i, exception, endpointUrl);
                }
                finally
                {
                    // Clean up the objects from the request
                    BaseHttpUtils.CleanUpHttp(httpClient, null, httpResponseMessage);
                }
            }

            return(message);
        }
Beispiel #2
0
        public ServiceResponseAPI InvokeNotifyUsers(INotifier notifier, IAuthenticatedWho authenticatedWho, ServiceRequestAPI serviceRequest)
        {
            ChatterPostedMessage     chatterPostedMessage     = null;
            ChatterNewMessageSegment chatterNewMessageSegment = null;
            ServiceResponseAPI       serviceResponse          = null;
            SforceService            sforceService            = null;
            String groupAuthenticationToken = null;
            String authenticationUrl        = null;
            String username       = null;
            String password       = null;
            String securityToken  = null;
            String chatterBaseUrl = null;
            String adminEmail     = null;
            String endpointUrl    = null;
            String message        = null;

            // Grab the configuration values from the service request
            authenticationUrl = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_AUTHENTICATION_URL, serviceRequest.configurationValues, true);
            username          = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_USERNAME, serviceRequest.configurationValues, true);
            password          = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_PASSWORD, serviceRequest.configurationValues, true);
            securityToken     = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_SECURITY_TOKEN, serviceRequest.configurationValues, false);
            chatterBaseUrl    = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_CHATTER_BASE_URL, serviceRequest.configurationValues, true);
            adminEmail        = ValueUtils.GetContentValue(SalesforceServiceSingleton.SERVICE_VALUE_ADMIN_EMAIL, serviceRequest.configurationValues, true);

            if (serviceRequest.authorization != null)
            {
                Boolean postMade = false;

                // Get the message from the inputs
                message = ValueUtils.GetContentValue("Post", serviceRequest.inputs, true);

                // Check to see if we have any group authorization - if so, we post to those groups
                if (serviceRequest.authorization.groups != null &&
                    serviceRequest.authorization.groups.Count > 0)
                {
                    foreach (GroupAuthorizationGroupAPI groupAuthorization in serviceRequest.authorization.groups)
                    {
                        // For group posts, we post as the admin, not as the user - as it's very likely the user does not have permissions to post
                        if (groupAuthenticationToken == null ||
                            groupAuthenticationToken.Trim().Length == 0)
                        {
                            // Login as the API user
                            sforceService = SalesforceDataSingleton.GetInstance().Login(authenticatedWho, serviceRequest.configurationValues, true, false);

                            if (sforceService == null)
                            {
                                throw new ArgumentNullException("SalesforceService", "Unable to log into Salesforce.");
                            }

                            // Get the session id out as we'll use that for the oauth login
                            groupAuthenticationToken = sforceService.SessionHeaderValue.sessionId;
                        }

                        // Create the endpoint url for the group
                        endpointUrl = chatterBaseUrl + SalesforceServiceSingleton.CHATTER_URI_PART_API_VERSION + String.Format(SalesforceServiceSingleton.CHATTER_URI_PART_POSTS, groupAuthorization.authenticationId);

                        // Create a new chatter post
                        chatterPostedMessage      = new ChatterPostedMessage();
                        chatterPostedMessage.Body = new ChatterNewMessageBody();
                        chatterPostedMessage.Body.MessageSegments = new List <ChatterSegment>();

                        // Create a message segment for the actual post
                        chatterNewMessageSegment      = new ChatterNewMessageSegment();
                        chatterNewMessageSegment.Type = ChatterMessageSegmentType.Text.ToString();
                        chatterNewMessageSegment.Text = message;

                        // Add the segment to the post
                        chatterPostedMessage.Body.MessageSegments.Add(chatterNewMessageSegment);

                        // Post the message synchronously
                        SalesforceSocialSingleton.GetInstance().PostNotification(notifier, authenticatedWho, groupAuthenticationToken, serviceRequest, endpointUrl, serviceRequest.joinPlayerUri, chatterPostedMessage);

                        // Set the flag that we did in fact make a post
                        postMade = true;
                    }
                }

                // Check to see if we have any user authorization - if so, we post to those users
                if (serviceRequest.authorization.users != null &&
                    serviceRequest.authorization.users.Count > 0)
                {
                    // Create the endpoint url
                    endpointUrl = chatterBaseUrl + SalesforceServiceSingleton.CHATTER_URI_PART_API_VERSION + String.Format(SalesforceServiceSingleton.CHATTER_URI_PART_MY_FEED, authenticatedWho.UserId);

                    // Create a new chatter post
                    chatterPostedMessage      = new ChatterPostedMessage();
                    chatterPostedMessage.Body = new ChatterNewMessageBody();
                    chatterPostedMessage.Body.MessageSegments = new List <ChatterSegment>();

                    // Create a message segment for the actual post
                    chatterNewMessageSegment      = new ChatterNewMessageSegment();
                    chatterNewMessageSegment.Type = ChatterMessageSegmentType.Text.ToString();
                    chatterNewMessageSegment.Text = message;

                    // Add the segment to the post
                    chatterPostedMessage.Body.MessageSegments.Add(chatterNewMessageSegment);

                    // Rather than posting to each user, we do a joint post to all of the users that need to be notified
                    foreach (GroupAuthorizationUserAPI userAuthorization in serviceRequest.authorization.users)
                    {
                        ChatterMentionsSegment chatterMentionsSegment = null;

                        chatterMentionsSegment      = new ChatterMentionsSegment();
                        chatterMentionsSegment.Id   = userAuthorization.authenticationId;
                        chatterMentionsSegment.Type = ChatterMessageSegmentType.Mention.ToString();

                        // Add the user to the @mention
                        chatterPostedMessage.Body.MessageSegments.Add(chatterMentionsSegment);
                    }

                    // Post the message synchronously
                    SalesforceSocialSingleton.GetInstance().PostNotification(notifier, authenticatedWho, serviceRequest, endpointUrl, serviceRequest.joinPlayerUri, chatterPostedMessage);

                    // Set the flag that we did in fact make a post
                    postMade = true;
                }

                if (postMade == false)
                {
                    // Alert the admin that no message was sent
                    ErrorUtils.SendAlert(notifier, authenticatedWho, ErrorUtils.ALERT_TYPE_WARNING, "The service request does not have anything in the authorization context, so there's no one to notify.");
                }
            }
            else
            {
                // Alert the admin that no one is in the authorization context
                ErrorUtils.SendAlert(notifier, authenticatedWho, ErrorUtils.ALERT_TYPE_WARNING, "The service request does not have an authorization context, so there's no one to notify.");
            }

            // Construct the service response
            serviceResponse            = new ServiceResponseAPI();
            serviceResponse.invokeType = ManyWhoConstants.INVOKE_TYPE_FORWARD;
            serviceResponse.token      = serviceRequest.token;
            serviceResponse.outputs    = null;

            return(serviceResponse);
        }
 public MessageAPI PostNotification(INotifier notifier, IAuthenticatedWho authenticatedWho, ServiceRequestAPI serviceRequest, String endpointUrl, String flowLink, ChatterPostedMessage chatterPostedMessage)
 {
     return(PostNotification(notifier, authenticatedWho, SalesforceHttpUtils.GetAuthenticationDetails(authenticatedWho.Token).Token, serviceRequest, endpointUrl, flowLink, chatterPostedMessage));
 }