This examples shows how to create outbound calls from a list of contacts
C# JavaScript Other
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
App_Start
Content
Controllers
Models
Properties
README_Images
Scripts
Views
.gitignore
CODE_OF_CONDUCT.md
CONTRIBUTING.md
CallApp.csproj
CallApp.sln
Global.asax
Global.asax.cs
ISSUE_TEMPLATE.md
LICENSE
PULLREQUEST.md
PULL_REQUEST_TEMPLATE.md
README.md
Startup.cs
Web.Debug.config
Web.Release.config
Web.config
azuredeploy.json
packages.config

README.md

CSharp Call Contacts Example

This application is outdated, but will be updated soon!

Catapult Api demo app (creating calls and bridges)

Demonstrates

Prerequisites

Build and Deploy

Azure One Click

Settings Required To Run

  • Catapult User Id
  • Catapult Api Token
  • Catapult Api Secret

Deploy to Azure

Locally

Clone the repository

git clone https://github.com/BandwidthExamples/csharp-call-contacts-example.git

Open this solution in Visual Studio.

Fill sections of Web.config with valid values: userId, apiToken, apiSecret are Catapult API auth data (you can get them on tab "Account" of https://catapult.inetwork.com/pages/catapult.jsf)

Build this solution. Missing modules will be downloaded on the build by nuget.

Click right button on thie project in solution explorer and select "Publish". Select "Microsoft Azure Websites" in opened dialog, then sign in on Azure and select exisitng website to deploy this project or create new (button "New"). If you create new site enter site name, select subscription and region. Database is not required. Change site profile options if need and press "Publish" to upload it on Azure. Now you can open it in the browser.

How it works

Basic Call Flow Chart

Basic Flow Chart

Create outbound call and set the callbackURL to the server's address

var cId = await Client.Call.CreateAsync(new CreateCallData
{
    From = PhoneNumberForCallbacks,
    To = call.From,
    CallbackUrl = BaseUrl + Url.Action("CatapultFromCallback"),
    Tag = call.To
});

Play Audio after answer event

case CallbackEventType.Answer:
{
    var contact = DbContext.Contacts.FirstOrDefault(c => c.PhoneNumber == ev.Tag);
    if (contact == null)
    throw new Exception("Missing contact for number " + ev.Tag);
    await Client.Call.SpeakSentenceAsync(ev.CallId,
    string.Format("We are connecting you to {0}", contact.Name), Gender.Female, "susan", "en_US", ev.Tag);
    break;
}

Wait until speak event is over and make 2nd call

case CallbackEventType.Speak:
{
    if (ev.Status != CallbackEventStatus.Done) break;
    //a messages was spoken to "from" number. calling to "to" number
    var cId = await Client.Call.CreateAsync(new CreateCallData
    {
    From = PhoneNumberForCallbacks,
    To = ev.Tag,
    CallbackUrl = BaseUrl + Url.Action("CatapultToCallback"),
    Tag = ev.CallId
    });
    Debug.WriteLine("Call Id to {0} is {1}", ev.Tag, cId);
    break;
}

Once 2nd call is answered, create bridge and place calls in bridge

case CallbackEventType.Answer:
{
    //"to" number answered a call. Making a bridge with "from" number's call
    var bId = await Client.Bridge.CreateAsync(new CreateBridgeData
    {
    BridgeAudio = true,
    CallIds = new[] {ev.CallId, ev.Tag}
    });
    var activeCalls = HttpContext.Application["ActiveCalls"] as Dictionary<string, string> ??
                    new Dictionary<string, string>();
    activeCalls.Add(ev.CallId, ev.Tag);
    activeCalls.Add(ev.Tag, ev.CallId);
    HttpContext.Application["ActiveCalls"] = activeCalls;
    Debug.WriteLine(string.Format("BridgeId is {0}", bId));
    break;
}

When one call leg is ended, be sure to end the other

private async Task HandleHangup(CallbackEvent ev)
{
    var activeCalls = HttpContext.Application["ActiveCalls"] as Dictionary<string, string> ??
                    new Dictionary<string, string>();
    //hang up another leg
    string anotherCallId;
    if (activeCalls.TryGetValue(ev.CallId, out anotherCallId))
    {
    activeCalls.Remove(ev.CallId);
    activeCalls.Remove(anotherCallId);
    Debug.WriteLine("Hang up another call of bridge (id {0})", anotherCallId);
    try
    {
        await Client.Call.HangupAsync(anotherCallId);
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Error on hang up another call (id {0}) of the bridge: {1}", anotherCallId,
        ex.Message);
    }
    }
}

Init

Search then order phone number

await client.AvailableNumber.SearchLocalAsync(new LocalNumberQuery
{
    State = "NC",
    City = "Cary",
    Quantity = 1
});
phoneNumber = numbers.First().Number;
await client.PhoneNumber.CreateAsync(new CreatePhoneNumberData {Number = phoneNumber});

Deploy and Demo

Deploy

Click the 'Deploy to Azure' Button and enter Catapult Creds

Fill in information

Click Deploy

Click Manage your resources

Find the newly Deployed App

Click the URL to open the web page

Demo

Add a contact (with real phone number)

Click to add contact

Adding Contact

Click to call contact

Click to call contact

Enter your 'From' phone number and click call

Add your number