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

了解标识符类型

通信服务 SDK 和 REST API 使用标识符类型来识别谁正在与谁通信。 例如,标识符指定被呼叫方,或聊天消息的发送方。

根据上下文使用额外的属性包装标识符,例如在聊天 SDK 中的 ChatParticipant 内或在呼叫 SDK 中的 RemoteParticipant 内进行包装。

本文介绍不同类型的标识符,以及它们在不同编程语言中的外观。 此外提供有关如何使用标识符的提示。

CommunicationIdentifier 类型

有些用户标识是你自己创建的,此外还存在一些外部标识。 Microsoft Teams 用户和电话号码是在互操作方案中发挥作用的外部标识。 在这些不同的标识类型中,每种类型都有一个对应的标识符表示该类型。 标识符是一种结构化类型,它提供类型安全性,能够很好地处理编辑器的代码完成。

通信用户

CommunicationUserIdentifier 接口表示使用标识 SDK 或 REST API 创建的用户标识。 如果应用程序不使用 Microsoft Teams 互操作性或电话功能,则它是唯一使用的标识符。

基本用法

// at some point you will have created a new user identity in your trusted service
const newUser = await identityClient.createUser();

// and then send newUser.communicationUserId down to your client application
// where you can again create an identifier for the user
const sameUser = { communicationUserId: newUserId };

API 参考

CommunicationUserIdentifier

Microsoft Teams 用户

MicrosoftTeamsUserIdentifier 接口代表 Teams 用户及其 Microsoft Entra 用户对象 ID。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 用户对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK。 或者,可以在用户登录并获取令牌后,在 Microsoft Entra 令牌Microsoft Entra 访问令牌中找到作为 oid 声明提供的 ID。

基本用法

// get the Teams user's ID from Graph APIs if only the email is known
const user = await graphClient.api("/users/bob@contoso.com").get();

// create an identifier
const teamsUser = { microsoftTeamsUserId: user.id };

// if you're not operating in the public cloud, you must also pass the right Cloud type.
const gcchTeamsUser = { microsoftTeamsUserId: userId, cloud: "gcch" };

API 参考

MicrosoftTeamsUserIdentifier

电话号码

PhoneNumberIdentifier 接口表示电话号码。 服务假设电话号码采用 E.164 格式。

基本用法

// create an identifier
const phoneNumber = { phoneNumber: "+112345556789" };

API 参考

PhoneNumberIdentifier

Microsoft Teams 应用程序

MicrosoftTeamsAppIdentifier 接口代表 Teams 语音应用程序的机器人,例如呼叫队列和自动助理及其 Microsoft Entra 机器人对象 ID。 应使用资源帐户配置 Teams 应用程序。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 机器人对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK

基本用法

// Get the Microsoft Teams App's ID from Graph APIs
const users = await graphClient.api("/users")
                    .filter(filterConditions)
                    .select('displayName,id')
                    .get();
//Here we assume that you have a function getBotFromUsers that gets the bot from the returned response
const bot = getBotFromUsers(users);
// Create an identifier
const teamsAppIdentifier = { teamsAppId: bot.id };

// If you're not operating in the public cloud, you must also pass the right Cloud type.
const gcchTeamsAppIdentifier = { teamsAppId: id, cloud: "gcch" };

API 参考

MicrosoftTeamsAppIdentifier

未知

UnknownIdentifier 接口面向未来,如果你使用的是旧版 SDK,而最近引入了新的标识符类型,则你可能会看到此接口。 服务中的任何未知标识符都将在 SDK 中反序列化为 UnknownIdentifier

基本用法

// create an identifier
const unknownId = { id: "a raw id that originated in the service" };

API 参考

UnknownIdentifier

如何处理 CommunicationIdentifier 基接口

为传入 SDK 的具体类型构造标识符时,SDK 会返回一个 CommunicationIdentifierKind,即一个可区分联合。 可以轻松缩小为具体类型,我们建议在模式匹配中使用 switch-case 语句:

switch (communicationIdentifier.kind)
{
    case "communicationUser":
        // TypeScript has narrowed communicationIdentifier to be a CommunicationUserKind
        console.log(`Communication user: ${communicationIdentifier.communicationUserId}`);
        break;
    case "microsoftTeamsUser":
        // narrowed to MicrosoftTeamsUserKind
        console.log(`Teams user: ${communicationIdentifier.microsoftTeamsUserId}`);
        break;
    case "phoneNumber":
         // narrowed to PhoneNumberKind
        console.log(`Phone number: ${communicationIdentifier.phoneNumber}`);
        break;
    case "unknown":
         // narrowed to UnknownIdentifierKind
        console.log(`Unknown: ${communicationIdentifier.id}`);
        break;
    case "microsoftBot":
        // narrowed to MicrosoftBotIdentifier
        console.log(`Microsoft bot: ${communicationIdentifier.botId}`);
        break;
    default:
        // be careful here whether you want to throw because a new SDK version
        // can introduce new identifier types
        break;
}

