如何向用户发送由bot C#BotFramework SDK3创建的CSV文件

时间:2018-08-23 07:03:03

标签: botframework

作为标题,我想向用户发送由bot C#BotFramework SDK3创建的CSV文件。我将使用网络聊天。让用户下载它或将用户复制到桌面。两种方法都可以。可以吗?

1 个答案:

答案 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
        };
    }
}

测试结果:

enter image description here

更新:

将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
};