Do we need azure functions to handle callbacks in outbound call ?

Nagane, Shahadatt 40 Reputation points
2023-10-09T09:31:19.43+00:00

Dear Team,

I hope you're all doing well. Currently, I'm facing a challenge while developing a .NET Core application. The goal of this application is to initiate outbound calls to end-user mobile numbers and play a sound when the user receives the call.

Here are the steps I've followed so far:

  1. I've successfully created an application that can place outbound calls to mobile numbers using the following libraries:

using Azure.Communication.CallAutomation
using Azure.Communication
using Azure.Messaging

CallAutomationClient callAutomationClient = new CallAutomationClient(connectionString);
CallInvite callInvite = new CallInvite(
    new PhoneNumberIdentifier(targetPhoneNumber),
    new PhoneNumberIdentifier(sourcePhoneNumber)
    );

CreateCallResult createCallResult = await callAutomationClient.CreateCallAsync(
    callInvite,
    new Uri(callbackUri)
    );
  1. After successfully initiating the call, my next objective is to play a sound (in .wav format) to the end user after he/she pickup the call.

Here's a code snippet for handling callback events:

app.MapPost("/CallBackEvent", async (CloudEvent[] cloudEvents, ILogger<Program> logger) =>
{
    foreach (var cloudEvent in cloudEvents)
    {
        CallAutomationEventBase parsedEvent = CallAutomationEventParser.Parse(cloudEvent);
        if (parsedEvent is CallConnected)
        {
            var playSource = new FileSource(new Uri(callbackUri + "/audio/welcomeMessage.wav"));
            var playResponse = callAutomationClient.GetCallConnection(createCallResult.CallConnection.CallConnectionId)
                .GetCallMedia()
                .PlayToAllAsync(playSource);
        }
    }
});

Now, I have some questions:

  1. How is the callback event currently triggered? I've been manually triggering it in Postman by passing the required data. However, my goal is to have it automatically triggered when the end user receives a call.
  2. Do I need to incorporate an Azure Function for this callback to trigger automatically when user receives a call?
  3. The above function isn't playing any audio even after triggered by postman with correct data. Could you provide guidance on what might be missing or causing this issue?

I've reviewed several samples and articles, but I haven't been able to achieve my goal. Any help or guidance would be greatly appreciated.

Thank you.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,412 questions
Azure Communication Services
Azure Communication Services
An Azure communication platform for deploying applications across devices and platforms.
712 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Franko Morales 25 Reputation points Microsoft Employee
    2023-10-10T03:40:24.4333333+00:00

    Hello,

    Franko from the Azure team here.
    Thank you so much for your question and interest in using our libraries.

    As an alternative of using the Azure Functions you described, you may use a webhook endpoint which relies on the Azure Dev Tunnel.
    You can see how to use such webhook on this quickstart, and to learn how to set them up you can follow this other one.

    Regarding your problem with the Play request: could you please provide your Azure ACS Resource, so we can check our logs to see what's happening?

    In the meantime while we investigate, you can check if you are passing a valid callConnectionId, or if you are correctly creating the URL where your WAV file is located. As a reference, in this quickstart, they are getting the callConnectionId from the event itself:

    app.MapPost("/api/callbacks", async (CloudEvent[] cloudEvents, ILogger<Program> logger) =>
    {
        foreach (var cloudEvent in cloudEvents)
        {
            logger.LogInformation($"Event received: {JsonConvert.SerializeObject(cloudEvent)}");
    
            CallAutomationEventBase parsedEvent = CallAutomationEventParser.Parse(cloudEvent);
            logger.LogInformation($"{parsedEvent?.GetType().Name} parsedEvent received for call connection id: {parsedEvent?.CallConnectionId}");
            var callConnection = callAutomationClient.GetCallConnection(parsedEvent.CallConnectionId);
            var callMedia = callConnection.GetCallMedia();
    
            if (parsedEvent is CallConnected)
            {
                //Handle Call Connected Event
            }
        }
    });
    

    Please, let me know your ACS resource for us to investigate further.

    Thank you.