标识符接口的设计使你不必指定 kind 来降低详细程度,并且仅在从 SDK 返回时才使用具有 kind 属性的区分联合。 但是,如果你需要将标识符转换为其对应的区分联合类型,可以使用以下帮助器:

const identifierKind = getIdentifierKind(identifier); // now you can switch-case on the kind

原始 ID 表示形式

有时需要将标识符序列化为平面字符串。 例如,如果你要将标识符存储在数据库表中,或者要将其用作 URL 参数。

为此,标识符可以采用名为 RawId 的另一种表示形式。 标识符始终可以转换为其对应的原始 ID,而有效的原始 ID 始终可以转换为标识符。

azure-communication-common@2.1.0 开始,SDK 可以帮助完成这种转换:

// get an identifier's raw Id
const rawId = getIdentifierRawId(communicationIdentifier);

// create an identifier from a given raw Id
const identifier = createIdentifierFromRawId(rawId);

无效的原始 ID 只会在 SDK 中转换为 UnknownIdentifier,任何验证只会在服务端发生。

通信用户

CommunicationUserIdentifier 表示使用标识 SDK 或 REST API 创建的用户标识。 如果应用程序不使用 Microsoft Teams 互操作性或电话功能,则它是唯一使用的标识符。

基本用法

// at some point you will have created a new user identity in your trusted service
CommunicationUserIdentifier newUser = await identityClient.CreateUser();

// and then send newUser.Id down to your client application
// where you can again create an identifier for the user
var sameUser = new CommunicationUserIdentifier(newUserId);

API 参考

CommunicationUserIdentifier

Microsoft Teams 用户

MicrosoftTeamsUserIdentifier 代表 Teams 用户及其 Microsoft Entra 用户对象 ID。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 用户对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK。 或者,可以在用户登录并获取令牌后,在 Microsoft Entra 令牌Microsoft Entra 访问令牌中找到作为 oid 声明提供的 ID。

基本用法

// get the Teams user's ID from Graph APIs if only the email is known
var user = await graphClient.Users["bob@contoso.com"]
    .Request()
    .GetAsync();

// create an identifier
var teamsUser = new MicrosoftTeamsUserIdentifier(user.Id);

// if you're not operating in the public cloud, you must also pass the right Cloud type.
var gcchTeamsUser = new MicrosoftTeamsUserIdentifier(userId: userId, cloud: CommunicationCloudEnvironment.Gcch);

API 参考

MicrosoftTeamsUserIdentifier

电话号码

PhoneNumberIdentifier 表示电话号码。 服务假设电话号码采用 E.164 格式。

基本用法

// create an identifier
var phoneNumber = new PhoneNumberIdentifier("+112345556789");

API 参考

PhoneNumberIdentifier

Microsoft Teams 应用程序

MicrosoftTeamsAppIdentifier 接口代表 Teams 语音应用程序的机器人,例如呼叫队列和自动助理及其 Microsoft Entra 机器人对象 ID。 应使用资源帐户配置 Teams 应用程序。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 机器人对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK

基本用法

// Get the Microsoft Teams App's ID from Graph APIs
var users = await graphClient.Users.GetAsync((requestConfiguration) =>
{
	requestConfiguration.QueryParameters.Select = new string []{ "displayName","id" };
	requestConfiguration.QueryParameters.Filter = filterConditions;
});

// Here we assume that you have a function GetBotFromUsers that gets the bot from the returned response
var bot = GetBotFromUsers(users);

// Create an identifier
var teamsAppIdentifier = new MicrosoftTeamsAppIdentifier(bot.Id);

// If you're not operating in the public cloud, you must also pass the right Cloud type.
var gcchTeamsAppIdentifier = new MicrosoftTeamsAppIdentifier(bot.Id, CommunicationCloudEnvironment.Gcch);

API 参考

MicrosoftTeamsAppIdentifier

未知

UnknownIdentifier 面向未来,如果你使用的是旧版 SDK,而最近引入了新的标识符类型,则你可能会看到此接口。 服务中的任何未知标识符都将在 SDK 中反序列化为 UnknownIdentifier

基本用法

// create an identifier
var unknown = new UnknownIdentifier("a raw id that originated in the service");

API 参考

UnknownIdentifier

如何处理 CommunicationIdentifier 基类

为传入 SDK 的具体类型构造标识符时,SDK 会返回 CommunicationIdentifier 协议。 可以轻松地向下强制转换为具体类型,我们建议在模式匹配中使用 switch-case 语句:

