Ejemplo n.º 1
0
        private string ProcessMessageParameters(MessageResponseContract messageResponse, string fullMessage,
                                                out Dictionary <string, string> messageParameters)
        {
            var regex = new Regex(_messageParametersRegexPattern, RegexOptions.Singleline);
            var match = regex.Match(fullMessage);

            if (!match.Success)
            {
                messageParameters = new Dictionary <string, string>();
                return(fullMessage);
            }

            messageParameters = new Dictionary <string, string>();

            // We need to check that message parameters are defined at the beginning of template
            try
            {
                messageParameters = JsonConvert.DeserializeObject <Dictionary <string, string> >(match.Groups[1].Value);
            }
            catch (Exception)
            {
                ProcessErrorMessageResponse(messageResponse,
                                            "Message parameters are not in proper (JSON) format or they are not defined at the beginning of message template and thus could not be extracted",
                                            new { fullMessage });
            }

            var message = match.Groups[2].Value;

            return(message);
        }
Ejemplo n.º 2
0
        public async Task <MessageResponseContract> ProcessEmailAsync(EmailMessage emailMessage, CallType callType)
        {
            var messageResponse = new MessageResponseContract
            {
                Status = ResponseStatus.Success
            };

            if (string.IsNullOrEmpty(emailMessage.CustomerId))
            {
                ProcessErrorMessageResponse(messageResponse, "Could not find customer id");

                return(messageResponse);
            }

            //Check if we know where the message is from
            if (string.IsNullOrEmpty(emailMessage.Source))
            {
                ProcessErrorMessageResponse(messageResponse, "Message source field is not set",
                                            emailMessage.CustomerId);

                return(messageResponse);
            }

            if (string.IsNullOrEmpty(emailMessage.SubjectTemplateId))
            {
                ProcessErrorMessageResponse(messageResponse, "Message subject template id is not set",
                                            new { emailMessage.CustomerId, emailMessage.Source });

                return(messageResponse);
            }

            if (string.IsNullOrEmpty(emailMessage.MessageTemplateId))
            {
                ProcessErrorMessageResponse(messageResponse, "Message template id is not set",
                                            new { emailMessage.CustomerId, emailMessage.Source });

                return(messageResponse);
            }

            var identityParameters = await GetIdentityParameters(emailMessage.CustomerId);

            // Temporary work-around to send notifications for non-customers
            string email;

            if (emailMessage.TemplateParameters.ContainsKey(EmailParamKey) &&
                !string.IsNullOrEmpty(emailMessage.TemplateParameters[EmailParamKey]))
            {
                email = emailMessage.TemplateParameters[EmailParamKey];
            }
            // Get customer's email
            else if (!identityParameters.TryGetValue(GetFormattedEmailKey(), out email) || string.IsNullOrEmpty(email))
            {
                ProcessErrorMessageResponse(messageResponse, "Could not find customer's email",
                                            new { emailMessage.CustomerId, Key = _emailKey, emailMessage.Source });

                return(messageResponse);
            }

            Localization localization;

            // get localization from emailMessage.TemplateParameters
            if (emailMessage.TemplateParameters.ContainsKey(LocalizationParamKey) &&
                !string.IsNullOrEmpty(emailMessage.TemplateParameters[LocalizationParamKey]))
            {
                localization = Localization.From(emailMessage.TemplateParameters[LocalizationParamKey]);
            }
            else
            {
                localization = GetLocalization(identityParameters);
            }

            //Find subject template
            var subjectTemplate =
                await _templateService.FindTemplateContentAsync(emailMessage.SubjectTemplateId, localization);

            if (subjectTemplate == null)
            {
                ProcessErrorMessageResponse(messageResponse, "Could not find subject template",
                                            new { emailMessage.CustomerId, emailMessage.SubjectTemplateId, emailMessage.Source });

                return(messageResponse);
            }

            //Find message template
            var messageTemplate =
                await _templateService.FindTemplateContentAsync(emailMessage.MessageTemplateId, localization);

            if (messageTemplate == null)
            {
                ProcessErrorMessageResponse(messageResponse, "Could not find message template",
                                            new { emailMessage.CustomerId, emailMessage.MessageTemplateId, emailMessage.Source });

                return(messageResponse);
            }

            // Join namespace parameters and custom parameters
            var templateParamValues = await GetNamespaceParamsAsync(emailMessage.CustomerId,
                                                                    identityParameters, subjectTemplate, messageTemplate);

            emailMessage.TemplateParameters.ForEach(tp => templateParamValues.Add(tp.Key, tp.Value));

            // Process subject and message templates
            var fullSubject = ProcessTemplate(subjectTemplate.Content, templateParamValues);
            var fullMessage = ProcessTemplate(messageTemplate.Content, templateParamValues);

            var subjectToSend = ProcessMessageParameters(messageResponse, fullSubject, out var subjectParameters);
            var messageToSend = ProcessMessageParameters(messageResponse, fullMessage, out var messageParameters);

            var messageId = Guid.NewGuid();

            // Publish audit event
            await PublishCreateAuditMessageEvent(callType, messageId, emailMessage, messageToSend);

            //Prepare email for broker and send it
            await SendEmailMessageAsync(messageId, email, subjectToSend, messageToSend, messageParameters);

            _log.Info("Email sent",
                      new { emailMessage.CustomerId, emailMessage.SubjectTemplateId, emailMessage.MessageTemplateId });

            messageResponse.MessageIds.Add(messageId.ToString());

            return(messageResponse);
        }
