private void ProcessSendGridEvent(SendGridEvent sendGridEvent, Rock.Data.RockContext rockContext) { Guid?actionGuid = null; Guid?communicationRecipientGuid = null; if (!string.IsNullOrWhiteSpace(sendGridEvent.WorkflowActionGuid)) { actionGuid = sendGridEvent.WorkflowActionGuid.AsGuidOrNull(); } if (!string.IsNullOrWhiteSpace(sendGridEvent.CommunicationRecipientGuid)) { communicationRecipientGuid = sendGridEvent.CommunicationRecipientGuid.AsGuidOrNull(); } if (actionGuid != null) { ProcessForWorkflow(actionGuid, rockContext, sendGridEvent); } if (communicationRecipientGuid != null) { ProcessForRecipient(communicationRecipientGuid, rockContext, sendGridEvent); } }
private bool IsToHanlde(SendGridEvent sendGridEvent) { // TODO: trace the messages that should not be handled for debugging purposes. // For now this service can handle only bounced and dropped events. return ((sendGridEvent.EventType.EqualsIgnoreCase(SendGridEventTypes.Bounce) || sendGridEvent.EventType.EqualsIgnoreCase(SendGridEventTypes.Dropped)) && sendGridEvent.SitefinityCampaignId != Guid.Empty && sendGridEvent.SitefinitySubscriberId != Guid.Empty); }
private BounceStatus ResolveStatus(SendGridEvent sendGridEvent) { BounceStatus bounceStatus; // If the event is for dropped email we mark it as hard bounced. if (sendGridEvent.EventType.EqualsIgnoreCase(SendGridEventTypes.Dropped)) { bounceStatus = BounceStatus.Hard; } else { bounceStatus = MessageParser.GetBounceMessageStatus(sendGridEvent.Status); } return(bounceStatus); }
private void ProcessForWorkflow(Guid?actionGuid, Rock.Data.RockContext rockContext, SendGridEvent payload) { RockLogger.Log.Debug(RockLogDomains.Communications, "ProcessForWorkflow {@payload}", payload); string status = string.Empty; switch (payload.EventType) { case "unsubscribe": case "delivered": status = SendEmailWithEvents.SENT_STATUS; break; case "click": status = SendEmailWithEvents.CLICKED_STATUS; break; case "open": status = SendEmailWithEvents.OPENED_STATUS; break; case "failed": case "dropped": case "blocked": case "bounce": case "bounced": status = SendEmailWithEvents.FAILED_STATUS; string message = payload.ServerResponse.IsNotNullOrWhiteSpace() ? payload.ServerResponse : payload.EventTypeReason; Rock.Communication.Email.ProcessBounce( payload.Email, Rock.Communication.BounceType.HardBounce, message, RockDateTime.ConvertLocalDateTimeToRockDateTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(payload.Timestamp).ToLocalTime())); break; } if (actionGuid != null && !string.IsNullOrWhiteSpace(status)) { SendEmailWithEvents.UpdateEmailStatus(actionGuid.Value, status, payload.EventType, rockContext, true); } }
/// <summary> /// Processes for recipient. /// </summary> /// <param name="eventType">Type of the event.</param> /// <param name="communicationRecipientGuid">The communication recipient unique identifier.</param> /// <param name="rockContext">The rock context.</param> private void ProcessForRecipient(Guid?communicationRecipientGuid, Rock.Data.RockContext rockContext, SendGridEvent payload) { RockLogger.Log.Debug(RockLogDomains.Communications, "ProcessForRecipient {@payload}", payload); if (!communicationRecipientGuid.HasValue) { return; } var communicationRecipient = new CommunicationRecipientService(rockContext).Get(communicationRecipientGuid.Value); if (communicationRecipient != null && communicationRecipient.Communication != null) { var communicationGuid = Rock.SystemGuid.InteractionChannel.COMMUNICATION.AsGuid(); var interactionComponent = new InteractionComponentService(rockContext) .GetComponentByEntityId(communicationGuid, communicationRecipient.CommunicationId, communicationRecipient.Communication.Subject); rockContext.SaveChanges(); var interactionService = new InteractionService(rockContext); DateTime timeStamp = RockDateTime.ConvertLocalDateTimeToRockDateTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(payload.Timestamp).ToLocalTime()); switch (payload.EventType) { case "processed": // Do nothing. break; case "dropped": communicationRecipient.Status = CommunicationRecipientStatus.Failed; communicationRecipient.StatusNote = payload.EventTypeReason; if (payload.EventTypeReason == "Bounced Address") { Rock.Communication.Email.ProcessBounce( payload.Email, Rock.Communication.BounceType.HardBounce, payload.EventTypeReason, timeStamp); } break; case "delivered": communicationRecipient.Status = CommunicationRecipientStatus.Delivered; communicationRecipient.StatusNote = string.Format("Confirmed delivered by SendGrid at {0}", timeStamp.ToString()); break; case "deferred": // TODO: handle deferred. break; case "bounce": communicationRecipient.Status = CommunicationRecipientStatus.Failed; communicationRecipient.StatusNote = payload.EventTypeReason + payload.ServerResponse; Rock.Communication.Email.ProcessBounce( payload.Email, Rock.Communication.BounceType.HardBounce, payload.EventTypeReason, timeStamp); break; case "blocked": // TODO: handle blocked. break; case "open": communicationRecipient.Status = CommunicationRecipientStatus.Opened; communicationRecipient.OpenedDateTime = timeStamp; communicationRecipient.OpenedClient = string.Format( "{0} {1} ({2})", payload.ClientOs ?? "unknown", payload.ClientBrowser ?? "unknown", payload.ClientDeviceType ?? "unknown"); if (interactionComponent != null) { interactionService.AddInteraction( interactionComponent.Id, communicationRecipient.Id, "Opened", payload.SendGridEventId, communicationRecipient.PersonAliasId, timeStamp, payload.ClientBrowser, payload.ClientOs, payload.ClientDeviceType, payload.ClientDeviceBrand, payload.IpAddress, null); } break; case "click": if (interactionComponent != null) { interactionService.AddInteraction( interactionComponent.Id, communicationRecipient.Id, "Click", payload.Url, communicationRecipient.PersonAliasId, timeStamp, payload.ClientBrowser, payload.ClientOs, payload.ClientDeviceType, payload.ClientDeviceBrand, payload.IpAddress, null); } break; case "spamreport": case "unsubscribe": case "group_unsubscribe": case "group_resubscribe": // Do nothing. break; } rockContext.SaveChanges(); } }
public HttpResponseMessage Report() { SendGridEventClient sgec = new SendGridEventClient(); string str = Request.Content.ReadAsStringAsync().Result; if (JToken.Parse(str).Type == JTokenType.Array) { dynamic stuff = JArray.Parse(str); foreach (dynamic item in stuff) { if (item["event"] != null && item["email"] != null) { string eventType = item["event"]; string email = item["email"]; email = email.ToLower(); SendGridEvent eventItem = sgec.GetByPartitionAndRowKey(SendGridEventClient.GetPartitionKeyForEmail(email), email); if (eventItem != null) { switch (eventType) { case "click": eventItem.Clicked++; break; case "open": eventItem.Opened++; break; case "dropped": eventItem.Delivered = false; break; case "bounce": eventItem.Delivered = false; break; case "delivered": eventItem.Delivered = true; break; case "unsubscribe": eventItem.Unsubscribed = true; break; case "spam": eventItem.Spam = true; break; default: continue; } eventItem.LastUpdated = EasternTimeConverter.Convert(DateTime.UtcNow); sgec.Update(eventItem); } else { eventItem = new SendGridEvent { PartitionKey = SendGridEventClient.GetPartitionKeyForEmail(email), RowKey = email, Email = email, Delivered = true, Clicked = 0, Opened = 0 }; switch (eventType) { case "click": eventItem.Clicked = 1; break; case "open": eventItem.Opened = 1; break; case "dropped": eventItem.Delivered = false; break; case "bounce": eventItem.Delivered = false; break; case "delivered": break; case "unsubscribe": eventItem.Unsubscribed = true; break; case "spam": eventItem.Spam = true; break; default: continue; } sgec.AddNewItem(eventItem); } } } } return(new HttpResponseMessage { StatusCode = HttpStatusCode.OK }); }