Example #1
0
        public async Task<SmsResult> SendAsync(App app, string to, string body, string token,
            CancellationToken ct = default)
        {
            try
            {
                var callbackUrl = smsUrl.SmsWebhookUrl(app.Id, integrationId);

                var sms = new MessageBirdSmsMessage(to, body, token, callbackUrl);

                var response = await messageBirdClient.SendSmsAsync(sms, ct);

                if (response.Recipients.TotalSentCount != 1)
                {
                    var errorMessage = string.Format(CultureInfo.CurrentCulture, Texts.MessageBird_ErrorUnknown, to);

                    throw new DomainException(errorMessage);
                }

                return SmsResult.Sent;
            }
            catch (ArgumentException ex)
            {
                var errorMessage = string.Format(CultureInfo.CurrentCulture, Texts.MessageBird_Error, to, ex.Message);

                throw new DomainException(errorMessage);
            }
        }
        public async Task Should_send_sms()
        {
            var sms = new MessageBirdSmsMessage("4917683297281", "Hello");

            var response = await sut.SendSmsAsync(sms);

            Assert.Equal(1, response.Recipients.TotalSentCount);
        }
Example #3
0
        public async Task <MessageBirdSmsResponse> SendSmsAsync(MessageBirdSmsMessage message,
                                                                CancellationToken ct)
        {
            Guard.NotNull(message);
            Guard.NotNullOrEmpty(message.Body, nameof(message.Body));
            Guard.NotNullOrEmpty(message.To, nameof(message.To));

            var(to, body, reference, reportUrl) = message;

            if (body.Length > 140)
            {
                throw new ArgumentException("Text must not have more than 140 characters.", nameof(message));
            }

            to = PhoneNumberUtil.Normalize(to).TrimStart(TrimChars);

            if (!long.TryParse(to, NumberStyles.Integer, CultureInfo.InvariantCulture, out var recipient))
            {
                throw new ArgumentException("Not a valid phone number.", nameof(message));
            }

            using (var client = httpClientFactory.CreateClient())
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("AccessKey", options.AccessKey);

                var request = new
                {
                    originator = GetOriginator(to),
                    body,
                    reportUrl,
                    reference,
                    recipients = new[]
                    {
                        recipient
                    }
                };

                var response = await client.PostAsJsonAsync("https://rest.messagebird.com/messages", request, ct);

                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadFromJsonAsync <MessageBirdSmsResponse>((JsonSerializerOptions?)null, ct);

                    return(result !);
                }

                throw await HandleErrorAsync(response, ct);
            }
        }
Example #4
0
        public async Task <SmsResult> SendAsync(string to, string body, string?token, CancellationToken ct)
        {
            try
            {
                var sms = new MessageBirdSmsMessage(to, body, token, smsUrl.WebhookUrl());

                var response = await smsClient.SendSmsAsync(sms, ct);

                if (response.Recipients.TotalSentCount != 1)
                {
                    throw new DomainException($"Failed to send sms to {to}.");
                }

                return(SmsResult.Sent);
            }
            catch (ArgumentException ex)
            {
                throw new DomainException($"Failed to send sms to {to}: {ex.Message}.", ex);
            }
        }