作为标题,我想向用户发送由bot C#BotFramework SDK3创建的CSV文件。我将使用网络聊天。让用户下载它或将用户复制到桌面。两种方法都可以。可以吗?
答案 0 :(得分:2)
如果您的CSV文件存储在您的项目文件夹中,要向用户发送CSV文件,您可以参考以下示例代码。
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);
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
};
}
}
测试结果:
更新:
将CSV文件存储在Azure存储blob中,并将其作为附件发送。
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
};