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

SendAttachmentDialog.cs 4.7 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  1. namespace SendAttachmentBot
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using Microsoft.Bot.Builder.Dialogs;
  9. using Microsoft.Bot.Connector;
  10. [Serializable]
  11. internal class SendAttachmentDialog : IDialog<object>
  12. {
  13. private const string ShowInlineAttachment = "(1) Show inline attachment";
  14. private const string ShowUploadedAttachment = "(2) Show uploaded attachment";
  15. private const string ShowInternetAttachment = "(3) Show Internet attachment";
  16. private readonly IDictionary<string, string> options = new Dictionary<string, string>
  17. {
  18. { "1", ShowInlineAttachment },
  19. { "2", ShowUploadedAttachment },
  20. { "3", ShowInternetAttachment }
  21. };
  22. public async Task StartAsync(IDialogContext context)
  23. {
  24. context.Wait(this.MessageReceivedAsync);
  25. }
  26. public async virtual Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
  27. {
  28. var message = await result;
  29. var welcomeMessage = context.MakeMessage();
  30. welcomeMessage.Text = "Welcome, here you can see attachment alternatives:";
  31. await context.PostAsync(welcomeMessage);
  32. await this.DisplayOptionsAsync(context);
  33. }
  34. public async Task DisplayOptionsAsync(IDialogContext context)
  35. {
  36. PromptDialog.Choice<string>(
  37. context,
  38. this.ProcessSelectedOptionAsync,
  39. this.options.Keys,
  40. "What sample option would you like to see?",
  41. "Ooops, what you wrote is not a valid option, please try again",
  42. 3,
  43. PromptStyle.PerLine,
  44. this.options.Values);
  45. }
  46. public async Task ProcessSelectedOptionAsync(IDialogContext context, IAwaitable<string> argument)
  47. {
  48. var message = await argument;
  49. var replyMessage = context.MakeMessage();
  50. Attachment attachment = null;
  51. switch (message)
  52. {
  53. case "1":
  54. attachment = GetInlineAttachment();
  55. break;
  56. case "2":
  57. attachment = await GetUploadedAttachmentAsync(replyMessage.ServiceUrl, replyMessage.Conversation.Id);
  58. break;
  59. case "3":
  60. attachment = GetInternetAttachment();
  61. break;
  62. }
  63. // The Attachments property allows you to send and receive images and other content
  64. replyMessage.Attachments = new List<Attachment> { attachment };
  65. await context.PostAsync(replyMessage);
  66. await this.DisplayOptionsAsync(context);
  67. }
  68. private static Attachment GetInlineAttachment()
  69. {
  70. var imagePath = HttpContext.Current.Server.MapPath("~/images/small-image.png");
  71. var imageData = Convert.ToBase64String(File.ReadAllBytes(imagePath));
  72. return new Attachment
  73. {
  74. Name = "small-image.png",
  75. ContentType = "image/png",
  76. ContentUrl = $"data:image/png;base64,{imageData}"
  77. };
  78. }
  79. private static async Task<Attachment> GetUploadedAttachmentAsync(string serviceUrl, string conversationId)
  80. {
  81. var imagePath = HttpContext.Current.Server.MapPath("~/images/big-image.png");
  82. using (var connector = new ConnectorClient(new Uri(serviceUrl)))
  83. {
  84. var attachments = new Attachments(connector);
  85. var response = await attachments.Client.Conversations.UploadAttachmentAsync(
  86. conversationId,
  87. new AttachmentData
  88. {
  89. Name = "big-image.png",
  90. OriginalBase64 = File.ReadAllBytes(imagePath),
  91. Type = "image/png"
  92. });
  93. var attachmentUri = attachments.GetAttachmentUri(response.Id);
  94. return new Attachment
  95. {
  96. Name = "big-image.png",
  97. ContentType = "image/png",
  98. ContentUrl = attachmentUri
  99. };
  100. }
  101. }
  102. private static Attachment GetInternetAttachment()
  103. {
  104. return new Attachment
  105. {
  106. Name = "BotFrameworkOverview.png",
  107. ContentType = "image/png",
  108. ContentUrl = "https://docs.microsoft.com/en-us/bot-framework/media/how-it-works/architecture-resize.png"
  109. };
  110. }
  111. }
  112. }
Tip!

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

Comments

Loading...