Ejemplo n.º 3
0
        public async Task <MessageResponseContract> ProcessPushNotificationAsync(PushNotification pushNotification, CallType callType)
        {
            var messageResponse = new MessageResponseContract
            {
                Status = ResponseStatus.Success
            };

            if (string.IsNullOrEmpty(pushNotification.CustomerId))
            {
                ProcessErrorMessageResponse(messageResponse, "Could not find customer id");

                return(messageResponse);
            }

            //Check if we know where the message is from
            if (string.IsNullOrEmpty(pushNotification.Source))
            {
                ProcessErrorMessageResponse(messageResponse, "Message source field is not set", pushNotification.CustomerId);

                return(messageResponse);
            }

            if (string.IsNullOrEmpty(pushNotification.MessageTemplateId))
            {
                ProcessErrorMessageResponse(messageResponse, "Message template id is not set",
                                            new { pushNotification.CustomerId, pushNotification.Source });

                return(messageResponse);
            }

            //Get customer personal data - this is where localization will come from
            var identityParameters = await GetIdentityParameters(pushNotification.CustomerId);

            var pushNotificationParameters =
                await GetNamespaceParamsAsync(pushNotification.CustomerId, new[] { _pushNotificationsNamespace });

            // Get push registration ids
            if (!pushNotificationParameters.TryGetValue(GetFormattedPushRegistrationIds(), out var pushRegistrationIdsString) ||
                string.IsNullOrEmpty(pushRegistrationIdsString))
            {
                ProcessInfoMessageResponse(messageResponse, "Could not find any customer push registration ids",
                                           new { pushNotification.CustomerId, Key = _pushRegistrationIdsKey, pushNotification.Source });

                return(messageResponse);
            }

            var localization = GetLocalization(identityParameters);

            //Find message template
            var messageTemplate =
                await _templateService.FindTemplateContentAsync(pushNotification.MessageTemplateId, localization);

            if (messageTemplate == null)
            {
                ProcessErrorMessageResponse(messageResponse, "Could not find message template",
                                            new { pushNotification.CustomerId, pushNotification.MessageTemplateId, pushNotification.Source });

                return(messageResponse);
            }

            // Join namespace parameters and custom parameters
            var templateParamValues = await GetNamespaceParamsAsync(pushNotification.CustomerId,
                                                                    identityParameters, messageTemplate);

            pushNotification.TemplateParameters.ForEach(tp => templateParamValues.Add(tp.Key, tp.Value));

            // Process subject and message templates
            var fullMessage = ProcessTemplate(messageTemplate.Content, templateParamValues);

            var messageToSend = ProcessMessageParameters(messageResponse, fullMessage, out var messageParameters);

            var pushRegistrationIds = JsonConvert.DeserializeObject <List <string> >(pushRegistrationIdsString);

            if (!pushRegistrationIds.Any())
            {
                ProcessInfoMessageResponse(messageResponse, "Could not find any customer push registration ids",
                                           new
                {
                    pushNotification.CustomerId,
                    Key = _pushRegistrationIdsKey,
                    pushNotification.Source,
                    pushNotification.MessageTemplateId
                });

                return(messageResponse);
            }

            var messageGroupId = Guid.NewGuid();

            foreach (var pushRegistrationId in pushRegistrationIds)
            {
                var messageId = Guid.NewGuid();

                // Publish audit event
                await PublishCreateAuditMessageEvent(callType, messageId, messageGroupId, pushNotification,
                                                     messageToSend);

                //Prepare email for broker and send it
                await SendPushNotificationAsync(messageId, messageGroupId, pushRegistrationId,
                                                pushNotification.CustomerId, messageToSend, pushNotification.CustomPayload,
                                                messageParameters);

                messageResponse.MessageIds.Add(messageId.ToString());
            }

            _log.Info("Push notifications sent",
                      new { pushNotification.CustomerId, pushNotification.MessageTemplateId });

            return(messageResponse);
        }
