Exemplo n.º 1
0
        /// <summary> Sends an SMS message from a phone number that belongs to the authenticated account. </summary>
        /// <param name="from"> The sender&apos;s phone number in E.164 format that is owned by the authenticated account. </param>
        /// <param name="to"> The recipient&apos;s phone number in E.164 format. In this version, up to 100 recipients in the list is supported. </param>
        /// <param name="message"> The contents of the message that will be sent to the recipient. The allowable content is defined by RFC 5724. </param>
        /// <param name="options"> Optional configuration for sending SMS messages. </param>
        /// <param name="cancellationToken"> The cancellation token to use. </param>
        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="from"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="to"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
        public virtual Response <IReadOnlyList <SmsSendResult> > Send(string from, IEnumerable <string> to, string message, SmsSendOptions options = default, CancellationToken cancellationToken = default)
        {
            using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(SmsClient)}.{nameof(Send)}");
            scope.Start();
            try
            {
                Argument.AssertNotNullOrEmpty(from, nameof(from));
                Argument.AssertNotNullOrEmpty(to, nameof(to));

                IEnumerable <SmsRecipient> recipients = to.Select(x =>
                                                                  new SmsRecipient(AssertNotNullOrEmpty(x, nameof(to)))
                {
                    RepeatabilityRequestId = Guid.NewGuid().ToString(),
                    RepeatabilityFirstSent = DateTimeOffset.UtcNow.ToString("r", CultureInfo.InvariantCulture),
                });

                Response <SmsSendResponse> response = RestClient.Send(from, recipients, message, options, cancellationToken);
                return(Response.FromValue(response.Value.Value, response.GetRawResponse()));
            }
            catch (Exception ex)
            {
                scope.Failed(ex);
                throw;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sends a SMS <paramref name="from"/> a phone number that is acquired by the authenticated account, <paramref name="to"/> another phone number.
        /// </summary>
        /// <param name="from">The sender's phone number that is owned by the authenticated account.</param>
        /// <param name="to">The recipient's phone number.</param>
        /// <param name="message">The contents of the message that will be sent to the recipient. The allowable content is defined by RFC 5724. If the message has more than 160 characters, the server will split it into multiple SMSs automatically.</param>
        /// <param name="options">Optional configuration for sending SMS messages.</param>
        /// <param name="cancellationToken">The cancellation token for the underlying request.</param>
        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="from"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="to"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
        public virtual Response <SmsSendResult> Send(string from, string to, string message, SmsSendOptions options = default, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNullOrEmpty(from, nameof(from));
            Argument.AssertNotNullOrEmpty(to, nameof(to));
            Response <IEnumerable <SmsSendResult> > allSmsSendResults = Send(from, new[] { to }, message, options, cancellationToken);
            SmsSendResult sendSMSResult = allSmsSendResults.Value.First();

            return(Response.FromValue(sendSMSResult, allSmsSendResults.GetRawResponse()));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sends a SMS <paramref name="from"/> a phone number that is acquired by the authenticated account, <paramref name="to"/> another phone number.
        /// </summary>
        /// <param name="from">The sender's phone number that is owned by the authenticated account.</param>
        /// <param name="to">The recipient's phone number.</param>
        /// <param name="message">The contents of the message that will be sent to the recipient. The allowable content is defined by RFC 5724. If the message has more than 160 characters, the server will split it into multiple SMSs automatically.</param>
        /// <param name="options">Optional configuration for sending SMS messages.</param>
        /// <param name="cancellationToken">The cancellation token for the underlying request.</param>
        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="from"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="to"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
        public virtual Response <SmsSendResult> Send(string from, string to, string message, SmsSendOptions options = default, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNullOrEmpty(from, nameof(from));
            Argument.AssertNotNullOrEmpty(to, nameof(to));
            Response <IReadOnlyList <SmsSendResult> > response = Send(from, new[] { to }, message, options, cancellationToken);

            return(Response.FromValue(response.Value[0], response.GetRawResponse()));
        }
        public async Task <Response <SmsSendResponse> > SendAsync(string @from, IEnumerable <SmsRecipient> smsRecipients, string message, SmsSendOptions smsSendOptions = null, CancellationToken cancellationToken = default)
        {
            if (@from == null)
            {
                throw new ArgumentNullException(nameof(@from));
            }
            if (smsRecipients == null)
            {
                throw new ArgumentNullException(nameof(smsRecipients));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            using var message0 = CreateSendRequest(@from, smsRecipients, message, smsSendOptions);
            await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false);

            switch (message0.Response.Status)
            {
            case 202:
            {
                SmsSendResponse value = default;
                using var document = await JsonDocument.ParseAsync(message0.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                value = SmsSendResponse.DeserializeSmsSendResponse(document.RootElement);
                return(Response.FromValue(value, message0.Response));
            }
        internal HttpMessage CreateSendRequest(string @from, IEnumerable <SmsRecipient> smsRecipients, string message, SmsSendOptions smsSendOptions)
        {
            var message0 = _pipeline.CreateMessage();
            var request  = message0.Request;

            request.Method = RequestMethod.Post;
            var uri = new RawRequestUriBuilder();

            uri.AppendRaw(_endpoint, false);
            uri.AppendPath("/sms", false);
            uri.AppendQuery("api-version", _apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("Content-Type", "application/json");
            var model = new SendMessageRequest(@from, smsRecipients.ToList(), message)
            {
                SmsSendOptions = smsSendOptions
            };
            var content = new Utf8JsonRequestContent();

            content.JsonWriter.WriteObjectValue(model);
            request.Content = content;
            return(message0);
        }
Exemplo n.º 6
0
 /// <summary> Sends an SMS message from a phone number that belongs to the authenticated account. </summary>
 /// <param name="from"> The sender&apos;s phone number in E.164 format that is owned by the authenticated account. </param>
 /// <param name="to"> The recipient&apos;s phone number in E.164 format. In this version, up to 100 recipients in the list is supported. </param>
 /// <param name="message"> The contents of the message that will be sent to the recipient. The allowable content is defined by RFC 5724. </param>
 /// <param name="options"> Optional configuration for sending SMS messages. </param>
 /// <param name="cancellationToken"> The cancellation token to use. </param>
 /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="from"/> is null.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="to"/> is null.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
 public virtual AsyncPageable <SmsSendResult> SendAsync(string from, IEnumerable <string> to, string message, SmsSendOptions options = default, CancellationToken cancellationToken = default)
 => PageResponseEnumerator.CreateAsyncEnumerable(async(continuationToken) =>
Exemplo n.º 7
0
        /// <summary>
        /// Sends a SMS <paramref name="from"/> a phone number that is acquired by the authenticated account, <paramref name="to"/> another phone number.
        /// </summary>
        /// <param name="from">The sender's phone number that is owned by the authenticated account.</param>
        /// <param name="to">The recipient's phone number.</param>
        /// <param name="message">The contents of the message that will be sent to the recipient. The allowable content is defined by RFC 5724. If the message has more than 160 characters, the server will split it into multiple SMSs automatically.</param>
        /// <param name="options">Optional configuration for sending SMS messages.</param>
        /// <param name="cancellationToken">The cancellation token for the underlying request.</param>
        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="from"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="to"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
        public virtual Response <SmsSendResult> Send(string from, string to, string message, SmsSendOptions options = default, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNullOrEmpty(from, nameof(from));
            Argument.AssertNotNullOrEmpty(to, nameof(to));
            Pageable <SmsSendResult> allSmsSendResults = Send(from, new[] { to }, message, options, cancellationToken);
            Page <SmsSendResult>     firstPage         = allSmsSendResults.AsPages().First();

            return(Response.FromValue(firstPage.Values[0], firstPage.GetRawResponse()));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Sends a SMS <paramref name="from"/> a phone number that is acquired by the authenticated account, <paramref name="to"/> another phone number.
        /// </summary>
        /// <param name="from">The sender's phone number that is owned by the authenticated account.</param>
        /// <param name="to">The recipient's phone number.</param>
        /// <param name="message">The contents of the message that will be sent to the recipient. The allowable content is defined by RFC 5724. If the message has more than 160 characters, the server will split it into multiple SMSs automatically.</param>
        /// <param name="options">Optional configuration for sending SMS messages.</param>
        /// <param name="cancellationToken">The cancellation token for the task.</param>
        /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> for details returned from the server.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="from"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="to"/> is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null.</exception>
        public virtual async Task <Response <SmsSendResult> > SendAsync(string from, string to, string message, SmsSendOptions options = default, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNullOrEmpty(from, nameof(from));
            Argument.AssertNotNullOrEmpty(to, nameof(to));
            AsyncPageable <SmsSendResult> allSmsSendResults = SendAsync(from, new[] { to }, message, options, cancellationToken);

            await foreach (Page <SmsSendResult> page in allSmsSendResults.AsPages().ConfigureAwait(false))
            {
                return(Response.FromValue(page.Values[0], page.GetRawResponse()));
            }

            throw new InvalidOperationException("Sequence contains no matching element");
        }