Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

GetConversationMembersDialog.cs 2.8 KB

You have to be logged in to leave a comment. Sign In
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
  1. namespace GetConversationMembersBot
  2. {
  3. using System;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Autofac;
  7. using Microsoft.Bot.Builder.Dialogs;
  8. using Microsoft.Bot.Builder.Dialogs.Internals;
  9. using Microsoft.Bot.Connector;
  10. [Serializable]
  11. public class GetConversationMembersDialog : IDialog<object>
  12. {
  13. public async Task StartAsync(IDialogContext context)
  14. {
  15. context.Wait(this.MessageReceivedAsync);
  16. }
  17. public async virtual Task MessageReceivedAsync(IDialogContext context, IAwaitable<IActivity> result)
  18. {
  19. var activity = (Activity)await result;
  20. if (activity.Type == ActivityTypes.ConversationUpdate)
  21. {
  22. if (activity.MembersAdded != null && activity.MembersAdded.Any())
  23. {
  24. string membersAdded = string.Join(
  25. ", ",
  26. activity.MembersAdded.Select(
  27. newMember => (newMember.Id != activity.Recipient.Id) ? $"{newMember.Name} (Id: {newMember.Id})"
  28. : $"{activity.Recipient.Name} (Id: {activity.Recipient.Id})"));
  29. await context.PostAsync($"Welcome {membersAdded}");
  30. }
  31. if (activity.MembersRemoved != null && activity.MembersRemoved.Any())
  32. {
  33. string membersRemoved = string.Join(
  34. ", ",
  35. activity.MembersRemoved.Select(
  36. removedMember => (removedMember.Id != activity.Recipient.Id) ? $"{removedMember.Name} (Id: {removedMember.Id})" : string.Empty));
  37. await context.PostAsync($"The following members {membersRemoved} were removed or left the conversation :(");
  38. }
  39. }
  40. if (activity.Type == ActivityTypes.Message)
  41. {
  42. using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
  43. {
  44. var client = scope.Resolve<IConnectorClient>();
  45. var activityMembers = await client.Conversations.GetConversationMembersAsync(activity.Conversation.Id);
  46. string members = string.Join(
  47. "\n ",
  48. activityMembers.Select(
  49. member => (member.Id != activity.Recipient.Id) ? $"* {member.Name} (Id: {member.Id})"
  50. : $"* {activity.Recipient.Name} (Id: {activity.Recipient.Id})"));
  51. await context.PostAsync($"These are the members of this conversation: \n {members}");
  52. }
  53. }
  54. context.Wait(this.MessageReceivedAsync);
  55. }
  56. }
  57. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...