Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I find the IDs needed for Bot Framework?

The IDs to use for Bot Framework are in most cases easy to find, because you receive them in the "Activity" object that is sent to the bot when the user initiates contact.

However, I am trying to use the Create Conversation endpoint, which means I have to know the ID of the user and the bot.

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

A simplified request (that works!) like this:

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

I need to find the user id (member id) somehow, maybe through Graph API? Or maybe through the Bot Framework API? But how?

Additionally, I'd like to be able to also programmatically find the Bot ID because I would be deploying this bot to many tenants and it would simplify the configuration a lot. However, where do I find the Bot ID, even manually? It doesn't look like it is the App Id from Azure or the Object id.

(I understand the prefix of 28 and 29, so that is not related to my question)

UPDATE:

The key take aways from the accepted answer are the following:

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

This means I cannot hope to find the userId somewhere else and that is a very important piece of information.

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

Apparently I can expect to receive a message in my bot even if the bot is just installed for a user. This would be my opportunity to find the userId.

When I try this out, I will confirm here whether that indeed happens in my scenario, which is a bot in a Personal Tab.

like image 379
Niels Brinch Avatar asked Jun 18 '21 13:06

Niels Brinch


People also ask

What is a bot ID?

For this article's purpose, a bot is simply an identity with linked accounts used by software. A bot's Active Directory account may look a lot like a human's Active Directory account.

What is bot framework authentication?

In the Bot Framework, two broad authentication categories exist: bot authentication and user authentication. Each has an associated token to allow access to secured resources. The following figure shows the elements involved in both bot and user authentication. In this figure: Host Platform is the bot hosting platform.

What is an ID field in the BOT framework?

This guide describes the characteristics of ID fields in the Bot Framework. Every Bot Framework channel is identified by a unique ID. Channel IDs serve as namespaces for other IDs.

How do I Find my bot ID?

For getting the bot id , you can find the Microsoft App ID from Azure portal of your bot configuration page which refers to the bot id of the bot service. Your bot can access additional context about the team or chat, such as user profile. A user ID can be found in the channel where your bot is connected .

Do I need an app ID and password for my bot?

For the most part, if you're using the Bot Framework SDK you don't need to care about the App ID and password. When you register your bot with the Azure Bot Service, you'll be given the App ID and password, you provide that when initializing the SDK, and that's the end of the story.

Where can I Find my bot framework password?

In the past, you needed to save your password somewhere secure, as the one pop up window when it was generated was your only opportunity to obtain it from the service. Now that the Bot Framework is on Azure, your bot credentials can be found by accessing the Resource Group which it is under.


Video Answer


3 Answers

For getting the bot id , you can find the Microsoft App ID from Azure portal of your bot configuration page which refers to the bot id of the bot service.

enter image description here

Your bot can access additional context about the team or chat, such as user profile.

A user ID can be found in the channel where your bot is connected . Your bot can query for the list of team members and their basic profiles. The basic profiles include Teams user IDs and Azure Active Directory (AAD) information such as name and object ID. enter image description here

  1. You can directly issue a GET request on /conversations/{teamId}/members/using the serviceUrl value as the endpoint.

The teamId can be found in the channeldata object of the activity payload that your bot receives in the following scenarios:

  • When a user messages or interacts with your bot in a team context.
  • When a new user or bot is added to a team. enter image description here

GET /v3/conversations/19:[email protected]/members

Response body
[{
    "id": "29:1GcS4EyB_oSI8A88XmWBN7NJFyMqe3QGnJdgLfFGkJnVelzRGos0bPbpsfJjcbAD22bmKc4GMbrY2g4JDrrA8vM06X1-cHHle4zOE6U4ttcc",
    "objectId": "9d3e08f9-a7ae-43aa-a4d3-de3f319a8a9c",
    "givenName": "Scott",
    "surname": "Mccall",
    "email": "[email protected]",
    "userPrincipalName": "[email protected]"
}, {
    "id": "29:1bSnHZ7Js2STWrgk6ScEErLk1Lp2zQuD5H2qQ960rtvstKp8tKLl-3r8b6DoW0QxZimuTxk_kupZ1DBMpvIQQUAZL-PNj0EORDvRZXy8kvWk",
    "objectId": "76b0b09f-d410-48fd-993e-84da521a597b",
    "givenName": "Allison",
    "surname": "Argent",
    "email": "[email protected]",
    "userPrincipalName": "[email protected]"
}]
  1. You can call GetConversationMembersAsync using Team.Id to return a list of user IDs.

