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

HeroCardExtensions.cs 2.4 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
68
  1. namespace ContosoFlowers.BotAssets.Extensions
  2. {
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Microsoft.Bot.Connector;
  6. public static class HeroCardExtensions
  7. {
  8. public static void AddHeroCard<T>(this IMessageActivity message, string title, string subtitle, IEnumerable<T> options, IEnumerable<string> images = default(IEnumerable<string>))
  9. {
  10. var heroCard = GenerateHeroCard(title, subtitle, options, images);
  11. if (message.Attachments == null)
  12. {
  13. message.Attachments = new List<Attachment>();
  14. }
  15. message.Attachments.Add(heroCard.ToAttachment());
  16. }
  17. public static void AddHeroCard(this IMessageActivity message, string title, string subtitle, IList<KeyValuePair<string, string>> options, IEnumerable<string> images = default(IEnumerable<string>))
  18. {
  19. var heroCard = GenerateHeroCard(title, subtitle, options, images);
  20. if (message.Attachments == null)
  21. {
  22. message.Attachments = new List<Attachment>();
  23. }
  24. message.Attachments.Add(heroCard.ToAttachment());
  25. }
  26. private static HeroCard GenerateHeroCard(string title, string subtitle, IEnumerable<KeyValuePair<string, string>> options, IEnumerable<string> images)
  27. {
  28. var actions = new List<CardAction>();
  29. foreach (var option in options)
  30. {
  31. actions.Add(new CardAction
  32. {
  33. Title = option.Key.ToString(),
  34. Type = ActionTypes.ImBack,
  35. Value = option.Value.ToString()
  36. });
  37. }
  38. var cardImages = new List<CardImage>();
  39. if (images != default(IEnumerable<string>))
  40. {
  41. foreach (var image in images)
  42. {
  43. cardImages.Add(new CardImage
  44. {
  45. Url = image,
  46. });
  47. }
  48. }
  49. return new HeroCard(title, subtitle, images: cardImages, buttons: actions);
  50. }
  51. private static HeroCard GenerateHeroCard<T>(string title, string subtitle, IEnumerable<T> options, IEnumerable<string> images)
  52. {
  53. return GenerateHeroCard(title, subtitle, options.Select(option => new KeyValuePair<string, string>(option.ToString(), option.ToString())), images);
  54. }
  55. }
  56. }
Tip!

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

Comments

Loading...