switch (communicationIdentifier)
{
    case CommunicationUserIdentifier communicationUser:
        Console.WriteLine($"Communication user: {communicationUser.Id}");
        break;
    case MicrosoftTeamsUserIdentifier teamsUser:
        Console.WriteLine($"Teams user: {teamsUser.UserId}");
        break;
    case PhoneNumberIdentifier phoneNumber:
        Console.WriteLine($"Phone number: {phoneNumber.PhoneNumber}");
        break;
    case MicrosoftBotIdentifier bot:
        Console.WriteLine($"Microsoft bot: {bot.BotId}");
        break;
    case UnknownIdentifier unknown:
        Console.WriteLine($"Unknown: {unknown.Id}");
        break;
    default:
        // be careful here whether you want to throw because a new SDK version
        // can introduce new identifier types
        break;
}

原始 ID 表示形式

有时需要将标识符序列化为平面字符串。 例如,如果你要将标识符存储在数据库表中,或者要将其用作 URL 参数。

为此,标识符可以采用名为 RawId 的另一种表示形式。 标识符始终可以转换为其对应的原始 ID,而有效的原始 ID 始终可以转换为标识符。

Azure.Communication.Common 1.2.0 开始,SDK 可以帮助完成这种转换:

// get an identifier's raw Id
string rawId = communicationIdentifier.RawId;

// create an identifier from a given raw Id
CommunicationIdentifier identifier = CommunicationIdentifier.FromRawId(rawId);

无效的原始 ID 只会在 SDK 中转换为 UnknownIdentifier,任何验证只会在服务端发生。

通信用户

CommunicationUserIdentifier 表示使用标识 SDK 或 REST API 创建的用户标识。 如果应用程序不使用 Microsoft Teams 互操作性或电话功能,则它是唯一使用的标识符。

基本用法

# at some point you will have created a new user identity in your trusted service
new_user = identity_client.create_user()

# and then send new_user.properties['id'] down to your client application
# where you can again create an identifier for the user
same_user = CommunicationUserIdentifier(new_user_id)

API 参考

CommunicationUserIdentifier

Microsoft Teams 用户

MicrosoftTeamsUserIdentifier 代表 Teams 用户及其 Microsoft Entra 用户对象 ID。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 用户对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK。 或者,可以在用户登录并获取令牌后,在 Microsoft Entra 令牌Microsoft Entra 访问令牌中找到作为 oid 声明提供的 ID。

基本用法

# get the Teams user's ID from Graph APIs if only the email is known
user_id = graph_client.get("/users/bob@contoso.com").json().get("id");

# create an identifier
teams_user = MicrosoftTeamsUserIdentifier(user_id)

# if you're not operating in the public cloud, you must also pass the right Cloud type.
gcch_teams_user = MicrosoftTeamsUserIdentifier(user_id, cloud=CommunicationCloudEnvironment.GCCH)

API 参考

MicrosoftTeamsUserIdentifier

电话号码

PhoneNumberIdentifier 表示电话号码。 服务假设电话号码采用 E.164 格式。

基本用法

# create an identifier
phone_number = PhoneNumberIdentifier("+112345556789")

API 参考

PhoneNumberIdentifier

Microsoft Teams 应用程序

MicrosoftTeamsAppIdentifier 接口代表 Teams 语音应用程序的机器人,例如呼叫队列和自动助理及其 Microsoft Entra 机器人对象 ID。 应使用资源帐户配置 Teams 应用程序。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 机器人对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK

基本用法

# Get the Microsoft Teams App's ID from Graph APIs
users = graph_client.get("/users").json()

# Here we assume that you have a function get_bot_from_users that gets the bot from the returned response
bot = get_bot_from_users(users);

# Create an identifier
teams_app_identifier = MicrosoftTeamsAppIdentifier(app_id=bot.get("id"))

# If you're not operating in the public cloud, you must also pass the right Cloud type.
gcch_teams_app_identifier = MicrosoftTeamsAppIdentifier(
            app_id=bot.get("id"),
            cloud=CommunicationCloudEnvironment.GCCH
        )

API 参考

MicrosoftTeamsAppIdentifier

未知

UnknownIdentifier 面向未来,如果你使用的是旧版 SDK,而最近引入了新的标识符类型,则你可能会看到此接口。 服务中的任何未知标识符都将在 SDK 中反序列化为 UnknownIdentifier

基本用法

# create an identifier
unknown = UnknownIdentifier("a raw id that originated in the service")

API 参考

UnknownIdentifier

如何处理 CommunicationIdentifier 基类

为传入 SDK 的具体类型构造标识符时,SDK 会返回 CommunicationIdentifier 协议。 具体标识符类只是 CommunicationIdentifier 协议和一个名为 properties 的类型化字典。 因此,可以在协议的 kind 实例变量上使用模式匹配来转换为具体类型:

match communication_identifier.kind:
    case CommunicationIdentifierKind.COMMUNICATION_USER:
        print(f"Communication user: {communication_identifier.properties['id']}")
    case CommunicationIdentifierKind.MICROSOFT_TEAMS_USER:
        print(f"Teams user: {communication_identifier.properties['user_id']}")
    case CommunicationIdentifierKind.PHONE_NUMBER:
        print(f"Phone number: {communication_identifier.properties['value']}")
    case CommunicationIdentifierKind.MICROSOFT_BOT:
        print(f"Microsoft bot: {communication_identifier.properties['bot_id']}")
    case CommunicationIdentifierKind.UNKNOWN:
        print(f"Unknown: {communication_identifier.raw_id}")
    case _:
        # be careful here whether you want to throw because a new SDK version
        # can introduce new identifier types

