从Azure功能v2配置Twilio SMS

从Azure功能v2配置Twilio SMS,azure,twilio,azure-functions,sendgrid,Azure,Twilio,Azure Functions,Sendgrid,我有一些代码,我从Azure事件中心读取消息,我想发送电子邮件或短信 电子邮件正在通过发送网格工作,但我不确定如何配置SMS部分 我想我应该使用Twilio,下面是我的代码示例。“messageCollector”用于发送电子邮件,因为本地json中有一些SendGrid配置。如何配置Twilio [FunctionName("SendAlert")] public static async Task Run( [EventHubTrigger("v1-email

我有一些代码,我从Azure事件中心读取消息,我想发送电子邮件或短信

电子邮件正在通过发送网格工作,但我不确定如何配置SMS部分

我想我应该使用Twilio,下面是我的代码示例。“messageCollector”用于发送电子邮件,因为本地json中有一些SendGrid配置。如何配置Twilio

    [FunctionName("SendAlert")]
    public static async Task Run(
        [EventHubTrigger("v1-email-hub", Connection = "EventHubConnection")] EventData[] events,
        [SendGrid] IAsyncCollector<SendGridMessage> messageCollector,
        [TwilioSms] IAsyncCollector<CreateMessageOptions> smsCollector,
        [Inject] NotificationEventLogic eventLogic,
        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)
            {



                if (ev.NotificationEventType == NotificationEventType.Email)
                {
                    var message = new SendGridMessage();

                    // ... ... make message and add it
                    await messageCollector.AddAsync(message);
                }
                else if (ev.NotificationEventType == NotificationEventType.SMS)
                {
                    // Not sure how to get this to work
                    var mobileMessage = new CreateMessageOptions(new PhoneNumber(ev.Data))
                    {
                        Body = $"Notification {ev.NotificationId}"
                    };

                    await smsCollector.AddAsync(mobileMessage);
                }


                // await smsCollector.AddAsync()
                await eventLogic.CreateEventAsync(ev);
            }

        }
    }
[FunctionName(“SendAlert”)]
公共静态异步任务运行(
[EventHubTrigger(“v1电子邮件中心”,Connection=“EventHubConnection”)]EventData[]事件,
[SendGrid]IAsyncCollector messageCollector,
[TwilioSms]IAsyncCollector SMSCCollector,
[Inject]NotificationEventLogic事件逻辑,
ILogger日志)
{
foreach(事件中的事件数据EventData)
{
string messageBody=Encoding.UTF8.GetString(eventData.Body.Array、eventData.Body.Offset、eventData.Body.Count);
var notificationEvents=JsonConvert.DeserializeObject(messageBody);
foreach(通知事件中的var ev)
{
if(ev.NotificationEventType==NotificationEventType.Email)
{
var message=new SendGridMessage();
//……制作消息并添加它
wait messageCollector.AddAsync(message);
}
else if(ev.NotificationEventType==NotificationEventType.SMS)
{
//我不知道该怎么做
var mobileMessage=new CreateMessageOptions(新电话号码(ev.Data))
{
正文=$“通知{ev.NotificationId}”
};
等待smsCollector.AddAsync(mobileMessage);
}
//等待smsCollector.AddAsync()
等待eventLogic.CreateEventAsync(ev);
}
}
}

您需要在属性中配置它

[TwilioSms(AccountSidSetting = "TwilioAccountSid", AuthTokenSetting = "TwilioAuthToken", From = "+1425XXXXXXX")]
正如书中提到的

必须设置此值 保存您的Twilio帐户Sid的应用程序设置的名称,例如。 我是希德。如果未设置,则默认应用程序设置名称为 “AzureWebJobsTwilioAccountSid”

TwilioAuthToken此值必须设置为 保存您的Twilio身份验证令牌的应用程序设置的名称 e、 g.TwilioAccountAuthToken。如果未设置,则为默认应用程序设置名称 是“AzureWebJobsTwilioAuthToken”