forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendAttachmentDialog.cs
135 lines (112 loc) · 4.65 KB
/
SendAttachmentDialog.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
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
namespace SendAttachmentBot
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
[Serializable]
internal class SendAttachmentDialog : IDialog<object>
{
private const string ShowInlineAttachment = "(1) Show inline attachment";
private const string ShowUploadedAttachment = "(2) Show uploaded attachment";
private const string ShowInternetAttachment = "(3) Show Internet attachment";
private readonly IDictionary<string, string> options = new Dictionary<string, string>
{
{ "1", ShowInlineAttachment },
{ "2", ShowUploadedAttachment },
{ "3", ShowInternetAttachment }
};
public async Task StartAsync(IDialogContext context)
{
context.Wait(this.MessageReceivedAsync);
}
public async virtual Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
var welcomeMessage = context.MakeMessage();
welcomeMessage.Text = "Welcome, here you can see attachment alternatives:";
await context.PostAsync(welcomeMessage);
await this.DisplayOptionsAsync(context);
}
public async Task DisplayOptionsAsync(IDialogContext context)
{
PromptDialog.Choice<string>(
context,
this.ProcessSelectedOptionAsync,
this.options.Keys,
"What sample option would you like to see?",
"Ooops, what you wrote is not a valid option, please try again",
3,
PromptStyle.PerLine,
this.options.Values);
}
public async Task ProcessSelectedOptionAsync(IDialogContext context, IAwaitable<string> argument)
{
var message = await argument;
var replyMessage = context.MakeMessage();
Attachment attachment = null;
switch (message)
{
case "1":
attachment = GetInlineAttachment();
break;
case "2":
attachment = await GetUploadedAttachmentAsync(replyMessage.ServiceUrl, replyMessage.Conversation.Id);
break;
case "3":
attachment = GetInternetAttachment();
break;
}
// The Attachments property allows you to send and receive images and other content
replyMessage.Attachments = new List<Attachment> { attachment };
await context.PostAsync(replyMessage);
await this.DisplayOptionsAsync(context);
}
private static Attachment GetInlineAttachment()
{
var imagePath = HttpContext.Current.Server.MapPath("~/images/small-image.png");
var imageData = Convert.ToBase64String(File.ReadAllBytes(imagePath));
return new Attachment
{
Name = "small-image.png",
ContentType = "image/png",
ContentUrl = $"data:image/png;base64,{imageData}"
};
}
private static async Task<Attachment> GetUploadedAttachmentAsync(string serviceUrl, string conversationId)
{
var imagePath = HttpContext.Current.Server.MapPath("~/images/big-image.png");
using (var connector = new ConnectorClient(new Uri(serviceUrl)))
{
var attachments = new Attachments(connector);
var response = await attachments.Client.Conversations.UploadAttachmentAsync(
conversationId,
new AttachmentData
{
Name = "big-image.png",
OriginalBase64 = File.ReadAllBytes(imagePath),
Type = "image/png"
});
var attachmentUri = attachments.GetAttachmentUri(response.Id);
return new Attachment
{
Name = "big-image.png",
ContentType = "image/png",
ContentUrl = attachmentUri
};
}
}
private static Attachment GetInternetAttachment()
{
return new Attachment
{
Name = "BotFrameworkOverview.png",
ContentType = "image/png",
ContentUrl = "https://docs.microsoft.com/en-us/bot-framework/media/how-it-works/architecture-resize.png"
};
}
}
}