Skip to content
A simple Voice app that demonstrates the use of BXML using Bandwidth's Voice API, C#, and ASP.NET
C#
Branch: master
Clone or download
Latest commit 6d3bc3a Sep 18, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vs done Sep 18, 2018
BXMLsample done Sep 18, 2018
.DS_Store testing bandwidth.net.callbackevent Sep 18, 2018
BXMLsample 3.35.35 PM.sln first test Sep 13, 2018
README.md done Sep 18, 2018

README.md

Simple BXML Sample

A simple Voice app that demonstrates the use of BXML using Bandwidth's Voice API, C#, and ASP.NET.

Table of Contents

Prereqs

How It Works

This app provides a very simple example of how to use BXML with a Bandwidth Voice callback in order to send audio back to an end user.

Prep

Make sure you have a Bandwidth account with at least one number set up to receive Voice calls. Make sure that the application for this number is set to auto-answer, because BXML itself cannot answer a call. Also make sure the Callback Request Method of your application is set to GET.

Create a New Solution in Visual Studio

The best solution type to use is ASP.NET Core Web API. Call it BXMLsample or whatever you choose. Open the ValuesController.cs file and delete the code that is already there. This guide will walk you through all the code you need.

Set Up the Namespace

The only namespace you need is the following:

using Microsoft.AspNetCore.Mvc;
using Bandwidth.Net;

Initiate API Routes

namespace BXMLsample.Controllers
{
    [Route("api")]
    [ApiController]
    public class ValuesController : ControllerBase
    {

    }
}

Create a Function that Creates a BXML Sequence

//within public class ValuesController : ControllerBase
public string CreateBXML()
{
    string bxmlString = "";
    bxmlString += "<?xml version='1.0' encoding='UTF-8' ?>";
    bxmlString += "<Response>";
    bxmlString += "<SpeakSentence voice='susan' locale='en_US' gender='female'>";
    bxmlString += "Hi, thanks for calling. Goodbye";
    bxmlString += "</SpeakSentence>";
    bxmlString += "<Hangup></Hangup>";
    bxmlString += "</Response>";

    return bxmlString;
}

Create Callback Endpoint That Returns BXML

This endpoint checks that the EventType of the callback is CallbackEventType.Answer. If it is, then the endpoint returns CreateBXML(). If it isn't, then the endpoint returns null.

//also within public class ValuesController : ControllerBase
[HttpGet("bxml")]
public string BXML([FromQuery] CallbackEvent callback)
{

    if (callback.EventType == CallbackEventType.Answer)
    {
        return CreateBXML();
    }
    else
    {
        return null;
    }
}

Deploy

One easy way to deploy this solution is using a Microsoft Azure account. You can sign up for an account here and read the Azure docs here.

Once your solution is deployed, set the Voice callback URL of your BXML application to https://[YOUR_DEPLOYED_SOLUTION_URL]/api/bxml.

Now when someone calls a phone number associated with your BXML application, they will hear brief computer-generated speech and the application will automatically hang up.

You can’t perform that action at this time.