如何使用C#在Web嵌入式bot的bot框架中发送附件

问题描述:

我想将CSV文件附件发送给bot中的用户.我正在使用Web聊天频道使用Microsoft机器人框架C#机器人构建器SDK.以下是将CSV文件发送给用户的信息:

I want to send a csv file attachment to to user in bot. I am using the Microsoft bot framework C# bot builder SDK, using the web chat channel. I've the following to send a csv file to the user:

var csvPath = "D://ProjecteFile/Results.csv";
//Attachment attachment = new Attachment();
//attachment.ContentType = "text/csv";
//attachment.ContentUrl = csvPath;
//attachment.Name = "click this link to get server report";
//return attachment;

我也尝试过使用英雄卡

var heroCard = new HeroCard {
    Title = "Click to download Report",
        Buttons = new List < CardAction > {
            new CardAction(ActionTypes.OpenUrl, "Get Started", value: "file://L-156222101/ProjectFile)")
        };

    return heroCard.ToAttachment();
}

我的机器人是否可以通过网络聊天将 csv 文件发送给用户?

Is there any way for my bot to send a csv file to a user by via web chat?

您可以使用英雄卡来实现.代码的问题是您没有指定文件名以及位置,即如果文件名是ProjectFile,则按钮中的值必须包含ProjectFile.csv

You can achieve this using a herocard. The problem with your code is that you are not specifying the filename along with the location i.e if your filename is ProjectFile then the value in the button must contain ProjectFile.csv

最好的方法是将文件添加到项目中的文件夹中,并在按钮中的Card Action值中添加引用.

The best approach is to add the file to a folder in your project and add the reference in the value of the Card Action in the button.

例如:

    Attachment attachment = new HeroCard
    {
        Title = "Click to download Report",
        Buttons = new List<CardAction>()
        {
            new CardAction()
            {
                Title = "Get Started",
                Type = ActionTypes.OpenUrl,
                Value = "D:\\ProjecteFile\\Results.csv"
            }
         }
    }.ToAttachment();

    var reply = context.MakeMessage();
    reply.Attachments.Add(attachment);
    await context.PostAsync(reply);