如何在Azure中接听电话并将其传输到意图识别(Azure认知服务)

eqzww0vc  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(112)

我正在尝试接听电话并将其传输到认知服务。我该怎么做?
我试过:在Azure中设置电话号码,制作Azure事件网格,并设置一个webhook将事件发送到我的应用程序。但是,我似乎无法从接收到的对象中获得流。代码:

[AllowAnonymous]
[HttpPost]
public async Task<IActionResult> IncomingCall([FromBody] EventGridEvent[] events)
{
            int eventCount = 0;

            foreach (var eventGridEvent in events)
            {
                try
                {

                    switch (eventGridEvent.EventType)
                    {
                        case SystemEventNames.EventGridSubscriptionValidation:
                            {
                                var eventData = eventGridEvent.Data.ToObjectFromJson<SubscriptionValidationEventData>();
                                var responseData = new SubscriptionValidationResponse
                                { ValidationResponse = eventData.ValidationCode };
                                if (responseData.ValidationResponse != null)
                                { return Ok(responseData);}
                            }
                            break;
                        case "Microsoft.Communication.IncomingCall":

                  var _client = new CallAutomationClient("<ACS connection string>");
                            var eventData2 = eventGridEvent.Data.ToObjectFromJson<AcsIncomingCallEventData>();
                            string incomingCallContext = eventData2.IncomingCallContext;
                            string serverCallId = eventData2.ServerCallId;
                            var answerCallOptions = new AnswerCallOptions(incomingCallContext, new Uri("wss://... my url here}"));
                            answerCallOptions.OperationContext = "";
                            var call = await _client.AnswerCallAsync(answerCallOptions);

                            // How to get the stream from the call?

                           // then stream into Azure CLU
                           // some constants
                           var speechConfig = SpeechConfig.FromSubscription(speechKey, speechRegion);
                           speechConfig.SpeechRecognitionLanguage = "en-US";
                      speechConfig.SetProperty(PropertyId.Speech_SegmentationSilenceTimeoutMs, "2000");
using AudioConfig audioConfig = AudioConfig.FromStreamInput(audioStream);
                           using (var intentRecognizer = new IntentRecognizer(speechConfig, audioConfig))

我试过这个。但是当我得到一些文本时,我如何将这些文本流到CLU(IntentRecognizer)中?

var callAutomationClient = new CallAutomationClient("<ACS connection string>");

                            var answerCallOptions = new AnswerCallOptions("<Incoming call context once call is connected>", new Uri("<https://sample-callback-uri>"))
                            {
                                AzureCognitiveServicesEndpointUrl = new Uri("https://sample-cognitive-service-resource.cognitiveservices.azure.com/") // for Speech-To-Text (choices)
                            };
                            var answerCallResult = await callAutomationClient.AnswerCallAsync(answerCallOptions);
q7solyqu

q7solyqu1#

在代码中,您已经设置了EventGrid订阅并正在接收事件。若要处理传入呼叫事件,可以使用以下代码

switch (eventGridEvent.EventType)
{
    case SystemEventNames.EventGridSubscriptionValidation:
    {
        // Handle subscription validation if necessary
        var eventData = eventGridEvent.Data.ToObjectFromJson<SubscriptionValidationEventData>();
        var responseData = new SubscriptionValidationResponse
        {
            ValidationResponse = eventData.ValidationCode
        };
        if (responseData.ValidationResponse != null)
        {
            return Ok(responseData);
        }
    }
    break;
    case "Microsoft.Communication.IncomingCall":
    {
        var eventData2 = eventGridEvent.Data.ToObjectFromJson<AcsIncomingCallEventData>();
        string incomingCallContext = eventData2.IncomingCallContext;
        string serverCallId = eventData2.ServerCallId;

        // Answer the incoming call and get the call object
        var _client = new CallAutomationClient("<ACS connection string>");
        var answerCallOptions = new AnswerCallOptions(incomingCallContext, new Uri("wss://... my url here}"));
        answerCallOptions.OperationContext = "";
        var call = await _client.AnswerCallAsync(answerCallOptions);

        // Now, you need to capture the audio stream from the call and send it to Azure Cognitive Services for speech recognition.
        
        // Stream the audio to Azure Cognitive Services
        var speechConfig = SpeechConfig.FromSubscription(speechKey, speechRegion);
        speechConfig.SpeechRecognitionLanguage = "en-US";
        speechConfig.SetProperty(PropertyId.Speech_SegmentationSilenceTimeoutMs, "2000");

        // Create an AudioConfig from the call's audio stream
        using AudioConfig audioConfig = AudioConfig.FromStreamInput(call.AudioStream);

        // Create an IntentRecognizer to perform speech recognition
        using (var intentRecognizer = new IntentRecognizer(speechConfig, audioConfig))
        {
            // Start recognizing and processing the audio stream
            var result = await intentRecognizer.RecognizeOnceAsync();
            
            // You can now process the result or send it to Azure Cognitive Language Understanding (LUIS) for further intent recognition.
        }
    }
    break;
}
  • call.AudioStream是包含来自电话呼叫的音频的流。
  • 我已经有一个测试通过电子邮件检查如下:

  • 此外,您可以将识别的Intent发送到Azure CLU(LUIS)以进行进一步处理。

相关问题