原始 ID 表示形式

有时需要将标识符序列化为平面字符串。 例如,如果你要将标识符存储在数据库表中,或者要将其用作 URL 参数。

为此,标识符可以采用名为 RawId 的另一种表示形式。 标识符始终可以转换为其对应的原始 ID,而有效的原始 ID 始终可以转换为标识符。

SDK 可以帮助完成这种转换:

# get an identifier's raw Id
raw_id = communication_identifier.raw_id

# create an identifier from a given raw Id
identifier = identifier_from_raw_id(raw_id)

无效的原始 ID 只会在 SDK 中转换为 UnknownIdentifier,任何验证只会在服务端发生。

通信用户

CommunicationUserIdentifier 表示使用标识 SDK 或 REST API 创建的用户标识。 如果应用程序不使用 Microsoft Teams 互操作性或电话功能,则它是唯一使用的标识符。

基本用法

// at some point you will have created a new user identity in your trusted service
CommunicationUserIdentifier newUser = identityClient.CreateUser();

// and then send newUser.getId() down to your client application
// where you can again create an identifier for the user
var sameUser = new CommunicationUserIdentifier(newUserId);

API 参考

CommunicationUserIdentifier

Microsoft Teams 用户

MicrosoftTeamsUserIdentifier 代表 Teams 用户及其 Microsoft Entra 用户对象 ID。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 用户对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK。 或者,可以在用户登录并获取令牌后,在 Microsoft Entra 令牌Microsoft Entra 访问令牌中找到作为 oid 声明提供的 ID。

基本用法

// get the Teams user's ID from Graph APIs if only the email is known
var user = graphClient.users("bob@contoso.com")
    .buildRequest()
    .get();

// create an identifier
var teamsUser = new MicrosoftTeamsUserIdentifier(user.id);

// if you're not operating in the public cloud, you must also set the right Cloud type.
var gcchTeamsUser = new MicrosoftTeamsUserIdentifier(userId).setCloudEnvironment(CommunicationCloudEnvironment.GCCH);

API 参考

MicrosoftTeamsUserIdentifier

电话号码

PhoneNumberIdentifier 表示电话号码。 服务假设电话号码采用 E.164 格式。

基本用法

// create an identifier
var phoneNumber = new PhoneNumberIdentifier("+112345556789");

API 参考

PhoneNumberIdentifier

Microsoft Teams 应用程序

MicrosoftTeamsAppIdentifier 接口代表 Teams 语音应用程序的机器人,例如呼叫队列和自动助理及其 Microsoft Entra 机器人对象 ID。 应使用资源帐户配置 Teams 应用程序。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 机器人对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK

基本用法

// Get the Microsoft Teams App's ID from Graph APIs
var user = graphClient.users()
	.buildRequest()
	.filter(filterConditions)
	.select("displayName,id")
	.get();

//Here we assume that you have a function getBotFromUsers that gets the bot from the returned response
var bot = getBotFromUsers(users);

// Create an identifier
var teamsAppIdentifier = new MicrosoftTeamsAppIdentifier(bot.id);

// If you're not operating in the public cloud, you must also pass the right Cloud type.
var gcchTeamsAppIdentifier = new MicrosoftTeamsAppIdentifier(bot.id, CommunicationCloudEnvironment.GCCH);

API 参考

MicrosoftTeamsAppIdentifier

未知

UnknownIdentifier 面向未来,如果你使用的是旧版 SDK,而最近引入了新的标识符类型,则你可能会看到此接口。 服务中的任何未知标识符都将在 SDK 中反序列化为 UnknownIdentifier

基本用法

// create an identifier
var unknown = new UnknownIdentifier("a raw id that originated in the service");

API 参考

UnknownIdentifier

如何处理 CommunicationIdentifier 基类

为传入 SDK 的具体类型构造标识符时,SDK 会返回抽象 CommunicationIdentifier。 可以向下强制转换回具体类型:

if (communicationIdentifier instanceof CommunicationUserIdentifier) {
    System.out.println("Communication user: " + ((CommunicationUserIdentifier)communicationIdentifier).getId());
}
else if (communicationIdentifier instanceof MicrosoftTeamsUserIdentifier) {
    System.out.println("Teams user: " + ((MicrosoftTeamsUserIdentifier)communicationIdentifier).getUserId());
}
else if (communicationIdentifier instanceof PhoneNumberIdentifier) {
    System.out.println("Phone number: " + ((PhoneNumberIdentifier)communicationIdentifier).getPhoneNumber());
}
else if (communicationIdentifier instanceof MicrosoftBotIdentifier) {
    Log.i(tag, "Microsoft bot: " + ((MicrosoftBotIdentifier)communicationIdentifier).getBotId());
}
else if (communicationIdentifier instanceof UnknownIdentifier) {
    System.out.println("Unkown user: " + ((UnknownIdentifier)communicationIdentifier).getId());
}
else {
    // be careful here whether you want to throw because a new SDK version
        // can introduce new identifier types
}

