As the title, I want to send user a CSV file which is created by bot C# BotFramework SDK3.. I'm going to use webchat. Let user Download it or Copy the users Desktop. Either way is ok. Is it possble?
1 Answer
If your CSV file is stored in your project folder, to send a CSV file to the user(s), you can refer to the following sample code.
var replymes = context.MakeMessage();
replymes.Text = "Here is a CSV file.";
replymes.Attachments.Add(await GetCSVAttachmentAsync(replymes.ServiceUrl, replymes.Conversation.Id));
await context.PostAsync(replymes);
The implementation of GetCSVAttachmentAsync:
private static async Task<Attachment> GetCSVAttachmentAsync(string serviceUrl, string conversationId)
{
var filePath = System.Web.HttpContext.Current.Server.MapPath(@"~\csv_files\userinfo.csv");
using (var connector = new ConnectorClient(new Uri(serviceUrl)))
{
var attachments = new Attachments(connector);
var response = await attachments.Client.Conversations.UploadAttachmentAsync(
conversationId,
new AttachmentData
{
Name = "userinfo.csv",
OriginalBase64 = System.IO.File.ReadAllBytes(filePath),
Type = "text/csv"
});
var attachmentUri = attachments.GetAttachmentUri(response.Id);
return new Attachment
{
Name = "userinfo.csv",
ContentType = "text/csv",
ContentUrl = attachmentUri
};
}
}
Test result:
Update:
Store the CSV file in Azure storage blob, and send it as attachment.
var storageAccount = CloudStorageAccount.Parse("{storage_connection_string}");
var blobClient = storageAccount.CreateCloudBlobClient();
var cloudBlobContainer = blobClient.GetContainerReference("mycontainer");
cloudBlobContainer.CreateIfNotExists();
var cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("userinfo.csv");
cloudBlockBlob.UploadFromFile(System.Web.HttpContext.Current.Server.MapPath(@"~\csv_files\userinfo.csv"));
var url = cloudBlockBlob.Uri.ToString();
return new Attachment
{
Name = "userinfo.csv",
ContentType = "text/csv",
ContentUrl = url
};
6 Comments
Jeff_hu
Thank you so much.. It works. I think it's not good to save it under project folder. I decide to manage it in blob storage.
Jeff_hu
Do you know how could create CSV be created in blob storage and write it?
Fei Han
Please refer to my updates, the code of storing the csv in Azure storage blob works for me.
Jeff_hu
Sorry for my bad descriptions. CSV is not is stored in my project folder. If possible, I want to send it to user without create a real CSV file( because, something important information would be saved). But user can download it as a CSV file. If it is impossible, I want to try store to Blob storage directly without creating a real file in local.
Jeff_hu
Thank you so much. I found if I set my data here to "OriginalBase64" directly, I don't need to create a real file anymore. Is it a proper way to use it?
|
