Nie*_*nch 5 azure botframework microsoft-teams azure-bot-service microsoft-graph-api
在大多数情况下,用于 Bot Framework 的 ID 很容易找到,因为您会在用户发起联系时发送给机器人的“活动”对象中接收它们。
但是,我正在尝试使用 Create Conversation 端点,这意味着我必须知道用户和机器人的 ID。
一个简化的请求(有效!)像这样:
{
"bot": {
"id": "28:4a4f500c-4897-4eaf-a364-c67942f41f6f"
},
"members":[{
"id": "29:1DUjC5z4ttsBQa0fX2O7B0IDu30R_6SfPMhwj-E1BwWmvYzb_IElqJwzPDocwPxTS0j8clYeb8gZx67V8TuChbA"
}],
"tenantId": "c7392b95-d07b-4653-87a7-6c709f527c42"
}
Run Code Online (Sandbox Code Playgroud)
我需要以某种方式找到用户 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.
Run Code Online (Sandbox Code Playgroud)
这意味着我不能希望在其他地方找到 userId,这是一条非常重要的信息。
When your app is installed in any particular context, you receive an onMembersAdded activity.
Run Code Online (Sandbox Code Playgroud)
显然,即使机器人刚刚为用户安装,我也可以期望在我的机器人中收到一条消息。这将是我找到 userId 的机会。
当我尝试这个时,我将在此处确认这是否确实发生在我的场景中,即个人选项卡中的机器人。
要获取 bot id ,您可以从 bot 配置页面的 Azure 门户中找到 Microsoft App ID,它指的是 bot 服务的 bot id。
您的机器人可以访问有关团队或聊天的其他上下文,例如用户个人资料。
可以在您的机器人连接的频道中找到用户 ID。您的机器人可以查询团队成员列表及其基本资料。基本配置文件包括 Teams 用户 ID 和 Azure Active Directory (AAD) 信息,例如名称和对象 ID。

/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"
}]Run Code Online (Sandbox Code Playgroud)
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()}");Run Code Online (Sandbox Code Playgroud)
[...]
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));
}
}
);Run Code Online (Sandbox Code Playgroud)
使用图形 API 或 SDK(注意:Bot 应在应用程序注册中注册到活动目录中,并且用户也必须在目录中):
GET https://graph.microsoft.com/v1.0/applications?$search="displayName:botname"&$count=true
ConsistencyLevel: eventualRun Code Online (Sandbox Code Playgroud)
GET https://graph.microsoft.com/v1.0/users?$filter=endswith(mail,'a@xyz.com')&$orderby=userPrincipalName&$count=true
ConsistencyLevel: eventualRun Code Online (Sandbox Code Playgroud)
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var users = await graphClient.Users
.Request()
.Header("ConsistencyLevel","eventual")
.Filter("endswith(mail,'a@xyz.com')")
.OrderBy("userPrincipalName")
.GetAsync();Run Code Online (Sandbox Code Playgroud)
你会得到这样的输出:
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"
}
]
}
Run Code Online (Sandbox Code Playgroud)
请参考使用 Graph API的 示例机器人。
根据您的要求,我了解到您正在尝试从微软团队频道获取成员信息。您可以从 ms-team 通道中的机器人连接器轻松找到这一点,而不是使用前缀 28 或 29。 机器人连接器 REST API 允许您的机器人向通道发送和接收消息,并且机器人状态 REST API 允许机器人存储和检索与用户和对话相关的状态。因此我们可以使用“Microsoft.Bot.Connector”来获取用户的详细信息。
var connector = new ConnectorClient(new Uri(turnContext.Activity.ServiceUrl), "MicrosoftAppId", "MicrosoftAppPassword");
var conversationId = turnContext.Activity.Conversation.Id;
var channelInfo = turnContext.Activity.ChannelId;
var userInfo = await connector.Conversations.GetConversationMembersAsync(conversationId);
Run Code Online (Sandbox Code Playgroud)
扩展方法“GetConversationMembersAsync”将从连接器获取成员信息。
输出:
“userInfo”对象将返回以下响应。此详细信息将包含重要信息,如成员 id、objectId、tenantId 等。
[{
"id": "29:15SDCCoTpDNJ_OiAdsOiMGAgg2S5HCRQvCt3ZVWbszpU5rSHkT95Zh2Tj1n-bhH4Sjc6zOBcuaSAUo-OtCm4ruD",
"name": "Rajeesh Menoth",
"aadObjectId": null,
"role": null,
"objectId": "bz341e75-60cf-8fdc-b490-04d6edd8a0f7",
"givenName": "Rajeesh",
"surname": "Raveendran",
"email": "Rajeesh.Menoth@menothsoft.com",
"userPrincipalName": "Rajeesh.Menoth@menothsoft.com",
"tenantId": "167f026d-8ffe-883f-5a11-9c2063481198",
"userRole": "user"
}]
Run Code Online (Sandbox Code Playgroud)
图形API:
如果您没有任何正在进行的对话,那么 Graph API 是从应用程序获取用户详细信息的唯一方法。您可以从机器人通道注册服务手动复制 BotID,您可以从图形 API 中获取用户 ID。
请检查此Microsoft 令牌生成器文档并使用 Graph API 获取用户信息
参考:
| 归档时间: |
|
| 查看次数: |
555 次 |
| 最近记录: |