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

时间:2018-03-26 11:03:09

标签: c# asp.net .net botframework bots

我想将一个csv文件附件发送给bot中的用户。我正在使用Microsoft bot框架C#bot builder SDK,使用网络聊天频道。我有以下内容将csv文件发送给用户:

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 文件?

3 个答案:

答案 0 :(得分:2)

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

最好的方法是将文件添加到项目中的文件夹中,并在按钮的卡片操作值中添加引用。

例如:

    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);

答案 1 :(得分:1)

您的csv文件似乎存储在项目文件夹中,并且您希望将其作为附件从机器人发送到Web客户端用户。我使用Attachments API上传它,然后将其发送给用户,这对我有用,你可以参考它。

获取上传的附件并发送给用户:

var replymes = context.MakeMessage();

Attachment attachment = null;
attachment = await GetUploadedAttachmentAsync(replymes.ServiceUrl, replymes.Conversation.Id);

replymes.Attachments.Add(attachment);

await context.PostAsync(replymes); 

<强> GetUploadedAttachmentAsync:

private static async Task<Attachment> GetUploadedAttachmentAsync(string serviceUrl, string conversationId)
{
    var imagePath = System.Web.HttpContext.Current.Server.MapPath(@"~\csv_files\Results.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 = "Results.csv",
                OriginalBase64 = File.ReadAllBytes(imagePath),
                Type = "text/csv"
            });

        var attachmentUri = attachments.GetAttachmentUri(response.Id);

        return new Attachment
        {
            Name = "Results.csv",
            ContentType = "text/csv",
            ContentUrl = attachmentUri
        };
    }
}

在网络聊天中测试:

enter image description here

注意 据我所知,指定存储在本地文件夹或共享网络文件夹中的文件的路径,并直接作为附件发送到用户,在bot中不受支持。

答案 2 :(得分:0)

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
    {
        //img and excel is special keyword which provide image or excel
        //Get message reply use getmsg method
        switch (turnContext.Activity.Text)
        {
            case "img":
                await turnContext.SendActivityAsync(MessageFactory.Attachment(GetImg()), cancellationToken);
                break;
            case "excel":
                await turnContext.SendActivityAsync(MessageFactory.Attachment(GetImg()), cancellationToken);
                break;
            default:
                await turnContext.SendActivityAsync(MessageFactory.Text(GetMsg(turnContext.Activity.Text)), cancellationToken);
                break;
        }

    }

    private Attachment GetImg()
    {
        var imagePath = Path.Combine(Environment.CurrentDirectory, @"bin\Debug\img.png");
        var imageData = Convert.ToBase64String(File.ReadAllBytes(imagePath));

        return new Attachment
        {
            Name = @"img.png",
            ContentType = "image/png",
            ContentUrl = $"data:image/png;base64,{imageData}",
        };
    }