forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetConversationMembersDialog.cs
67 lines (57 loc) · 2.75 KB
/
GetConversationMembersDialog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
namespace GetConversationMembersBot
{
using System;
using System.Linq;
using System.Threading.Tasks;
using Autofac;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Connector;
[Serializable]
public class GetConversationMembersDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
context.Wait(this.MessageReceivedAsync);
}
public async virtual Task MessageReceivedAsync(IDialogContext context, IAwaitable<IActivity> result)
{
var activity = (Activity)await result;
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 :(");
}
}
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}");
}
}
context.Wait(this.MessageReceivedAsync);
}
}
}