原始 ID 表示形式

有时需要将标识符序列化为平面字符串。 例如,如果你要将标识符存储在数据库表中,或者要将其用作 URL 参数。

为此,标识符可以采用名为 RawId 的另一种表示形式。 标识符始终可以转换为其对应的原始 ID,而有效的原始 ID 始终可以转换为标识符。

azure-communication-common 1.2.0 开始,SDK 可以帮助完成这种转换:

// get an identifier's raw Id
String rawId = communicationIdentifier.getRawId();

// create an identifier from a given raw Id
CommunicationIdentifier identifier = CommunicationIdentifier.fromRawId(rawId);

无效的原始 ID 只会在 SDK 中转换为 UnknownIdentifier,任何验证只会在服务端发生。

通信用户

CommunicationUserIdentifier 表示使用标识 SDK 或 REST API 创建的用户标识。 如果应用程序不使用 Microsoft Teams 互操作性或电话功能,则它是唯一使用的标识符。

基本用法

// at some point you will have created a new user identity in your trusted service
// and send the new user id down to your client application
// where you can create an identifier for the user
let user = CommunicationUserIdentifier(newUserId)

API 参考

CommunicationUserIdentifier

Microsoft Teams 用户

MicrosoftTeamsUserIdentifier 代表 Teams 用户及其 Microsoft Entra 用户对象 ID。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 用户对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK。 或者,可以在用户登录并获取令牌后,在 ID 令牌Microsoft Entra 访问令牌中找到作为 oid 声明提供的 ID。

基本用法

// get the Teams user's ID if only the email is known, assuming a helper method for the Graph API
let userId = await getUserIdFromGraph("bob@contoso.com")

// create an identifier
let teamsUser = MicrosoftTeamsUserIdentifier(userId: userId)

// if you're not operating in the public cloud, you must also pass the right Cloud type.
let gcchTeamsUser = MicrosoftTeamsUserIdentifier(userId: userId, cloud: CommunicationCloudEnvironment.Gcch)

API 参考

MicrosoftTeamsUserIdentifier

电话号码

PhoneNumberIdentifier 表示电话号码。 服务假设电话号码采用 E.164 格式。

基本用法

// create an identifier
let phoneNumber = PhoneNumberIdentifier(phoneNumber: "+112345556789")

API 参考

PhoneNumberIdentifier

Microsoft Teams 应用程序

MicrosoftTeamsAppIdentifier 接口代表 Teams 语音应用程序的机器人,例如呼叫队列和自动助理及其 Microsoft Entra 机器人对象 ID。 应使用资源帐户配置 Teams 应用程序。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 机器人对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK

基本用法

// Get the Microsoft Teams App's ID from Graph APIs, assuming a helper method for the Graph API
let botId = await getBotIdFromGraph()

// Create an identifier
let teamsAppIdentifier = MicrosoftTeamsAppIdentifier(appId: botId)

// If you're not operating in the public cloud, you must also pass the right Cloud type.
let gcchTeamsAppIdentifier = MicrosoftTeamsAppIdentifier(appId: botId, cloudEnvironment: CommunicationCloudEnvironment.Gcch)

API 参考

MicrosoftTeamsAppIdentifier

未知

UnknownIdentifier 面向未来,如果你使用的是旧版 SDK,而最近引入了新的标识符类型,则你可能会看到此接口。 服务中的任何未知标识符都将在 SDK 中反序列化为 UnknownIdentifier

基本用法

// create an identifier
let unknown = UnknownIdentifier("a raw id that originated in the service")

API 参考

UnknownIdentifier

如何处理 CommunicationIdentifier 基协议

为传入 SDK 的具体类型构造标识符时,SDK 会返回 CommunicationIdentifier 协议。 可以轻松地向下强制转换回具体类型,我们建议在模式匹配中使用 switch-case 语句:

