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

在呼叫之间传递上下文数据

调用自动化允许开发人员在路由调用时传递自定义上下文信息。 开发人员可以传递有关呼叫、调用方或任何其他与应用程序或业务逻辑相关的信息的元数据。 然后,企业可以跨网络管理和路由呼叫,而无需担心失去上下文。

支持通过指定自定义标头来传递上下文。 此可选的键/值对列表包含在AddParticipantTransfer操作的一部分中。 稍后将检索上下文作为 IncomingCall 事件有效负载的一部分。

自定义调用上下文也会转发到会话初始协议(SIP),其中包括任意格式的自定义标头和标准用户到用户信息(UUI)SIP 标头。 路由来自电话网络的入站呼叫时,自定义标头中的会话边界控制器 (SBC) 中的数据集也同样包含在 IncomingCall 事件有效负载中。

所有自定义上下文数据都与调用自动化或 SIP 协议不透明,其内容与任何基本函数无关。

以下示例演示如何在调用自动化中使用自定义上下文标头入门。

Prerequisites

  • 介绍操作事件编程模型和事件回调的通话自动化概念指南
  • 了解本文中使用的 用户标识符,如 CommunicationUserIdentifier 和 PhoneNumberIdentifier。

对于所有代码示例,client是可以创建的CallAutomationClient对象,而callConnection是从AnswerCreateCall响应获取的CallConnection对象。 还可以从应用程序收到的回调事件中获取它。

Technical parameters

呼叫自动化最多支持五个自定义 SIP 标头和 1,000 个自定义 IP 语音(VoIP)标头。 开发人员可以将专用用户到用户标头包含在 SIP 标头列表的一部分。

自定义 SIP 标头密钥必须以必需 X-MS-Custom- 前缀开头。 SIP 标头密钥的最大长度为 64 个字符,包括 X-MS-Custom 前缀。 SIP 标头键由字母数字字符和几个选定的符号组成,其中包括.!、、、%*_+~-。 SIP 标头值的最大长度为 256 个字符。 在 SBC 上配置 SIP 标头时,同样的限制适用。 SIP 标头值由字母数字字符和几个选定的符号组成,其中包括、、、 =;.!%*_和。+~-

VoIP 标头键的最大长度为 64 个字符。 可以在不添加x-MS-Custom前缀的情况下发送这些标头。 VoIP 标头值的最大长度为 1,024 个字符。

邀请参与者时添加自定义上下文

// Invite an Azure Communication Services user and include one VOIP header
var addThisPerson = new CallInvite(new CommunicationUserIdentifier("<user_id>"));
addThisPerson.CustomCallingContext.AddVoip("myHeader", "myValue");
AddParticipantsResult result = await callConnection.AddParticipantAsync(addThisPerson);
// Invite a PSTN user and set UUI and custom SIP headers
var callerIdNumber = new PhoneNumberIdentifier("+16044561234"); 
var addThisPerson = new CallInvite(new PhoneNumberIdentifier("+16041234567"), callerIdNumber);

// Set custom UUI header. This key is sent on SIP protocol as User-to-User
addThisPerson.CustomCallingContext.AddSipUui("value");

// This provided key will be automatically prefixed with X-MS-Custom on SIP protocol, such as 'X-MS-Custom-{key}'
addThisPerson.CustomCallingContext.AddSipX("header1", "customSipHeaderValue1");
AddParticipantsResult result = await callConnection.AddParticipantAsync(addThisPerson);

在呼叫转移期间添加自定义上下文

//Transfer to an Azure Communication Services user and include one VOIP header
var transferDestination = new CommunicationUserIdentifier("<user_id>"); 
var transferOption = new TransferToParticipantOptions(transferDestination);   
var transferOption = new TransferToParticipantOptions(transferDestination) {
    OperationContext = "<Your_context>",
    OperationCallbackUri = new Uri("<uri_endpoint>") // Sending event to a non-default endpoint.
};
transferOption.CustomCallingContext.AddVoip("customVoipHeader1", "customVoipHeaderValue1");
TransferCallToParticipantResult result = await callConnection.TransferCallToParticipantAsync(transferOption);

//Transfer a PSTN call to phone number and set UUI and custom SIP headers
var transferDestination = new PhoneNumberIdentifier("<target_phoneNumber>");
var transferOption = new TransferToParticipantOptions(transferDestination);
transferOption.CustomCallingContext.AddSipUui("uuivalue");
transferOption.CustomCallingContext.AddSipX("header1", "headerValue");
TransferCallToParticipantResult result = await callConnection.TransferCallToParticipantAsync(transferOption)

从传入调用事件读取自定义上下文

AcsIncomingCallEventData incomingEvent = <incoming call event from Event Grid>;
// Retrieve incoming call custom context
AcsIncomingCallCustomContext callCustomContext = incomingEvent.CustomContext;

// Inspect dictionary with key/value pairs
var voipHeaders = callCustomContext.VoipHeaders;
var sipHeaders = callCustomContext.SipHeaders;

// Get SIP UUI header value
var userToUser = sipHeaders["user-To-User"]

// Proceed to answer or reject call as usual