예제 #1
0
        /// <summary>
        /// Creates the CommunicationResponse for Rock SMS Conversations
        /// </summary>
        /// <param name="fromPerson">From person.</param>
        /// <param name="messageKey">The message key.</param>
        /// <param name="toPersonAliasId">To person alias identifier.</param>
        /// <param name="message">The message.</param>
        /// <param name="rockSmsFromPhoneDv">From phone.</param>
        /// <param name="responseCode">The response code.</param>
        /// <param name="rockContext">The rock context.</param>
        private void CreateCommunicationResponse(Person fromPerson, string messageKey, int?toPersonAliasId, string message, DefinedValueCache rockSmsFromPhoneDv, string responseCode, Rock.Data.RockContext rockContext)
        {
            var smsMedium       = EntityTypeCache.Get(SystemGuid.EntityType.COMMUNICATION_MEDIUM_SMS);
            var smsTransport    = this.Transport.EntityType.Id;
            int?communicationId = null;

            if (fromPerson != null)
            {
                communicationId = GetCommunicationId(rockSmsFromPhoneDv, fromPerson.PrimaryAliasId.Value, 2);
            }

            var communicationResponse = new CommunicationResponse
            {
                MessageKey                   = messageKey,
                FromPersonAliasId            = fromPerson?.PrimaryAliasId,
                ToPersonAliasId              = toPersonAliasId,
                IsRead                       = false,
                RelatedSmsFromDefinedValueId = rockSmsFromPhoneDv.Id,
                RelatedCommunicationId       = communicationId,
                RelatedTransportEntityTypeId = smsTransport,
                RelatedMediumEntityTypeId    = smsMedium.Id,
                Response                     = message
            };

            var communicationResposeService = new CommunicationResponseService(rockContext);

            communicationResposeService.Add(communicationResponse);
            rockContext.SaveChanges();
        }
예제 #2
0
파일: Sms.cs 프로젝트: SparkDevNetwork/Rock
        /// <summary>
        /// Creates the CommunicationResponse for Rock SMS Conversations
        /// </summary>
        /// <param name="fromPerson">From person.</param>
        /// <param name="messageKey">The message key.</param>
        /// <param name="toPersonAliasId">To person alias identifier.</param>
        /// <param name="message">The message.</param>
        /// <param name="rockSmsFromPhoneDv">From phone.</param>
        /// <param name="responseCode">The response code.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="attachments">The attachments.</param>
        /// <exception cref="System.Exception">Configuration Error. No SMS Transport Component is currently active.</exception>
        private void CreateCommunicationResponse(Person fromPerson, string messageKey, int?toPersonAliasId, string message, DefinedValueCache rockSmsFromPhoneDv, string responseCode, Rock.Data.RockContext rockContext, List <BinaryFile> attachments = null)
        {
            var smsMedium = EntityTypeCache.Get(SystemGuid.EntityType.COMMUNICATION_MEDIUM_SMS);

            if (this.Transport == null)
            {
                throw new Exception("Configuration Error. No SMS Transport Component is currently active.");
            }

            var smsTransport    = this.Transport.EntityType.Id;
            int?communicationId = null;

            if (fromPerson != null)
            {
                communicationId = GetCommunicationId(rockSmsFromPhoneDv, fromPerson.PrimaryAliasId.Value, 2);
            }

            var communicationResponse = new CommunicationResponse
            {
                MessageKey                   = messageKey,
                FromPersonAliasId            = fromPerson?.PrimaryAliasId,
                ToPersonAliasId              = toPersonAliasId,
                IsRead                       = false,
                RelatedSmsFromDefinedValueId = rockSmsFromPhoneDv.Id,
                RelatedCommunicationId       = communicationId,
                RelatedTransportEntityTypeId = smsTransport,
                RelatedMediumEntityTypeId    = smsMedium.Id,
                Response                     = message
            };

            var communicationResposeService = new CommunicationResponseService(rockContext);

            communicationResposeService.Add(communicationResponse);
            rockContext.SaveChanges();

            // Now that we have a communication response ID we can add the attachments
            if (attachments != null && attachments.Any())
            {
                foreach (var attachment in attachments)
                {
                    communicationResponse.Attachments.Add(
                        new CommunicationResponseAttachment
                    {
                        BinaryFileId            = attachment.Id,
                        CommunicationResponseId = communicationResponse.Id,
                        CommunicationType       = CommunicationType.SMS
                    }
                        );
                }

                rockContext.SaveChanges();
            }
        }