switch (communicationIdentifier)
{
    case let communicationUser as CommunicationUserIdentifier:
        print(#"Communication user: \(communicationUser.id)"#)
    case let teamsUser as MicrosoftTeamsUserIdentifier:
        print(#"Teams user: \(teamsUser.UserId)"#)
    case let phoneNumber as PhoneNumberIdentifier:
        print(#"Phone number: \(phoneNumber.PhoneNumber)"#)
    case let bot as MicrosoftBotIdentifier:
        print(#"Microsoft bot: \(bot.botId)"#)
    case let unknown as UnknownIdentifier:
        print(#"Unknown: \(unknown.Id)"#)
    @unknown default:
        // be careful here whether you want to throw because a new SDK version
        // can introduce new identifier types
        break;
}

原始 ID 表示形式

有时需要将标识符序列化为平面字符串。 例如,如果你要将标识符存储在数据库表中,或者要将其用作 URL 参数。

为此,标识符可以采用名为 RawId 的另一种表示形式。 标识符始终可以转换为其对应的原始 ID,而有效的原始 ID 始终可以转换为标识符。

Azure.Communication.Common 1.1.0 开始,SDK 可以帮助完成这种转换:

Swift

// get an identifier's raw Id
let rawId = communicationIdentifier.rawId;

// create an identifier from a given raw Id
let identifier = createCommunicationIdentifier(fromRawId: rawId);

无效的原始 ID 只会在 SDK 中转换为 UnknownIdentifier,任何验证只会在服务端发生。

通信用户

CommunicationUserIdentifier 表示使用标识 SDK 或 REST API 创建的用户标识。 如果应用程序不使用 Microsoft Teams 互操作性或电话功能,则它是唯一使用的标识符。

基本用法

// at some point you will have created a new user identity in your trusted service
CommunicationUserIdentifier newUser = identityClient.CreateUser();

// and then send newUser.getId() down to your client application
// where you can again create an identifier for the user
CommunicationUserIdentifier sameUser = new CommunicationUserIdentifier(newUserId);

API 参考

CommunicationUserIdentifier

Microsoft Teams 用户

MicrosoftTeamsUserIdentifier 代表 Teams 用户及其 Microsoft Entra 用户对象 ID。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 用户对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK。 或者,可以在用户登录并获取令牌后,在 ID 令牌Microsoft Entra 访问令牌中找到作为 oid 声明提供的 ID。

基本用法

// get the Teams user's ID from Graph APIs if only the email is known
User user = graphClient.users("bob@contoso.com")
    .buildRequest()
    .get();

// create an identifier
MicrosoftTeamsUserIdentifier teamsUser = new MicrosoftTeamsUserIdentifier(user.id);

// if you're not operating in the public cloud, you must also set the right Cloud type.
MicrosoftTeamsUserIdentifier gcchTeamsUser = new MicrosoftTeamsUserIdentifier(userId).setCloudEnvironment(CommunicationCloudEnvironment.GCCH);

API 参考

MicrosoftTeamsUserIdentifier

电话号码

PhoneNumberIdentifier 表示电话号码。 服务假设电话号码采用 E.164 格式。

基本用法

// create an identifier
PhoneNumberIdentifier phoneNumber = new PhoneNumberIdentifier("+112345556789");

API 参考

PhoneNumberIdentifier

Microsoft Teams 应用程序

MicrosoftTeamsAppIdentifier 接口代表 Teams 语音应用程序的机器人,例如呼叫队列和自动助理及其 Microsoft Entra 机器人对象 ID。 应使用资源帐户配置 Teams 应用程序。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 机器人对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK

基本用法

// Get the Microsoft Teams App's ID from Graph APIs
UserCollectionPage users = graphClient.users()
	.buildRequest()
	.filter(filterConditions)
	.select("displayName,id")
	.get();

//Here we assume that you have a function getBotFromUsers that gets the bot from the returned response
User bot = getBotFromUsers(users);

// Create an identifier
MicrosoftTeamsAppIdentifier teamsAppIdentifier = new MicrosoftTeamsAppIdentifier(bot.id);

// If you're not operating in the public cloud, you must also pass the right Cloud type.
MicrosoftTeamsAppIdentifier gcchTeamsAppIdentifier = new MicrosoftTeamsAppIdentifier(bot.id, CommunicationCloudEnvironment.GCCH);

API 参考

MicrosoftTeamsAppIdentifier

未知

UnknownIdentifier 面向未来,如果你使用的是旧版 SDK,而最近引入了新的标识符类型,则你可能会看到此接口。 服务中的任何未知标识符都将在 SDK 中反序列化为 UnknownIdentifier

基本用法

// create an identifier
UnknownIdentifier unknown = new UnknownIdentifier("a raw id that originated in the service");

API 参考

UnknownIdentifier

如何处理 CommunicationIdentifier 基类

为传入 SDK 的具体类型构造标识符时,SDK 会返回抽象 CommunicationIdentifier。 可以向下强制转换回具体类型:

if (communicationIdentifier instanceof CommunicationUserIdentifier) {
    Log.i(tag, "Communication user: " + ((CommunicationUserIdentifier)communicationIdentifier).getId());
}
else if (communicationIdentifier instanceof MicrosoftTeamsUserIdentifier) {
    Log.i(tag, "Teams user: " + ((MicrosoftTeamsUserIdentifier)communicationIdentifier).getUserId());
}
else if (communicationIdentifier instanceof PhoneNumberIdentifier) {
    Log.i(tag, "Phone number: " + ((PhoneNumberIdentifier)communicationIdentifier).getPhoneNumber());
}
else if (communicationIdentifier instanceof MicrosoftBotIdentifier) {
    Log.i(tag, "Microsoft bot: " + ((MicrosoftBotIdentifier)communicationIdentifier).getBotId());
}
else if (communicationIdentifier instanceof UnknownIdentifier) {
    Log.i(tag, "Unkown user: " + ((UnknownIdentifier)communicationIdentifier).getId());
}
else {
    // be careful here whether you want to throw because a new SDK version
    // can introduce new identifier types
}

原始 ID 表示形式

有时需要将标识符序列化为平面字符串。 例如,如果你要将标识符存储在数据库表中,或者要将其用作 URL 参数。

为此,标识符可以采用名为 RawId 的另一种表示形式。 标识符始终可以转换为其对应的原始 ID,而有效的原始 ID 始终可以转换为标识符。

azure-communication-common 1.1.0 开始,SDK 可以帮助完成这种转换:

// get an identifier's raw Id
String rawId = communicationIdentifier.getRawId();

// create an identifier from a given raw Id
CommunicationIdentifier identifier = CommunicationIdentifier.fromRawId(rawId);

无效的原始 ID 只会在 SDK 中转换为 UnknownIdentifier,任何验证只会在服务端发生。

在 REST API 中,标识符是一种多态类型:可以构造一个 JSON 对象和一个映射到具体标识符子类型的属性。 出于方便性和后向兼容,kindrawId 属性在请求中是可选的,但会在服务响应中填充。

通信用户

CommunicationUserIdentifierModel 表示使用标识 SDK 或 REST API 创建的用户标识。 如果应用程序不使用 Microsoft Teams 互操作性或电话功能,则它是唯一使用的标识符。

基本用法


// at some point you will have created a new user identity in your trusted service
// you can specify an identifier with the id of the new user in a request
{
    "communicationUser": {
        "id": "8:acs:8540c0de-899f-5cce-acb5-3ec493af3800_c94ff260-162d-46d6-94fd-e79f4d213715"
    }
}

// the corresponding serialization in a response
{
    "kind": "communicationUser",
    "rawId": "8:acs:8540c0de-899f-5cce-acb5-3ec493af3800_c94ff260-162d-46d6-94fd-e79f4d213715",
    "communicationUser": {
        "id": "8:acs:8540c0de-899f-5cce-acb5-3ec493af3800_c94ff260-162d-46d6-94fd-e79f4d213715"
    }
}

可以在用于添加参与者的聊天 REST API 中找到包含标识符的请求示例,以及在获取聊天消息下找到包含标识符的响应示例。

API 参考

CommunicationUserIdentifierModel

Microsoft Teams 用户

MicrosoftTeamsUserIdentifierModel 代表 Teams 用户及其 Microsoft Entra 用户对象 ID。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 用户对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK。 或者,可以在用户登录并获取令牌后,在 Microsoft Entra 令牌Microsoft Entra 访问令牌中找到作为 oid 声明提供的 ID。

基本用法

// request
{
    "microsoftTeamsUser": {
        "userId": "daba101a-91c5-44c9-bb9b-e2a9a790571a"
    }
}

// response
{
    "kind": "microsoftTeamsUser",
    "rawId": "8:orgid:daba101a-91c5-44c9-bb9b-e2a9a790571a",
    "microsoftTeamsUser": {
        "userId": "daba101a-91c5-44c9-bb9b-e2a9a790571a"
    }
}


// if you're not operating in the public cloud, you must also pass the right Cloud type in a request
{
    "microsoftTeamsUser": {
        "userId": "daba101a-91c5-44c9-bb9b-e2a9a790571a",
        "cloud": "gcch"
    }
}

// response
{
    "kind": "microsoftTeamsUser",
    "rawId": "8:gcch:daba101a-91c5-44c9-bb9b-e2a9a790571a",
    "microsoftTeamsUser": {
        "userId": "daba101a-91c5-44c9-bb9b-e2a9a790571a",
        "isAnonymous": false,
        "cloud": "gcch"
    }
}

API 参考

MicrosoftTeamsUserIdentifierModel

电话号码

PhoneNumberIdentifierModel 表示电话号码。 服务假设电话号码采用 E.164 格式。

基本用法

// request
{
    "phoneNumber": {
        "value": "+112345556789"
    }
}

// response
{
    "kind": "phoneNumber",
    "rawId": "4:+112345556789",
    "phoneNumber": {
        "value": "+112345556789"
    }
}

API 参考

PhoneNumberIdentifierModel

Microsoft Teams 应用程序

MicrosoftTeamsAppIdentifierModel 代表 Teams 语音应用程序的机器人,例如呼叫队列和自动助理及其 Microsoft Entra 机器人对象 ID。 应使用资源帐户配置 Teams 应用程序。 可以通过 Microsoft Graph REST API /users 终结点从响应中的 id 属性检索 Microsoft Entra 机器人对象 ID。 有关如何使用 Microsoft Graph 的详细信息,请尝试使用 Graph 浏览器并查看 Graph SDK

基本用法

// request
{
    "microsoftTeamsApp": {
        "appId": "45ab2481-1c1c-4005-be24-0ffb879b1130"
    }
}

// response
{
    "kind": "microsoftTeamsApp",
    "rawId": "28:orgid:45ab2481-1c1c-4005-be24-0ffb879b1130",
    "microsoftTeamsApp": {
        "appId": "45ab2481-1c1c-4005-be24-0ffb879b1130"
    }
}


// if you're not operating in the public cloud, you must also pass the right Cloud type in a request
{
    "microsoftTeamsApp": {
        "appId": "45ab2481-1c1c-4005-be24-0ffb879b1130",
        "cloud": "gcch"
    }
}

// response
{
    "kind": "microsoftTeamsApp",
    "rawId": "28:gcch:45ab2481-1c1c-4005-be24-0ffb879b1130",
    "microsoftTeamsApp": {
        "appId": "45ab2481-1c1c-4005-be24-0ffb879b1130",
        "cloud": "gcch"
    }
}

API 参考

MicrosoftTeamsAppIdentifierModel

未知

如果服务中引入了新标识符,并且你使用的是旧 API 版本,该标识符将降级到 CommunicationIdentifierModel

基本用法

// request
{
    "rawId": "a raw id that originated in the service"
}

// response
{
    "kind": "unknown",
    "rawId": "a raw id that originated in the service"
}

API 参考

CommunicationIdentifierModel

如何处理响应中的 CommunicationIdentifierModel

最近的 API 版本填充了可用于区分的 kind 属性:

switch (communicationIdentifier.kind)
{
    case "communicationUser":
        console.log(`Communication user: ${communicationIdentifier.communicationUser.id}`);
        break;
    case "microsoftTeamsUser":
        console.log(`Teams user: ${communicationIdentifier.microsoftTeamsUser.userId}`);
        break;
    case "phoneNumber":
        console.log(`Phone number: ${communicationIdentifier.phoneNumber.value}`);
        break;
    case "unknown":
        console.log(`Unknown: ${communicationIdentifier.rawId}`);
        break;
    default:
        // this case should not be hit because adding a new identifier type requires a new API version
        // if it does get hit, please file an issue on https://github.com/Azure/azure-rest-api-specs/issues 
        break;
}

较旧的 API 版本中缺少 kind 属性,必须检查是否存在正确的子属性:

if (communicationIdentifier.communicationUser) {
    console.log(`Communication user: ${communicationIdentifier.communicationUser.id}`);
} else if (communicationIdentifier.microsoftTeamsUser) {
    console.log(`Teams user: ${communicationIdentifier.microsoftTeamsUser.userId}`);
} else if (communicationIdentifier.phoneNumber) {
    console.log(`Phone number: ${communicationIdentifier.phoneNumber.value}`);
} else {
    console.log(`Unknown: ${communicationIdentifier.rawId}`);
}

原始 ID 表示形式

有时需要将标识符序列化为平面字符串。 例如,如果你要将标识符存储在数据库表中,或者要将其用作 URL 参数。

为此,标识符可以采用名为 RawId 的另一种表示形式。 标识符始终可以转换为其对应的原始 ID,而有效的原始 ID 始终可以转换为标识符。

如果你正在使用 Azure SDK,它将帮助你进行转换。 如果直接使用 REST API,则需要手动生成原始 ID,如下所述。

通信用户

标识符:

{
    "communicationUser": {
        "id": "[communicationUserId]"
    }
}

原始 ID:

[communicationUserId]

原始 ID 与 communicationUser.id 相同。

Microsoft Teams 用户

标识符:

{
    "microsoftTeamsUser": {
        "userId": "[aadUserId]"
    }
}

原始 ID:

8:orgid:[aadUserId]

原始 ID 是前缀为 8:orgid: 的 Microsoft Entra 用户对象 ID。

标识符:

{
    "microsoftTeamsUser": {
        "userId": "[aadUserId]",
        "cloud": "gcch"
    }
}

原始 ID:

8:gcch:[aadUserId]

原始 ID 是前缀为 8:gcch:8:dod:(取决于云环境)的 Microsoft Entra 用户对象 ID。

标识符:

{
    "microsoftTeamsUser": {
        "userId": "[visitorUserId]",
        "isAnonymous": true
    }
}

原始 ID:

8:teamsvisitor:[visitorUserId]

原始 ID 是以 8:teamsvisitor: 为前缀的 Teams 访客 ID。 Teams 访客 ID 是 Teams 为使访客能够访问会议而生成的临时 ID。

电话号码

标识符:

{
    "phoneNumber": {
        "value": "+1123455567"
    }
}

原始 ID:

4:+1123455567

原始 ID 是前缀为 4: 的 E.164 格式电话号码。

未知

标识符:

{
    "rawId": "[unknown identifier id]"
}

原始 ID:

[unknown identifier id]

如果原始 ID 无效,则服务会使失败请求。

后续步骤