/// <inheritdoc cref="NotificationProvider.Send(MessageParameterCollection)"/>
        public override async Task <NotificationResult> Send(MessageParameterCollection messageParameters)
        {
            var smsMessage = new SMSMessage(messageParameters);

            PhoneNumber fromNumber;

            if (!string.IsNullOrEmpty(smsMessage.FromNumber))
            {
                fromNumber = new PhoneNumber(smsMessage.FromNumber);
            }
            else if (_options != null && !string.IsNullOrEmpty(_options.DefaultFromNumber))
            {
                fromNumber = new PhoneNumber(_options.DefaultFromNumber);
            }
            else
            {
                return(new NotificationResult(new List <string> {
                    "From Number should not be empty"
                }));
            }

            Logger.LogDebug(TwilioLogMessages.Sending_Start, smsMessage.ToNumber);

            try
            {
                var message = await MessageResource.CreateAsync(
                    new PhoneNumber(smsMessage.ToNumber),
                    from : fromNumber,
                    body : smsMessage.Content,
                    client : _twilioRestClient);

                Logger.LogDebug(TwilioLogMessages.Sending_End, smsMessage.ToNumber, message.Sid);
                return(new NotificationResult(true, new Dictionary <string, object> {
                    { nameof(message.Sid), message.Sid }
                }));
            }
            catch (Exception ex)
            {
                Logger.LogWarning(ex, TwilioLogMessages.Sending_Failed, smsMessage.ToNumber, ex.Message);
                return(new NotificationResult(new List <string> {
                    ex.Message
                }));
            }
        }
示例#2
0
 /// <summary>
 /// Send message with <see cref="TwilioProvider"/>
 /// </summary>
 /// <param name="notification">The notification object</param>
 /// <param name="message">The wrapper of SMS to be sent</param>
 /// <returns></returns>
 public static Task <NotificationResult> SendSmsWithTwilio(this INotification notification, SMSMessage message)
 {
     return(notification.Send(TwilioConstants.DefaultName, message.ToParameters()));
 }