// 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 example

[...]
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));
    }
  }
);

Using graph API or SDK (Note: Bot should be registered in app registration in active directory and the users also must be present in the Directory) :

  1. Get the Appid of the bot by using list and search :

GET https://graph.microsoft.com/v1.0/applications?$search="displayName:botname"&$count=true
ConsistencyLevel: eventual
  1. Get the list of users present in the active directory domain: (search by filtering the domain mail)
  • Using HTTP request

GET https://graph.microsoft.com/v1.0/users?$filter=endswith(mail,'[email protected]')&$orderby=userPrincipalName&$count=true
ConsistencyLevel: eventual
  • Using C#

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var users = await graphClient.Users
    .Request()
    .Header("ConsistencyLevel","eventual")
    .Filter("endswith(mail,'[email protected]')")
    .OrderBy("userPrincipalName")
    .GetAsync();

You will get a output like this :

 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": "[email protected]", 
"userPrincipalName": "[email protected]", 
"id": "e8b753b5-4117-464e-9a08-713e1ff266b3" 
} 
] 
}

Please refer to Integrate Bot with Azure Graph

Please refer a sample bot that uses Graph API.

like image 150
AnsumanBal-MT Avatar answered Oct 24 '22 06:10

AnsumanBal-MT


As per your requirement and I understood that you are trying to fetch member info from microsoft teams channel. You can easily find this out from the bot connector in the ms-team channel instead of using prefix 28 or 29. The Bot Connector REST API allows your bot to send and receive messages to channels and also the Bot State REST API allows a bot to store and retrieve state associated with users and conversations. So we can use the "Microsoft.Bot.Connector"to fetch the details of a user.

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);

The extension method "GetConversationMembersAsync" will fetch the member info from connector.

Output:

The "userInfo" object will return the following response. This details will contain important information like member id, objectId, tenantId,etc.

[{
"id": "29:15SDCCoTpDNJ_OiAdsOiMGAgg2S5HCRQvCt3ZVWbszpU5rSHkT95Zh2Tj1n-bhH4Sjc6zOBcuaSAUo-OtCm4ruD",
"name": "Rajeesh Menoth",
"aadObjectId": null,
"role": null,
"objectId": "bz341e75-60cf-8fdc-b490-04d6edd8a0f7",
"givenName": "Rajeesh",
"surname": "Raveendran",
"email": "[email protected]",
"userPrincipalName": "[email protected]",
"tenantId": "167f026d-8ffe-883f-5a11-9c2063481198",
"userRole": "user"
}]

Graph API:

If you don't have any ongoing conversation then, Graph API is the only way to get the user detail from application. BotID you can manually copy from bot channel registration service and user id you can pickup from graph API.

Please check this Microsoft Token Generator docs & Get user info using Graph API

Reference:

  1. Microsoft Connector Client
like image 29
Rajeesh Menoth Avatar answered Oct 24 '22 06:10

Rajeesh Menoth


While you're asking about bot id, I think your main question is really about proactive messaging. You're wanting bot id I think because of how some of the Microsoft docs recommend (really: "suggest") sending proactive messages. However, I prefer to use an approach that is much more simple, and requires only conversationid and serviceUrl. Please see here for a sample implementation (I've included both C# and Node versions).

In short, you get the Conversation Id and Service Url when the user installs the bot (personal scope, group chat, or Team channel). You can get it on any message the user sends actually, but getting it when they install the bot on activityUpdate is the best. You need to store Conversation Id and Service Url on your side, e.g. in a database, and then simply use them whenever you need to send the proactive message, as per the sample. Incidentally, I suggest also storing the timezone info from the activityUpdate, especially for users, because they may want messages sent at a particular time, depending on your app (8:00 for you might not be the same 8:00 for the user).

Update: Here's a video of an conference session I did which covered this topic - https://www.youtube.com/watch?v=mM7-fYdcJhw. The proactive stuff is about half-way through, but the early part might also be useful as background.

like image 2
Hilton Giesenow Avatar answered Oct 24 '22 07:10

Hilton Giesenow