A sample bot that retrieves the conversation's members list and detects when it changes.
The minimum prerequisites to run this sample are:
- The latest update of Visual Studio 2015. You can download the community version here for free.
- The Bot Framework Emulator. To install the Bot Framework Emulator, download it from here. Please refer to this documentation article to know more about the Bot Framework Emulator.
Your bot often needs to know when the state of the conversation it's in has changed. This may represent the bot being added to the conversation, or a person added or remove from the chat. When these changes happen, your bot will receive a conversationUpdate Activity.
In this event, the MembersAdded and MembersRemoved lists will contain the changes to the conversation since the last event. Both properties contain an array of ChannelAccount
used to represent an address a user or bot on a communication channel.
Check out the GetConversationMembersDialog.cs class handling the ConversationUpdate
Activity type.
if (activity.Type == ActivityTypes.ConversationUpdate)
{
if (activity.MembersAdded != null && activity.MembersAdded.Any())
{
string membersAdded = string.Join(
", ",
activity.MembersAdded.Select(
newMember => (newMember.Id != activity.Recipient.Id) ? $"{newMember.Name} (Id: {newMember.Id})"
: $"{activity.Recipient.Name} (Id: {activity.Recipient.Id})"));
await context.PostAsync($"Welcome {membersAdded}");
}
if (activity.MembersRemoved != null && activity.MembersRemoved.Any())
{
string membersRemoved = string.Join(
", ",
activity.MembersRemoved.Select(
removedMember => (removedMember.Id != activity.Recipient.Id) ? $"{removedMember.Name} (Id: {removedMember.Id})" : string.Empty));
await context.PostAsync($"The following members {membersRemoved} were removed or left the conversation :(");
}
}
To enumerate the members of a conversation you can use the GetConversationMembers
method from the Bot Connector class. This method returns an array of ChannelAccount
objects which are the members of the conversation.
Check out the GetConversationMembersDialog.cs class using the GetConversationMembersAsync
method to list the Conversation members.
if (activity.Type == ActivityTypes.Message)
{
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
var client = scope.Resolve<IConnectorClient>();
var activityMembers = await client.Conversations.GetConversationMembersAsync(activity.Conversation.Id);
string members = string.Join(
"\n ",
activityMembers.Select(
member => (member.Id != activity.Recipient.Id) ? $"* {member.Name} (Id: {member.Id})"
: $"* {activity.Recipient.Name} (Id: {activity.Recipient.Id})"));
await context.PostAsync($"These are the members of this conversation: \n {members}");
}
}
You will see the following in the Bot Framework Emulator when opening and running the sample solution.
You will see the following in Slack.
On the other hand, you will see the following in Skype.
To get more information about how to get started in Bot Builder for .NET and Activities please review the following resources:
Limitations
The functionality provided by the Bot Framework Activity can be used across many channels. Moreover, some special channel features can be unleashed using the ChannelData property.The Bot Framework does its best to support the reuse of your Bot in as many channels as you want. However, due to the very nature of some of these channels, some features are not fully portable.
The features used in this sample are fully supported in the following channels:
- Skype
- Slack
- DirectLine
- WebChat
- SMS
- Kik
- GroupMe
They are also supported, with some limitations, in the following channels:
- Microsoft Teams
On the other hand, they are not supported and the sample won't work as expected in the following channel:
- Telegram