예제 #3
0
        public void AddNamelessSmsConversation()
        {
            var smsMediumEntityTypeId    = EntityTypeCache.GetId(SystemGuid.EntityType.COMMUNICATION_MEDIUM_SMS.AsGuid()).Value;
            var smsTransportEntityTypeId = EntityTypeCache.GetId(Constants.TestTwilioTransportEntityTypeGuid.AsGuid()).Value;

            var dataContext = new RockContext();

            var responseCode = Rock.Communication.Medium.Sms.GenerateResponseCode(dataContext);

            // Create a new Communication
            var adminPerson = this.GetAdminPersonOrThrow(dataContext);

            var personService = new PersonService(dataContext);

            var namelessPerson1 = personService.GetPersonFromMobilePhoneNumber(Constants.NamelessPerson1MobileNumber, createNamelessPersonIfNotFound: true);

            dataContext.SaveChanges();

            var smsSender = DefinedValueCache.Get(Constants.TestSmsSenderGuid.AsGuid());

            var communicationService         = new CommunicationService(dataContext);
            var communicationResponseService = new CommunicationResponseService(dataContext);

            // From Admin: Initial Message
            Rock.Model.Communication communication;

            var conversationStartDateTime = RockDateTime.Now;

            var message = "If you'd like to attend our Family Movie Night Tea this coming Saturday, please reply to let us know how many people will be attending with you.";

            communication = communicationService.CreateSMSCommunication(adminPerson, namelessPerson1.PrimaryAliasId, message, smsSender, responseCode, "From: " + adminPerson.FullName);

            communication.Recipients.ToList().ForEach(r =>
            {
                r.Status          = CommunicationRecipientStatus.Delivered;
                r.CreatedDateTime = conversationStartDateTime;
                r.ForeignKey      = _TestDataSourceOfChange;
            });

            communication.ForeignKey      = _TestDataSourceOfChange;
            communication.CreatedDateTime = conversationStartDateTime;

            // From Nameless: Reply #1
            message = "We will be there! 2 adults, 2 kids. Do we need to bring chairs?";
            communicationResponseService.Add(new CommunicationResponse
            {
                ForeignKey      = _TestDataSourceOfChange,
                CreatedDateTime = conversationStartDateTime.AddMinutes(2),
                RelatedTransportEntityTypeId = smsTransportEntityTypeId,
                RelatedMediumEntityTypeId    = smsMediumEntityTypeId,
                RelatedSmsFromDefinedValueId = smsSender.Id,
                ToPersonAliasId   = adminPerson.PrimaryAliasId,
                Response          = message,
                IsRead            = false,
                FromPersonAliasId = namelessPerson1.PrimaryAliasId,
                MessageKey        = Constants.NamelessPerson1MobileNumber
            });

            // From Admin: Response #1
            message       = "No need to bring anything extra - just yourselves!";
            communication = communicationService.CreateSMSCommunication(adminPerson, namelessPerson1.PrimaryAliasId, message, smsSender, responseCode, "From: " + adminPerson.FullName);
            communication.Recipients.ToList().ForEach(r =>
            {
                r.Status          = CommunicationRecipientStatus.Delivered;
                r.CreatedDateTime = conversationStartDateTime.AddMinutes(3);
                r.ForeignKey      = _TestDataSourceOfChange;
            });

            communication.ForeignKey      = _TestDataSourceOfChange;
            communication.CreatedDateTime = conversationStartDateTime.AddMinutes(3);

            // From Nameless: Reply #2
            message = "Ok, thanks. What movie is showing?";
            communicationResponseService.Add(new CommunicationResponse
            {
                ForeignKey      = _TestDataSourceOfChange,
                CreatedDateTime = conversationStartDateTime.AddMinutes(4),
                RelatedTransportEntityTypeId = smsTransportEntityTypeId,
                RelatedMediumEntityTypeId    = smsMediumEntityTypeId,
                RelatedSmsFromDefinedValueId = smsSender.Id,
                ToPersonAliasId   = adminPerson.PrimaryAliasId,
                Response          = message,
                IsRead            = false,
                FromPersonAliasId = namelessPerson1.PrimaryAliasId,
                MessageKey        = Constants.NamelessPerson1MobileNumber
            });

            // From Admin: Response #2
            message       = "We'll be screening Star Wars: Episode 1.";
            communication = communicationService.CreateSMSCommunication(adminPerson, namelessPerson1.PrimaryAliasId, message, smsSender, responseCode, "From: " + adminPerson.FullName);
            communication.Recipients.ToList().ForEach(r =>
            {
                r.Status          = CommunicationRecipientStatus.Delivered;
                r.CreatedDateTime = conversationStartDateTime.AddMinutes(5);
                r.ForeignKey      = _TestDataSourceOfChange;
            });

            communication.ForeignKey      = _TestDataSourceOfChange;
            communication.CreatedDateTime = conversationStartDateTime.AddMinutes(5);

            // From Nameless: Reply #3
            message = "Is that the one with Jar Jar Binks?";
            communicationResponseService.Add(new CommunicationResponse
            {
                ForeignKey      = _TestDataSourceOfChange,
                CreatedDateTime = conversationStartDateTime.AddMinutes(9),
                RelatedTransportEntityTypeId = smsTransportEntityTypeId,
                RelatedMediumEntityTypeId    = smsMediumEntityTypeId,
                RelatedSmsFromDefinedValueId = smsSender.Id,
                ToPersonAliasId   = adminPerson.PrimaryAliasId,
                Response          = message,
                IsRead            = false,
                FromPersonAliasId = namelessPerson1.PrimaryAliasId,
                MessageKey        = Constants.NamelessPerson1MobileNumber
            });

            // From Admin: Response #2
            message       = "Yep, that's the one!";
            communication = communicationService.CreateSMSCommunication(adminPerson, namelessPerson1.PrimaryAliasId, message, smsSender, responseCode, "From: " + adminPerson.FullName);
            communication.Recipients.ToList().ForEach(r =>
            {
                r.Status          = CommunicationRecipientStatus.Delivered;
                r.CreatedDateTime = conversationStartDateTime.AddMinutes(10);
                r.ForeignKey      = _TestDataSourceOfChange;
            });

            communication.ForeignKey      = _TestDataSourceOfChange;
            communication.CreatedDateTime = conversationStartDateTime.AddMinutes(10);
            // From Nameless: Reply #3
            message = "Right, I just remembered we may have some other plans, sorry...";
            communicationResponseService.Add(new CommunicationResponse
            {
                ForeignKey      = _TestDataSourceOfChange,
                CreatedDateTime = conversationStartDateTime.AddMinutes(12),
                RelatedTransportEntityTypeId = smsTransportEntityTypeId,
                RelatedMediumEntityTypeId    = smsMediumEntityTypeId,
                RelatedSmsFromDefinedValueId = smsSender.Id,
                ToPersonAliasId   = adminPerson.PrimaryAliasId,
                Response          = message,
                IsRead            = false,
                FromPersonAliasId = namelessPerson1.PrimaryAliasId,
                MessageKey        = Constants.NamelessPerson1MobileNumber
            });

            dataContext.SaveChanges();
        }