你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

在调用启动时记录调用

呼叫录制通常通过调用应用程序的 UI 直接使用,用户在其中触发录制。 对于银行或医疗保健等行业的应用程序,需要从即用电话记录。 出于合规性目的,服务需要自动记录。 此示例演示如何在调用启动时记录调用。 它使用Azure 通信服务和Azure 事件网格在调用启动时触发 Azure 函数。 它会自动记录Azure 通信服务资源中的每个调用。

在本快速入门中,我们重点介绍如何使用事件网格触发器通过 Azure Functions 展示调用启动事件的处理。 我们使用用于Azure 通信服务的呼叫自动化 SDK 开始录制。

当调用开始采用以下方式设置格式时,调用开始事件:


[
  {
    "id": "a8bcd8a3-12d7-46ba-8cde-f6d0bda8feeb",
    "topic": "/subscriptions/{subscription-id}/resourcegroups/{group-name}/providers/microsoft.communication/communicationservices/{communication-services-resource-name}",
    "subject": "call/{serverCallId}/startedBy/8:acs:bc360ba8-d29b-4ef2-b698-769ebef85521_0000000c-1fb9-4878-07fd-0848220077e1",
    "data": {
      "startedBy": {
        "communicationIdentifier": {
          "rawId": "8:acs:bc360ba8-d29b-4ef2-b698-769ebef85521_0000000c-1fb9-4878-07fd-0848220077e1",
          "communicationUser": {
            "id": "8:acs:bc360ba8-d29b-4ef2-b698-769ebef85521_0000000c-1fb9-4878-07fd-0848220077e1"
          }
        },
        "role": "{role}"
      },
      "serverCallId": "{serverCallId}",
      "group": {
        "id": "00000000-0000-0000-0000-000000000000"
      },
      "room": {
        "id": "{roomId}"
      },
      "isTwoParty": false,
      "correlationId": "{correlationId}",
      "isRoomsCall": true
    },
    "eventType": "Microsoft.Communication.CallStarted",
    "dataVersion": "1.0",
    "metadataVersion": "1",
    "eventTime": "2021-09-22T17:02:38.6905856Z"
  }
]

注意

使用 Azure 事件网格会产生额外的费用。 有关详细信息,请参阅 Azure 事件网格定价

先决条件

设置本地环境

  1. 使用 Visual Studio Code 安装 Azure Functions 扩展

  2. 使用扩展,按照以下说明创建 Azure 函数。

    按照以下说明配置函数:

    • 语言:C#
    • 模板:Azure 事件网格触发器
    • 函数名称:用户定义

    创建后,会看到在目录中创建的函数,如下所示:

    
    using System;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.EventGrid;
    using Microsoft.Extensions.Logging;
    using Azure.Messaging.EventGrid;
    using System.Threading.Tasks;
    
    namespace Company.Function
    {
        public static class acs_recording_test
        {
            [FunctionName("acs_recording_test")]
            public static async Task RunAsync([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log)
            {
                log.LogInformation(eventGridEvent.EventType);
            }
    
        }
    }
    
    

配置 Azure Function 以接收 CallStarted 事件

  1. 将 Azure Function 配置为在事件触发时 CallStarted 执行操作。

    public static async Task RunAsync([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log)
    {
        if(eventGridEvent.EventType == "Microsoft.Communication.CallStarted")
        {
            log.LogInformation("Call started");
            var callEvent = eventGridEvent.Data.ToObjectFromJson<CallStartedEvent>();
            
            // CallStartedEvent class is defined in documentation, but the objects looks like this:
            // public class CallStartedEvent
            // {
            //     public StartedBy startedBy { get; set; }
            //     public string serverCallId { get; set; }
            //     public Group group { get; set; }
            //     public bool isTwoParty { get; set; }
            //     public string correlationId { get; set; }
            //     public bool isRoomsCall { get; set; }
            // }
            // public class Group
            // {
            //     public string id { get; set; }
            // }
            // public class StartedBy
            // {
            //     public CommunicationIdentifier communicationIdentifier { get; set; }
            //     public string role { get; set; }
            // }
            // public class CommunicationIdentifier
            // {
            //     public string rawId { get; set; }
            //     public CommunicationUser communicationUser { get; set; }
            // }
            // public class CommunicationUser
            // {
            //     public string id { get; set; }
            // }
        }
    }

开始录制

  1. 创建用于处理 CallStarted 事件的方法。 此方法触发录制以在调用启动时启动。

    public static async Task RunAsync([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log)
    {
        if(eventGridEvent.EventType == "Microsoft.Communication.CallStarted")
        {
            log.LogInformation("Call started");
            var callEvent = eventGridEvent.Data.ToObjectFromJson<CallStartedEvent>();
            await startRecordingAsync(callEvent.serverCallId);
        }
    }

    public static async Task startRecordingAsync (String serverCallId)
    {
        CallAutomationClient callAutomationClient = new CallAutomationClient(Environment.GetEnvironmentVariable("ACS_CONNECTION_STRING"));
        StartRecordingOptions recordingOptions = new StartRecordingOptions(new ServerCallLocator(serverCallId));
        recordingOptions.RecordingChannel = RecordingChannel.Mixed;
        recordingOptions.RecordingContent = RecordingContent.AudioVideo;
        recordingOptions.RecordingFormat = RecordingFormat.Mp4;        
        var startRecordingResponse = await callAutomationClient.GetCallRecording()
            .StartRecordingAsync(recordingOptions).ConfigureAwait(false);
    }

在本地运行

若要在本地运行函数,可以在 Visual Studio Code 中按。F5 我们使用 ngrok 将本地运行的 Azure 函数与 Azure 事件网格挂钩。

  1. 函数运行后,我们将配置 ngrok。 (需要下载适用于环境的 ngrok。)

     ngrok http 7071
    

    复制在函数运行的位置提供的 ngrok 链接。

  2. 通过Azure 通信服务资源中的事件网格配置 CallStarted 事件。 我们使用 Azure CLI 执行此操作。 你需要使用 Azure 门户中找到的 Azure 通信服务资源的资源 ID。 (资源 ID 如下所示: /subscriptions/<<AZURE SUBSCRIPTION ID>>/resourceGroups/<<RESOURCE GROUP NAME>>/providers/Microsoft.Communication/CommunicationServices/<<RESOURCE NAME>>

    
    az eventgrid event-subscription create --name "<<EVENT_SUBSCRIPTION_NAME>>" --endpoint-type webhook --endpoint "<<NGROK URL>>/runtime/webhooks/EventGrid?functionName=<<FUNCTION NAME>> " --source-resource-id "<<RESOURCE_ID>>"  --included-event-types Microsoft.Communication.CallStarted
    
    
  3. 现在,所有内容都已挂钩,通过对资源启动调用来测试流。 应在函数运行的终端上看到控制台日志。 你可以检查在通话 SDK 上使用通话录制功能开始录制,检查布尔值变为 TRUE。

部署到 Azure

若要将 Azure 函数部署到 Azure,需要按照以下说明进行操作。 部署后,我们将为 Azure 通信服务资源配置事件网格。 使用部署的 Azure 函数的 URL(在函数下的Azure 门户中找到的 URL),我们运行类似的命令:


az eventgrid event-subscription update --name "<<EVENT_SUBSCRIPTION_NAME>>" --endpoint-type azurefunction --endpoint "<<AZ FUNCTION URL>> " --source-resource-id "<<RESOURCE_ID>>"

由于我们正在更新我们创建的事件订阅,因此请确保使用在上一步中使用的相同事件订阅名称。

可以通过在资源中启动调用(类似于上一步)进行测试。