Ejemplo n.º 1
0
        public async Task Should_send_voice()
        {
            var voice = new MessageBirdVoiceMessage("4917683297281", "Guten Tag, wie geht es ihnen?", "de-de");

            var response = await sut.SendVoiceAsync(voice);

            Assert.Equal(1, response.Recipients.TotalSentCount);
        }
Ejemplo n.º 2
0
        public async Task <MessageBirdSmsResponse> SendVoiceAsync(MessageBirdVoiceMessage message, CancellationToken ct)
        {
            Guard.NotNull(message, nameof(message));
            Guard.NotNullOrEmpty(message.Body, nameof(message.Body));
            Guard.NotNullOrEmpty(message.To, nameof(message.To));

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

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

            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.To));
            }

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

                var request = new
                {
                    originator = GetOriginator(to),
                    body,
                    language,
                    ifMachine = "continue",
                    reportUrl,
                    reference,
                    machineTimeout = 400,
                    recipients     = new[]
                    {
                        recipient
                    }
                };

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

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