我有一个azure函数设置为从事件中心读取以通过sendgrid或twilio发送警报消息。我希望能够告诉我何时发送SendGrid消息“await smsCollector.AddAsync(mobileMessage)”是否已成功发送 - 即电子邮件无效。这个设置可以实现吗?
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);
}
}
}
你可以打电话给IAsyncCollector
has a FlushAsync
method。 AddAsync
可以缓冲项目,但FlushAsync
将强制它们通过(并在出错时引发异常)。