azure - 在哪里可以找到 Bot Framework 所需的 ID?

标签 azure microsoft-graph-api botframework microsoft-teams azure-bot-service

在大多数情况下,用于 Bot Framework 的 ID 很容易找到,因为您会在用户发起联系时发送到机器人的“事件”对象中收到它们。

但是,我正在尝试使用“创建对话”端点,这意味着我必须知道用户和机器人的 ID。

https://learn.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-connector-api-reference?view=azure-bot-service-4.0#create-conversation

一个简化的请求(有效!)如下所示:

{
"bot": {
    "id": "28:4a4f500c-4897-4eaf-a364-c67942f41f6f"
},
"members":[{
    "id": "29:1DUjC5z4ttsBQa0fX2O7B0IDu30R_6SfPMhwj-E1BwWmvYzb_IElqJwzPDocwPxTS0j8clYeb8gZx67V8TuChbA"
}],
"tenantId": "c7392b95-d07b-4653-87a7-6c709f527c42"
}

我需要以某种方式找到用户 ID(成员 ID),也许是通过 Graph API?或者也许通过 Bot Framework API?但如何呢?

此外,我还希望能够以编程方式查找机器人 ID,因为我会将此机器人部署到许 Multi-Tenancy ,这会大大简化配置。但是,我在哪里可以找到机器人 ID(即使是手动找到)?它看起来不像是来自 Azure 的应用程序 ID 或对象 ID。

(我理解28和29的前缀,所以这与我的问题无关)

更新:

已接受答案的关键要点如下:

The userId is unique to your bot ID and a particular user. You cannot reuse the userId between bots. The channelId is global.

这意味着我不能指望在其他地方找到 userId,而这是一条非常重要的信息。

When your app is installed in any particular context, you receive an onMembersAdded activity.

显然,即使机器人刚刚为用户安装,我也可以在机器人中收到消息。这将是我找到 userId 的机会。

当我尝试这个时,我将在这里确认这是否确实发生在我的场景中,即个人选项卡中的机器人。

最佳答案

要获取机器人 ID,您可以从机器人配置页面的 Azure 门户中找到 Microsoft App ID,它指的是机器人服务的机器人 ID。

enter image description here

您的机器人可以访问有关团队或聊天的其他上下文,例如用户个人资料。

可以在您的机器人连接的 channel 中找到用户 ID。您的机器人可以查询团队成员列表及其基本资料。基本配置文件包括 Teams 用户 ID 和 Azure Active Directory (AAD) 信息,例如名称和对象 ID。 enter image description here

  1. 您可以使用 serviceUrl 值作为端点,直接在 /conversations/{teamId}/members/ 上发出 GET 请求。

在以下情况下,您的机器人收到的事件负载的 channeldata 对象中可以找到 teamId:

  • 当用户在团队环境中向您的机器人发送消息或互动时。
  • 当新用户或机器人添加到团队时。 enter image description here

GET /v3/conversations/19:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="caa0abfaa9bffbf8faa3fba0a5aefbf8a08ab9a1b3baafe4a4afbe" rel="noreferrer noopener nofollow">[email protected]</a>/members

