private async Task SendMessage(NotifyMessage content, CommunicationType communicationType)
        {
            var notificationsClient = new NotificationClient(_configuration.NotificationServiceApiKey);

            // Needs to be a dictionary<string,dynamic> for the client.....
            var personalisationDictionary = content.Personalisation.ToDictionary(x => x.Key, x => x.Value as dynamic);

            try
            {
                Logger.Info($"Sending communication request to Gov Notify");
                if (communicationType == CommunicationType.Email)
                {
                    var response = await notificationsClient.SendEmailAsync(content.To, content.Template, personalisationDictionary, content.Reference);
                }
                else if (communicationType == CommunicationType.Sms)
                {
                    var response = await notificationsClient.SendSmsAsync(content.To, content.Template, personalisationDictionary, content.Reference);
                }
            }
            catch (NotifyClientException notifyClientException)
            {
                Logger.Error(notifyClientException, $"Error sending communication {communicationType.ToString()} to Gov Notify with Gov.Notify Client");

                if (communicationType != CommunicationType.Sms || !SuppressSmsError(notifyClientException.Message))
                {
                    throw;
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception, $"Generic Error sending communication {communicationType.ToString()} to Gov Notify");
                throw;
            }
        }
コード例 #2
0
        public async Task SendMessage(NotifyMessageTypes messageType, string[] addresses, string message)
        {
            if (messageType == null || addresses == null)
            {
                LoggingHandler.LogError("Notify request with invalid arguments");
                throw new ArgumentException("Notify request with invalid arguments");
            }
            var template        = string.Empty;
            var personalisation = new Dictionary <string, dynamic>();

            switch (messageType)
            {
            case NotifyMessageTypes.Reminder:
                template = _reminderTemplate;
                break;

            case NotifyMessageTypes.AdminNotification:
                template = _adminNotificationTemplate;
                break;

            case NotifyMessageTypes.StatusUpdate:
                template = _statusTemplate;
                break;

            case NotifyMessageTypes.NotReverified:
                template = _notReverifiedTemplate;
                break;

            case NotifyMessageTypes.NotApproved:
                template = _notApprovedTemplate;
                personalisation.Add("status_message", message ?? "");
                break;
            }

            try
            {
                for (int a = 0; a < addresses.Length; a++)
                {
                    await _client.SendEmailAsync(addresses[a], template, personalisation).ConfigureAwait(false);
                }
            }
            catch (NotifyClientException e)
            {
                LoggingHandler.LogError("Gov Notify send error");
                LoggingHandler.LogError(e.Message);
                LoggingHandler.LogError(e.StackTrace);
            }
            catch (Exception e)
            {
                LoggingHandler.LogError(e.Message);
                LoggingHandler.LogError(e.StackTrace);
                throw;
            }
        }
コード例 #3
0
 public async Task SendEmailAsync(MailAddress emailAddress, string templateId, Dictionary <string, dynamic>?parameters = null, string?clientReference = null, string?replyToId = null)
 {
     try
     {
         var notificationClient = new NotificationClient(_apiKey);
         var result             = await notificationClient.SendEmailAsync(emailAddress.Address, templateId, parameters, clientReference, replyToId);
     }
     catch (NotifyClientException ex)
     {
         _logger.LogError(ex, $"Error: SendEmail");
         throw new DependencyFailedException($"Error: Failed to send email");
     }
 }
コード例 #4
0
        public async Task SendEmailNotificationGeneratesExpectedRequest()
        {
            Dictionary <string, dynamic> personalisation = new Dictionary <string, dynamic>
            {
                { "name", "someone" }
            };
            JObject expected = new JObject
            {
                { "email_address", Constants.fakeEmail },
                { "template_id", Constants.fakeTemplateId },
                { "personalisation", JObject.FromObject(personalisation) },
                { "reference", Constants.fakeNotificationReference }
            };

            MockRequest(Constants.fakeTemplatePreviewResponseJson,
                        client.SEND_EMAIL_NOTIFICATION_URL,
                        AssertValidRequest,
                        HttpMethod.Post,
                        AssertGetExpectedContent, expected.ToString(Formatting.None));

            EmailNotificationResponse response = await client.SendEmailAsync(Constants.fakeEmail, Constants.fakeTemplateId, personalisation, Constants.fakeNotificationReference);
        }
コード例 #5
0
        private async Task <string> SendEmail(string toAddress, string templateId, Dictionary <string, dynamic> placeholders)
        {
            var apiKey = ConfigurationManager.AppSettings["NotifyAPIKey"];
            var client = new NotificationClient(apiKey);

            var response = await client.SendEmailAsync(
                emailAddress : toAddress,
                templateId : templateId,
                personalisation : placeholders
                );

            return(response.reference);
        }