0
votes

I have an azure function set up to read from an event hub to send out alert messages via sendgrid or twilio. I want to be able to tell when I send out the SendGrid message "await smsCollector.AddAsync(mobileMessage)" whether it got sent successfully -- i.e. the email was not invalid. Is this possible with this set up?

         public static class SendAlert
         {
        [FunctionName("v1_EventHub_SendAlert")]
        public static async Task Run(
            [EventHubTrigger("v1-email-hub", Connection = "EventHubConnection")] EventData[] events,
            [SendGrid] IAsyncCollector<SendGridMessage> messageCollector,
            [TwilioSms(From = "xXXXxxXxxx)] IAsyncCollector<CreateMessageOptions> smsCollector,
            [Inject] NotificationEventLogic eventLogic,
            [Inject] NotificationLogic notificationLogic,
            ILogger log)
        {
            foreach (EventData eventData in events)
            {
                string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);

                var notificationEvents = JsonConvert.DeserializeObject<List<NotificationEvent>>(messageBody);

                foreach (var ev in notificationEvents)
                {
                    var notification = await notificationLogic.GetNotificationAsync(ev.NotificationId);

                    if (ev.NotificationEventType == NotificationEventType.Email)
                    {
                        var message = CreateEmail(ev, notification);
                        await messageCollector.AddAsync(message);
                    }
                    else if (ev.NotificationEventType == NotificationEventType.SMS)
                    {
                        var mobileMessage = CreateSms(ev, notification);

                        await smsCollector.AddAsync(mobileMessage);
                    }

                    await eventLogic.CreateEventAsync(ev);
                }

            }
        }
1

1 Answers

0
votes

IAsyncCollector has a FlushAsync method that you can call. AddAsync can buffer items, but FlushAsync will force them through (and raise an exception on error).