private void SendTextMessage(List <Person> smsRecipients, Dictionary <string, object> mergeFields) { var rockContext = new RockContext(); var communicationService = new CommunicationService(rockContext); var communicationRecipientService = new CommunicationRecipientService(rockContext); var communication = new Rock.Model.Communication(); communication.Status = CommunicationStatus.Approved; communication.SenderPersonAliasId = CurrentPerson.PrimaryAliasId; communicationService.Add(communication); communication.EnabledLavaCommands = GetAttributeValue("EnabledLavaCommands"); communication.IsBulkCommunication = false; communication.CommunicationType = CommunicationType.SMS; communication.ListGroup = null; communication.ListGroupId = null; communication.ExcludeDuplicateRecipientAddress = true; communication.CommunicationTemplateId = null; communication.FromName = mergeFields["FromName"].ToString().TrimForMaxLength(communication, "FromName"); communication.FromEmail = mergeFields["FromEmail"].ToString().TrimForMaxLength(communication, "FromEmail"); communication.Subject = GetAttributeValue("Subject"); communication.Message = GetAttributeValue("MessageBody"); communication.SMSFromDefinedValueId = DefinedValueCache.GetId(GetAttributeValue("SMSFromNumber").AsGuid()); communication.SMSMessage = GetAttributeValue("SMSMessageBody"); communication.FutureSendDateTime = null; communicationService.Add(communication); rockContext.SaveChanges(); foreach (var smsPerson in smsRecipients) { communication.Recipients.Add(new CommunicationRecipient() { PersonAliasId = smsPerson.PrimaryAliasId, MediumEntityTypeId = EntityTypeCache.Get(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_SMS.AsGuid()).Id, AdditionalMergeValues = mergeFields }); } rockContext.SaveChanges(); var transaction = new Rock.Transactions.SendCommunicationTransaction(); transaction.CommunicationId = communication.Id; transaction.PersonAlias = CurrentPersonAlias; Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction); }
private void BuildCommunication(List <GroupMember> members) { members = members.Where(m => m.GroupMemberStatus == GroupMemberStatus.Active).ToList(); if (!members.Any()) { nbError.Text = "No members matched these filters."; return; } RockContext rockContext = new RockContext(); CommunicationService communicationService = new CommunicationService(rockContext); var communication = new Rock.Model.Communication { IsBulkCommunication = true, Status = CommunicationStatus.Transient }; foreach (var person in members.Select(m => m.Person).ToList()) { communication.Recipients.Add(new CommunicationRecipient() { PersonAliasId = person.PrimaryAliasId.Value }); } communicationService.Add(communication); rockContext.SaveChanges(); Response.Redirect("/Communication/" + communication.Id.ToString()); }
/// <summary> /// Create and persist a new SMS Communication instance. /// </summary> /// <param name="dataContext"></param> /// <param name="guid"></param> /// <param name="communicationDateTime"></param> /// <param name="title"></param> /// <param name="message"></param> /// <param name="openedDateTime"></param> /// <param name="senderPersonAliasGuid"></param> /// <param name="reviewerPersonAliasGuid"></param> /// <param name="smsSenderId"></param> /// <param name="isBulk"></param> /// <returns></returns> private Rock.Model.Communication CreateSmsCommunication(RockContext dataContext, string guid, DateTime?communicationDateTime, string title, string message, DateTime?openedDateTime, Guid senderPersonAliasGuid, Guid reviewerPersonAliasGuid, int smsSenderId, bool isBulk = false) { var personGuidToAliasIdMap = GetPersonGuidToAliasIdMap(dataContext); var communicationService = new CommunicationService(dataContext); var newSms = new Rock.Model.Communication(); newSms.Guid = guid.AsGuid(); newSms.CommunicationType = CommunicationType.SMS; newSms.Subject = title; newSms.Status = CommunicationStatus.Approved; newSms.ReviewedDateTime = communicationDateTime; newSms.ReviewerNote = "Read and approved by the Communications Manager."; newSms.IsBulkCommunication = isBulk; newSms.SenderPersonAliasId = personGuidToAliasIdMap[senderPersonAliasGuid]; newSms.ReviewerPersonAliasId = personGuidToAliasIdMap[reviewerPersonAliasGuid]; newSms.SendDateTime = communicationDateTime; newSms.SMSMessage = message; newSms.SMSFromDefinedValueId = smsSenderId; newSms.CreatedDateTime = communicationDateTime; newSms.CreatedByPersonAliasId = personGuidToAliasIdMap[senderPersonAliasGuid]; newSms.ForeignKey = _TestDataSourceOfChange; communicationService.Add(newSms); dataContext.SaveChanges(); return(newSms); }
/// <summary> /// Create and persist a new Email Communication instance. /// </summary> /// <param name="dataContext"></param> /// <param name="guid"></param> /// <param name="communicationDateTime"></param> /// <param name="subject"></param> /// <param name="message"></param> /// <param name="openedDateTime"></param> /// <param name="senderPersonAliasGuid"></param> /// <param name="reviewerPersonAliasGuid"></param> /// <param name="isBulk"></param> /// <returns></returns> private Rock.Model.Communication CreateEmailCommunication(RockContext dataContext, string guid, DateTime?communicationDateTime, string subject, string message, DateTime?openedDateTime, Guid senderPersonAliasGuid, Guid reviewerPersonAliasGuid, bool isBulk = false) { var personGuidToAliasIdMap = GetPersonGuidToAliasIdMap(dataContext); var communicationService = new CommunicationService(dataContext); var newEmail = new Rock.Model.Communication(); newEmail.Guid = guid.AsGuid(); newEmail.CommunicationType = CommunicationType.Email; newEmail.Subject = subject; newEmail.Status = CommunicationStatus.Approved; newEmail.ReviewedDateTime = communicationDateTime; newEmail.ReviewerNote = "Read and approved by the Communications Manager."; newEmail.IsBulkCommunication = isBulk; newEmail.SenderPersonAliasId = personGuidToAliasIdMap[senderPersonAliasGuid]; newEmail.ReviewerPersonAliasId = personGuidToAliasIdMap[reviewerPersonAliasGuid]; newEmail.SendDateTime = communicationDateTime; newEmail.Message = message; newEmail.CreatedDateTime = communicationDateTime; newEmail.CreatedByPersonAliasId = personGuidToAliasIdMap[senderPersonAliasGuid]; newEmail.ForeignKey = _TestDataSourceOfChange; communicationService.Add(newEmail); dataContext.SaveChanges(); return(newEmail); }
/// <summary> /// Updates a communication model with the user-entered values /// </summary> /// <param name="communicationService">The service.</param> /// <returns></returns> private Rock.Model.Communication UpdateCommunication(RockContext rockContext, Guid templateGuid) { var communicationService = new CommunicationService(rockContext); var communicationAttachmentService = new CommunicationAttachmentService(rockContext); var communicationRecipientService = new CommunicationRecipientService(rockContext); var MediumEntityTypeId = EntityTypeCache.Read("Rock.Communication.Medium.Email").Id; Rock.Model.Communication communication = null; IQueryable <CommunicationRecipient> qryRecipients = null; CommunicationDetails CommunicationData = new CommunicationDetails(); var template = new CommunicationTemplateService(new RockContext()).Get(templateGuid); if (template != null) { CommunicationDetails.Copy(template, CommunicationData); CommunicationData.EmailAttachmentBinaryFileIds = template.EmailAttachmentBinaryFileIds; } if (communication == null) { communication = new Rock.Model.Communication(); communication.Status = CommunicationStatus.Transient; communication.SenderPersonAliasId = CurrentPersonAliasId; communicationService.Add(communication); } if (qryRecipients == null) { qryRecipients = communication.GetRecipientsQry(rockContext); } communication.IsBulkCommunication = false; var medium = MediumContainer.GetComponentByEntityTypeId(MediumEntityTypeId); if (medium != null) { communication.CommunicationType = medium.CommunicationType; } communication.CommunicationTemplateId = template.Id; //GetMediumData(); foreach (var recipient in communication.Recipients) { recipient.MediumEntityTypeId = MediumEntityTypeId; } CommunicationDetails.Copy(CommunicationData, communication); // delete any attachments that are no longer included foreach (var attachment in communication.Attachments.Where(a => !CommunicationData.EmailAttachmentBinaryFileIds.Contains(a.BinaryFileId)).ToList()) { communication.Attachments.Remove(attachment); communicationAttachmentService.Delete(attachment); } // add any new attachments that were added foreach (var attachmentBinaryFileId in CommunicationData.EmailAttachmentBinaryFileIds.Where(a => !communication.Attachments.Any(x => x.BinaryFileId == a))) { communication.AddAttachment(new CommunicationAttachment { BinaryFileId = attachmentBinaryFileId }, CommunicationType.Email); } communication.FutureSendDateTime = null; return(communication); }