.Net , Bot Framework , Bot Framework V4 , Bots , C# ,

.Net , Bot Framework , Bot Framework V4 , Bots , C# , Tags: BotframeworkV4, Bots



When hearing the term “chat bot”, surely most people think about social media bots, QnA Bots e.t.c. But bots can serve different purposes. They can be used to channel messages, or automate certain tasks. These functionalities which diverge from the idea most people have of chat bots require certain specific features which are not available by default in normal bots. An example of these functionalities is getting information about the user with which the bot is communicating.

What We Will be Doing

This blog post will demonstrate how to get the profile information for every user in the Team. Then, get user info with your chat bot for a specific user. We will use Microsoft’s Botframework and the bot will be available on Microsoft Teams only. We will have access to only a few information about the user. These include user’s profile info.

Get User Info With your Chat Bot

If you like this post, you can chose to follow me on Twitter or Github and subscribe to this page’s notifications to stay updated with new articles or like my page on Facebook.

For this demo, we will use Microsoft’s Botframework v4. A specific nuget package will also be needed “Microsoft.Bot.Builder.Teams” which will help us perform easily several tasks relate to Microsoft Teams.

Get the Profile Info for Every User in The Team and The User Starting the Conversation

Obviously, the bot has be deployed in a Team.

First, we need to get the turn context “ITurnContext” from the bot. Using this object and helper classes in the Teams nugget package, we will get the team’s channel data.

List<ChannelAccount> teamMembers = (await turnContext.TurnState.Get<IConnectorClient>().Conversations.GetConversationMembersAsync( turnContext.Activity.GetChannelData<TeamsChannelData>().Team.Id).ConfigureAwait(false)).ToList(); 1 2 List < ChannelAccount > teamMembers = ( await turnContext . TurnState . Get < IConnectorClient > ( ) . Conversations . GetConversationMembersAsync ( turnContext . Activity . GetChannelData < TeamsChannelData > ( ) . Team . Id ) . ConfigureAwait ( false ) ) . ToList ( ) ;

To get the profile info of these team members, we need to access the property of the “ChannelAccount” named “Properties” which is a JObject. Then, convert these properties in a C# class as follows.

public class MicrosoftTeamUser { public string ObjectId { get; set; } public string GivenName { get; set; } public string Surname { get; set; } public string Email { get; set; } public string UserPrincipalName { get; set; } public string TenantId { get; set; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class MicrosoftTeamUser { public string ObjectId { get ; set ; } public string GivenName { get ; set ; } public string Surname { get ; set ; } public string Email { get ; set ; } public string UserPrincipalName { get ; set ; } public string TenantId { get ; set ; } }

To get every team member’s profile info, convert the JObject in each of the items in the teamMembers list to a MicrosoftTeamUser.

List<MicrosoftTeamUser> teamUsers = new List<MicrosoftTeamUser>(); string email = string.Empty; foreach (var item in teamMembers) { teamUsers.Add(JsonConvert.DeserializeObject<MicrosoftTeamUser>(item.Properties.ToString())); } 1 2 3 4 5 6 List < MicrosoftTeamUser > teamUsers = new List < MicrosoftTeamUser > ( ) ; string email = string . Empty ; foreach ( var item in teamMembers ) { teamUsers . Add ( JsonConvert . DeserializeObject < MicrosoftTeamUser > ( item . Properties . ToString ( ) ) ) ; }

Now, let’s do a more specific task. That is ; getting the email of the user who started the conversation with our bot.

To do this, we will just go through the members of our team. Then get the specific user who’s name and surname match with that of the user who started the conversation. Once we get this user, we will just need to get the email from the returned user object.

var email = teamUsers.Where(u => turnContext.Activity.From.Name.ToLower().Contains(u.GivenName.ToLower()) && turnContext.Activity.From.Name.ToLower().Contains(u.Surname.ToLower())).Select(u => u.Email).FirstOrDefault(); 1 var email = teamUsers . Where ( u = > turnContext . Activity . From . Name . ToLower ( ) . Contains ( u . GivenName . ToLower ( ) ) && turnContext . Activity . From . Name . ToLower ( ) . Contains ( u . Surname . ToLower ( ) ) ) . Select ( u = > u . Email ) . FirstOrDefault ( ) ;

Conclusion

With this, you can get user info with your chat bot in Teams. The Bot Builder Teams package has other functionalities which you can use to enhance your bot with. Here is a link the the repository for this package. You might be interested by this post about building a proactive bot with the Microsoft Botframework.

Like this: Like Loading...

Want to get your idea turned into software ? Visit To Tech .Follow me on social media and stay updated

If this post was useful to you, please share it and follow me on Twitter, Github or like my Facebook page.Follow me on social media and stay updated

Like this: Like Loading...

Like this: Like Loading...

Follow me on social media and stay updated