public async Task <ActionResult> RecordingAvailableCallback()
        {
            _logger.LogInformation($"{Request.Path.Value} requested.");

            // Read in the json body.
            using var reader = new StreamReader(Request.Body, Encoding.UTF8);
            var body = await reader.ReadToEndAsync();

            var json = JObject.Parse(body);

            // Grab the event type from the json response.
            var eventType = (string)json["eventType"];

            // We're only interested if the event type is "recordingAvailable".
            if (eventType == "recordingAvailable")
            {
                var client = new BandwidthClient
                             .Builder()
                             .Environment(Bandwidth.Standard.Environment.Production)
                             .VoiceBasicAuthCredentials(Username, Password)
                             .Build();

                // We're using the file format to create the local file's extension.
                var fileFormat = (string)json["fileFormat"];

                // Create a local path for the recording file to be saved.
                var path = Path.Combine(
                    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                    Path.ChangeExtension("MyRecording", fileFormat)
                    );
                using var fileStream = new FileStream(path, FileMode.Create);

                var callId      = (string)json["callId"];
                var recordingId = (string)json["recordingId"];

                // Get the recording's stream and copy it to the local file's stream.
                var response = await client.Voice.APIController.GetStreamRecordingMediaAsync(AccountId, callId, recordingId);

                await response.Data.CopyToAsync(fileStream);
            }

            return(new OkResult());
        }
Beispiel #2
0
        public static async Task Main(string[] args)
        {
            BandwidthClient client = new BandwidthClient.Builder()
                                     .Environment(Bandwidth.Standard.Environment.Production)
                                     .MessagingBasicAuthCredentials(API_TOKEN, API_SECRET)
                                     .Build();
            APIController msgController = client.Messaging.APIController;

            string dog1    = "dog.jpg";
            string dog2    = "critter.jpg";
            string dogUrl1 = $"https://messaging.bandwidth.com/api/v2/users/{ACCOUNT_ID}/media/{dog1}";
            string dogUrl2 = $"https://messaging.bandwidth.com/api/v2/users/{ACCOUNT_ID}/media/{dog2}";

            using (FileStream fs = File.OpenRead(dog1))
            {
                FileStreamInfo fsi = new FileStreamInfo(fs, dog1, "image/jpg");
                try
                {
                    await msgController.UploadMediaAsync(ACCOUNT_ID, dog1, fs.Length, fsi);
                }
                catch (MessagingException e)
                {
                    string body = ((Bandwidth.Standard.Http.Response.HttpStringResponse)e.HttpContext.Response).Body;
                    Console.WriteLine($"Failed Uploading Media: {e.Message}\n{body}");
                    System.Environment.Exit(-1);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Something unknown went wrong");
                    Console.WriteLine(e.Message);
                    System.Environment.Exit(-1);
                }
            }
            using (FileStream fs = File.OpenRead(dog2))
            {
                FileStreamInfo fsi = new FileStreamInfo(fs, dog2, "image/jpg");
                try
                {
                    await msgController.UploadMediaAsync(ACCOUNT_ID, dog2, fs.Length, fsi);
                }
                catch (MessagingException e)
                {
                    string body = ((Bandwidth.Standard.Http.Response.HttpStringResponse)e.HttpContext.Response).Body;
                    Console.WriteLine($"Failed Uploading Media: {e.Message}\n{body}");
                    System.Environment.Exit(-1);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Something unknown went wrong");
                    Console.WriteLine(e.Message);
                    System.Environment.Exit(-1);
                }
            }
            MessageRequest myMessage = new MessageRequest();

            myMessage.ApplicationId = APP_ID;
            myMessage.To            = new List <string> {
                TO_PHONE_NUMBER
            };
            myMessage.From  = BANDWIDTH_PHONE_NUMBER;
            myMessage.Text  = "👋 🐶";
            myMessage.Media = new List <string> {
                dogUrl1, dogUrl2
            };
            try
            {
                ApiResponse <BandwidthMessage> apiResponse = (await msgController.CreateMessageAsync(ACCOUNT_ID, myMessage));
                Console.WriteLine($"Message ID: {apiResponse.Data.Id}");
            }
            catch (MessagingException e)
            {
                string body = ((Bandwidth.Standard.Http.Response.HttpStringResponse)e.HttpContext.Response).Body;
                Console.WriteLine($"Failed Sending Message: {e.Message}\n{body}");
                System.Environment.Exit(-1);
            }
            catch (Exception e)
            {
                Console.WriteLine("Something unknown went wrong");
                Console.WriteLine(e.Message);
                System.Environment.Exit(-1);
            }
        }
        public async Task <string> MessageCallback()
        {
            string json;

            using (var reader = new StreamReader(Request.Body))
            {
                json = await reader.ReadToEndAsync();
            }

            BandwidthCallbackMessage[] callbackMessages = ApiHelper.JsonDeserialize <BandwidthCallbackMessage[]>(json);
            BandwidthCallbackMessage   callbackMessage  = callbackMessages[0];
            BandwidthMessage           message          = callbackMessage.Message;

            if (message.Direction.ToLower().Trim().Equals("out"))
            {
                string logMessage = $"Message ID: {message.Id} DLR Status: {callbackMessage.Description}";
                Console.WriteLine(logMessage);
                return("");
            }

            string        owner   = message.Owner;
            List <string> numbers = new List <string>(message.To);

            numbers.Remove(owner);
            numbers.Add(message.MFrom);

            MessageRequest messageRequest = new MessageRequest();

            messageRequest.ApplicationId = msgApplicationId;
            messageRequest.To            = numbers;
            messageRequest.MFrom         = owner;

            bool isDog = message.Text.ToLower().Trim().Equals("dog");

            if (isDog)
            {
                List <string> media = new List <string>()
                {
                    "https://bw-demo.s3.amazonaws.com/dog.jpg"
                };
                messageRequest.Text  = "🐶";
                messageRequest.Media = media;
            }
            else
            {
                messageRequest.Text = "👋 Hello From Bandwidth!";
            }

            BandwidthClient client = new BandwidthClient.Builder()
                                     .Environment(Bandwidth.Standard.Environment.Production)
                                     .VoiceBasicAuthCredentials(bandwidthAPIUser, bandwidthAPIPassowrd)
                                     .MessagingBasicAuthCredentials(msgApiToken, msgApiSecret)
                                     .Build();

            Bandwidth.Standard.Messaging.Controllers.APIController          msgController = client.Messaging.APIController;
            Bandwidth.Standard.Http.Response.ApiResponse <BandwidthMessage> response      = await msgController.CreateMessageAsync(bandwidthAccountId, messageRequest);

            Console.WriteLine($"Sent message with ID: {response.Data.Id}");

            return("");
        }