接收所有对话消息

最初为 Microsoft Teams Graph API 开发的特定于资源的许可 (RSC) 权限模型正在扩展到机器人方案。 使用 RSC,聊天所有者可以同意机器人在标准频道和聊天中接收所有用户消息,而无需为 @mentioned。 可以通过在应用清单中指定 ChannelMessage.Read.GroupChatMessage.Read.Chat 权限字符串来启用此功能, (以前称为 Teams 应用清单) 。 发布应用更新后,对话所有者可以在应用安装或升级过程中授予许可。 有关为应用和租户内部启用 RSC 的详细信息,请参阅 特定于资源的许可

注意

使用 RSC 接收所有聊天消息的机器人在 政府社区云 (GCC) 、GCC-High 和国防部 (DOD) 环境中受支持。

使机器人能够接收所有频道或聊天消息

ChannelMessage.Read.GroupChatMessage.Read.Chat RSC 权限正在扩展到机器人。 在用户同意和应用安装的情况下,这些权限:

  • 允许指定的图形应用程序分别获取频道和聊天中的所有消息。
  • 启用在应用清单中定义的机器人以接收所有对话消息,而无需 @mentioned 在应用权限的相关上下文中。

筛选提及消息

// When ChannelMessage.Read.Group or ChatMessage.Read.Chat RSC is in the app manifest, this method is called even when bot is not @mentioned.
// This code snippet allows the bot to ignore all messages that do not @mention the bot.
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
        // Ignore the message if bot was not mentioned. 
        // Remove this if block to process all messages received by the bot.
        if (!turnContext.Activity.GetMentions().Any(mention => mention.Mentioned.Id.Equals(turnContext.Activity.Recipient.Id, StringComparison.OrdinalIgnoreCase)))
        {
            return;
        }
        // Sends an activity to the sender of the incoming activity.
        await turnContext.SendActivityAsync(MessageFactory.Text("Using RSC the bot can receive messages across channels or chats in team without being @mentioned."));
}

RSC 权限

需要访问所有 Teams 消息数据的服务必须使用 Graph API 来提供对频道和聊天中存档数据的访问权限。 机器人必须适当地使用 ChannelMessage.Read.GroupChatMessage.Read.Chat RSC 权限来构建和增强用户的吸引力体验,以便通过 Microsoft Teams 应用商店审批。 应用说明必须包括机器人如何使用它读取的数据:

  • ChannelMessage.Read.Group机器人不能使用 和 ChatMessage.Read.Chat RSC 权限来提取大量客户数据。
  • 仅当重新安装或新安装聊天后,机器人才能使用 ChatMessage.Read.Chat 在聊天中接收所有消息。
  • 如果你有使用 GRAPH 方案的 RSC 权限的应用 ChatMessage.Read.Chat ,请按照在 对话中上传自定义应用 中的步骤测试应用,并在该功能 正式发布之前修改应用。 如果不希望机器人接收所有聊天消息,请实现以下 代码片段。 如果未执行任何操作,机器人将在新安装后收到所有消息。

更新应用清单

若要让机器人接收所有聊天消息,必须在应用清单的 属性中 authorization.permissions.resourceSpecific 指定相关的 RSC 权限字符串。 有关详细信息,请参阅 应用清单架构

显示应用清单中要进行的更改的屏幕截图。

以下代码是应用清单示例:

  • webApplicationInfo.id:Microsoft Entra应用 ID。 应用 ID 可以与机器人 ID 相同。
  • webApplicationInfo.resource:任何字符串。 资源字段在 RSC 中没有操作,但必须使用值添加以避免错误响应。
  • authorization.permissions.resourceSpecific:应用的 RSC 权限,其中一个或两者都ChannelMessage.Read.Group指定。ChatMessage.Read.Chat 有关详细信息,请参阅特定于资源的权限

以下代码提供了应用清单版本 1.12 或更高版本的示例:

{
    "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.12/MicrosoftTeams.schema.json",
    "manifestVersion": "1.12",
    "version": "1.0.0",
    "id": "8239c8f3-ed78-4512-933e-babfd28856f1",
    "packageName": "com.contoso.rscechobot",
    "developer": {
        "name": "Contoso",
        "websiteUrl": "https://www.contoso.com",
        "privacyUrl": "https://www.contoso.com/privacy",
        "termsOfUseUrl": "https://www.contoso.com/tos"
    },
    "icons": {
        "color": "color.png",
        "outline": "outline.png"
    },
    "name": {
        "short": "RscEchoBot",
        "full": "Echo bot with RSC configured for all conversation messages"
    },
    "description": {
        "short": "Echo bot with RSC configured for all channel and chat messages",
        "full": "Echo bot configured with all channel and chat messages RSC permission in manifest"
    },
    "accentColor": "#FFFFFF",
    "staticTabs": [
        {
            "entityId": "conversations",
            "scopes": [
                "personal"
            ]
        },
        {
            "entityId": "about",
            "scopes": [
                "personal"
            ]
        }
    ],
    "webApplicationInfo": {
        "id": "07338883-af76-47b3-86e4-2603c50be638",
        "resource": "https://AnyString"
    },
    "authorization": {
        "permissions": {
            "resourceSpecific": [
                {
                    "type": "Application",
                    "name": "ChannelMessage.Read.Group"
                },
                {
                    "type": "Application",
                    "name": "ChatMessage.Read.Chat"
                }
            ]
        }
    },
    "bots": [
        {
            "botId": "07338883-af76-47b3-86e4-2603c50be638",
            "scopes": [
                "personal",
                "team",
                "groupchat"
            ],
            "supportsFiles": false,
            "isNotificationOnly": false
        }
    ],
    "permissions": [
        "identity",
        "messageTeamMembers"
    ],
    "validDomains": []
}

在对话中上传自定义应用

以下步骤将指导你上传和验证接收团队中所有频道消息的机器人,而不使用 @mentioned:

  1. 选择或创建团队。

  2. 从左窗格中选择“●●”。 此时会显示下拉菜单。

  3. 从下拉菜单中选择“ 管理团队 ”。

    屏幕截图显示了 Teams 应用程序中的“管理团队”选项。

  4. 选择“应用”。 此时将显示多个应用。

  5. 从右下角选择上传自定义应用

    屏幕截图显示了“上传自定义应用”选项。

  6. 选择 “打开”

    屏幕截图显示了用于选择应用包的打开对话框。

  7. 从应用详细信息弹出窗口中选择“添加”,以便将机器人添加到选定团队。

    屏幕截图显示了用于将机器人添加到团队的“添加”按钮。

  8. 选择一个频道并在机器人频道中输入消息。

    机器人接收消息时不为 @mentioned。

    屏幕截图显示机器人在通道中接收消息。

代码段

以下代码提供了 RSC 权限的示例:


// Handle when a message is addressed to the bot.
// When rsc is enabled the method will be called even when bot is addressed without being @mentioned.
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
        // Sends an activity to the sender of the incoming activity.
         await turnContext.SendActivityAsync(MessageFactory.Text("Using RSC the bot can receive messages across channels or chats in team without being @mentioned."));
}

代码示例

示例名称 Description .NET Node.js 应用部件清单
具有 RSC 权限的频道消息 此示例应用演示机器人如何在不使用 的情况下 @mentioned使用 RSC 接收所有通道消息。 View View View

另请参阅