Response body
[{
    "id": "29:1GcS4EyB_oSI8A88XmWBN7NJFyMqe3QGnJdgLfFGkJnVelzRGos0bPbpsfJjcbAD22bmKc4GMbrY2g4JDrrA8vM06X1-cHHle4zOE6U4ttcc",
    "objectId": "9d3e08f9-a7ae-43aa-a4d3-de3f319a8a9c",
    "givenName": "Scott",
    "surname": "Mccall",
    "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97c4f4f8e3e3b9daf4f4f6fbfbd7efeeedb9f4f8fa" rel="noreferrer noopener nofollow">[email protected]</a>",
    "userPrincipalName": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8dbcbc9c4c4e8d0d1d286cbc7c5" rel="noreferrer noopener nofollow">[email protected]</a>"
}, {
    "id": "29:1bSnHZ7Js2STWrgk6ScEErLk1Lp2zQuD5H2qQ960rtvstKp8tKLl-3r8b6DoW0QxZimuTxk_kupZ1DBMpvIQQUAZL-PNj0EORDvRZXy8kvWk",
    "objectId": "76b0b09f-d410-48fd-993e-84da521a597b",
    "givenName": "Allison",
    "surname": "Argent",
    "email": "Allis<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcd3d292fddbced9d2c8fcc4c5c692dfd3d1" rel="noreferrer noopener nofollow">[email protected]</a>",
    "userPrincipalName": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d2c212a2823390d353437632e2220" rel="noreferrer noopener nofollow">[email protected]</a>"
}]

  • 您可以使用 Team.Id 调用 GetConversationMembersAsync 以返回用户 ID 列表。
  • // Fetch the members in the current conversation
    var connector = new ConnectorClient(new Uri(context.Activity.ServiceUrl));
    var teamId = context.Activity.GetChannelData<TeamsChannelData>().Team.Id;
    var members = await connector.Conversations.GetConversationMembersAsync(teamId);
    
    // Concatenate information about all members into a string
    var sb = new StringBuilder();
    foreach (var member in members.AsTeamsChannelAccounts())
    {
        sb.AppendFormat(
            "GivenName = {0}, TeamsMemberId = {1}",
            member.Name, member.Id);
    
        sb.AppendLine();
    }
    
    // Post the member info back into the conversation
    await context.PostAsync($"People in this conversation: {sb.ToString()}");

  • node.js 示例
  • [...]
    import * as builder from "botbuilder";
    [...]
    
    var teamId = session.message.sourceEvent.team.id;
    connector.fetchMembers(
      (<builder.IChatConnectorAddress>session.message.address).serviceUrl,
      teamId,
      (err, result) => {
        if (err) {
          session.endDialog('There is some error');
        }
        else {
          session.endDialog('%s', JSON.stringify(result));
        }
      }
    );

    使用图形 API 或 SDK(注意:机器人应在事件目录中的应用程序注册中注册,并且用户也必须存在于目录中):

    1. 使用列表和搜索获取机器人的 Appid:

    GET https://graph.microsoft.com/v1.0/applications?$search="displayName:botname"&$count=true
    ConsistencyLevel: eventual

  • 获取 Active Directory 域中存在的用户列表: (通过过滤域名邮件进行搜索)
    • 使用 HTTP 请求

    GET https://graph.microsoft.com/v1.0/users?$filter=endswith(mail,'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e7f5e666764307d7173" rel="noreferrer noopener nofollow">[email protected]</a>')&$orderby=userPrincipalName&$count=true
    ConsistencyLevel: eventual

    • 使用 C#

    GraphServiceClient graphClient = new GraphServiceClient( authProvider );
    
    var users = await graphClient.Users
        .Request()
        .Header("ConsistencyLevel","eventual")
        .Filter("endswith(mail,'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="10715068696a3e737f7d" rel="noreferrer noopener nofollow">[email protected]</a>')")
        .OrderBy("userPrincipalName")
        .GetAsync();

    您将得到如下输出:

     HTTP/1.1 200 OK 
    Content-type: application/json 
    { 
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users", 
    "@odata.count": 1, 
    "value": [ 
    { 
    "displayName": "Allison Argent", 
    "givenName": "Allison", 
    "jobTitle": "Senior Engineer", 
    "mail": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8beae7eceee5ffcbf3f2f1a5e8e4e6" rel="noreferrer noopener nofollow">[email protected]</a>", 
    "userPrincipalName": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98f9f4fffdf6ecd8e0e1e2b6fbf7f5" rel="noreferrer noopener nofollow">[email protected]</a>", 
    "id": "e8b753b5-4117-464e-9a08-713e1ff266b3" 
    } 
    ] 
    }
    

    请引用Integrate Bot with Azure Graph

    请引用sample使用 Graph API 的机器人。

    关于azure - 在哪里可以找到 Bot Framework 所需的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68036094/

    相关文章:

    azure - 如何使用 Azure 资源管理器选择最新的 Ubuntu 14.04 LTS 镜像?

    azure - Azure 存储模拟器是否支持文件共享?

    asp.net-mvc - 服务应用程序的 Microsoft Graph API 身份验证

    azure-functions - Microsoft Graph 更改通知是否始终为 CREATED 事件发送 UPDATED 通知?

    python - 使用 Python 的 Microsoft Teams 机器人

    azure - 如何针对特定 LUIS 意图开始对话?

    windows - 尝试通过 RDP 连接到 Windows Azure

    Magento 2 : Error when listing customers or adding a new one

    azure - 使用 https ://graph. microsoft.com 和 ActiveDirectoryClient 将用户添加到 Azure Active Directory

    c# - 机器人框架.Net。在 Bot 上添加输入指示器