Ejemplo n.º 4
0
 private void ProcessInfoMessageResponse(MessageResponseContract messageResponse, string message, object context = null)
 {
     _log.Info(message, context);
     messageResponse.ErrorDescription = message;
     messageResponse.Status           = ResponseStatus.Error;
 }
Ejemplo n.º 5
0
        public async Task <MessageResponseContract> ProcessSmsAsync(Sms sms, CallType callType)
        {
            var messageResponse = new MessageResponseContract
            {
                Status = ResponseStatus.Success
            };

            if (string.IsNullOrEmpty(sms.CustomerId))
            {
                ProcessErrorMessageResponse(messageResponse, "Could not find customer id");

                return(messageResponse);
            }

            //Check if we know where the message is from
            if (string.IsNullOrEmpty(sms.Source))
            {
                ProcessErrorMessageResponse(messageResponse, "Message source field is not set", sms.CustomerId);

                return(messageResponse);
            }

            if (string.IsNullOrEmpty(sms.MessageTemplateId))
            {
                ProcessErrorMessageResponse(messageResponse, "Message template id is not set",
                                            new { sms.CustomerId, sms.Source });

                return(messageResponse);
            }

            //Get customer personal data - this is where localization will come from
            var identityParameters = await GetIdentityParameters(sms.CustomerId);

            // Temporary work-around to send notifications for non-customers
            string phoneNumber;

            if (sms.TemplateParameters.ContainsKey(PhoneNumberParamKey) &&
                !string.IsNullOrEmpty(sms.TemplateParameters[PhoneNumberParamKey]))
            {
                phoneNumber = sms.TemplateParameters[PhoneNumberParamKey];
            }
            // Get customer's phone number
            else if (!identityParameters.TryGetValue(GetFormattedPhoneNumberKey(), out phoneNumber) ||
                     string.IsNullOrEmpty(phoneNumber))
            {
                ProcessErrorMessageResponse(messageResponse, "Could not find customer's phone number",
                                            new { sms.CustomerId, Key = _phoneNumberKey, sms.Source });

                return(messageResponse);
            }

            var localization = GetLocalization(identityParameters);

            //Find message template
            var messageTemplate =
                await _templateService.FindTemplateContentAsync(sms.MessageTemplateId, localization);

            if (messageTemplate == null)
            {
                ProcessErrorMessageResponse(messageResponse, "Could not find message template",
                                            new { sms.CustomerId, sms.MessageTemplateId, sms.Source });

                return(messageResponse);
            }

            // Join namespace parameters and custom parameters
            var templateParamValues = await GetNamespaceParamsAsync(sms.CustomerId,
                                                                    identityParameters, messageTemplate);

            sms.TemplateParameters.ForEach(tp => templateParamValues.Add(tp.Key, tp.Value));

            // Process subject and message templates
            var fullMessage = ProcessTemplate(messageTemplate.Content, templateParamValues);

            var messageToSend = ProcessMessageParameters(messageResponse, fullMessage, out var messageParameters);

            var messageId = Guid.NewGuid();

            // Publish audit event
            await PublishCreateAuditMessageEvent(callType, messageId, sms, messageToSend);

            //Prepare email for broker and send it
            await SendSmsAsync(messageId, phoneNumber, messageToSend, messageParameters);

            _log.Info("SMS sent",
                      new { sms.CustomerId, sms.MessageTemplateId });

            messageResponse.MessageIds.Add(messageId.ToString());

            return(messageResponse);
        }