How to Play IVR after Receiving PSTN inbound call incommingcall Event

Soundhar M 50 Reputation points
2024-07-03T07:55:43.02+00:00

How to play IVR(Inbound call) in my scenario. I have a PSTN Number when customer call this Number I Received Incoming call Event via Webhook after this Event received I want to play the IVR to the caller(customer), Could you provide me a solution to play IVR in this Scenario

Azure Communication Services
Azure Communication Services
An Azure communication platform for deploying applications across devices and platforms.
941 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. brtrach-MSFT 16,926 Reputation points Microsoft Employee
    2024-12-19T05:45:36.5633333+00:00

    Below is a quick overview I put together:

    To play an IVR (Interactive Voice Response) message to a caller using Azure Communication Services (ACS) after receiving an inbound PSTN call, you can follow these general steps:

    1. Set Up Azure Communication Services:
      1. Ensure you have an ACS resource set up in your Azure portal.
      2. Ensure you have a PSTN number associated with your ACS resource.
    2. Set Up a Webhook to Receive Incoming Call Events:
      1. Create a webhook endpoint on your server to handle incoming call events.
      2. Register this webhook URL with ACS to receive incoming call notifications. You can do this by setting up an Event Grid subscription to your ACS resource.
    3. Handle the Incoming Call Event:
      1. When an inbound call is received, ACS will send an event to your webhook. This event contains details about the incoming call.
      2. Parse the event to extract necessary information such as the caller ID and the call ID.
    4. Play IVR to the Caller:
      1. Use the Azure Communication Services SDK to interact with the call.
      2. You can use the CallAutomationClient class from the ACS SDK to handle the call.
      3. Use the PlayAudio operation to play a pre-recorded IVR message to the caller. You need to have the audio file hosted on a publicly accessible URL.

    Here's a basic example in C# to illustrate how you might handle this:

    using Azure.Communication.CallingServer;
    using Azure.Messaging.EventGrid;
    using System;
    using System.Threading.Tasks;
    
    public class IVRService
    {
        private readonly string connectionString = "<YOUR_ACS_CONNECTION_STRING>";
        private readonly CallAutomationClient callAutomationClient;
    
        public IVRService()
        {
            callAutomationClient = new CallAutomationClient(connectionString);
        }
    
        public async Task HandleIncomingCall(EventGridEvent eventGridEvent)
        {
            // Parse the incoming call event
            var incomingCallEvent = eventGridEvent.Data.ToObjectFromJson<IncomingCallEventData>();
    
            // Extract the call ID and caller ID
            var callId = incomingCallEvent.CallId;
    
            // Play IVR message
            await PlayIVRMessage(callId);
        }
    
        private async Task PlayIVRMessage(string callId)
        {
            var audioUrl = new Uri("https://<YOUR_AUDIO_FILE_URL>.wav");
            var playSource = new FileSource(audioUrl);
    
            // Play the audio to the caller
            await callAutomationClient.GetCallConnection(callId).PlayAsync(playSource);
        }
    }
    
    
    

    Additional thoughts:

    • Audio File: Ensure your audio file is in a supported format (e.g., WAV) and hosted on a publicly accessible URL.
    • Event Grid: You may need to set up an Event Grid subscription for your ACS resource to route events to your webhook.
    • Security: Consider security measures for your webhook, such as validating incoming requests to ensure they originate from Azure.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.