在哪里可以找到BotFramework所需的ID?

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

但是,我正在尝试使用 Create Conversation 端点,这意味着我必须知道用户和机器人的 ID。

https://docs.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?但是如何?

此外,我还希望能够以编程方式找到 Bot ID,因为我会将这个 bot 部署到许多租户,它会大大简化配置。但是,我在哪里可以找到 Bot 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 的机会。

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

回答

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

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

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

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

teamId 可以在发现 channeldata 你的机器人接收在下列情况下活动有效载荷的对象:

  • 当用户在团队环境中向您的机器人发送消息或与之交互时。
  • 将新用户或机器人添加到团队时。

GET /v3/conversations/19:ja0cu120i1jod12j@skype.net/members

Response body
[{
    "id": "29:1GcS4EyB_oSI8A88XmWBN7NJFyMqe3QGnJdgLfFGkJnVelzRGos0bPbpsfJjcbAD22bmKc4GMbrY2g4JDrrA8vM06X1-cHHle4zOE6U4ttcc",
    "objectId": "9d3e08f9-a7ae-43aa-a4d3-de3f319a8a9c",
    "givenName": "Scott",
    "surname": "Mccall",
    "email": "Scott.Mccall@xyz.com",
    "userPrincipalName": "scall@xyz.com"
}, {
    "id": "29:1bSnHZ7Js2STWrgk6ScEErLk1Lp2zQuD5H2qQ960rtvstKp8tKLl-3r8b6DoW0QxZimuTxk_kupZ1DBMpvIQQUAZL-PNj0EORDvRZXy8kvWk",
    "objectId": "76b0b09f-d410-48fd-993e-84da521a597b",
    "givenName": "Allison",
    "surname": "Argent",
    "email": "Allison.Agrent@xyz.com",
    "userPrincipalName": "algent@xyz.com"
}]

  1. 您可以调用GetConversationMembersAsyncusingTeam.Id返回用户 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()}");

  1. 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(注意:Bot 应在应用程序注册中注册到活动目录中,并且用户也必须在目录中):

  1. 使用 list 和 search 获取机器人的 Appid :

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

  1. 获取活动目录域中存在的用户列表:(通过过滤域邮件进行搜索)
  • 使用 HTTP 请求

GET https://graph.microsoft.com/v1.0/users?$filter=endswith(mail,'a@xyz.com')&$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@xyz.com')")
    .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": "algent@xyz.com", 
"userPrincipalName": "algent@xyz.com", 
"id": "e8b753b5-4117-464e-9a08-713e1ff266b3" 
} 
] 
}

请参阅将机器人与 Azure Graph 集成

请参考使用 Graph API的 示例机器人。


以上是在哪里可以找到BotFramework所需的ID?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>