从Azure Functions v2配置Twilio SMS

问题描述 投票:1回答:1

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

电子邮件正在通过发送网格,但我不知道如何配置SMS部分。

我想我想要使用Twilio,这里是我的代码的样本。 “messageCollector”适用于发送Email,因为本地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);
            }

        }
    }
azure twilio azure-functions sendgrid
1个回答
1
投票

您需要在属性中配置它

[TwilioSms(AccountSidSetting = "TwilioAccountSid", AuthTokenSetting = "TwilioAuthToken", From = "+1425XXXXXXX")]

正如它在documentation中提到的那样

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

TwilioAuthToken必须将此值设置为包含Twilio身份验证令牌的应用程序设置的名称,例如TwilioAccountAuthToken。如果未设置,则默认应用程序设置名称为“AzureWebJobsTwilioAuthToken”。

© www.soinside.com 2019 - 2024. All rights reserved.