How to send attachment in bot framework in web embedded bot using c#

放肆的年华 提交于 2020-01-05 04:18:07

问题


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;

I also tried using a hero card

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

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


回答1:


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

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.

eg:

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



回答2:


It seems that your csv file is stored in your project folder and you’d like to send it as attachment from your bot to web client user. I use Attachments API to upload it and then send it to user, which works for me, you can refer to it.

Get uploaded attachment and send to user:

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

Test in Web Chat:

Note: as far as I know, specify the path of the file that is stored in local folder or shared network folder and directly send as attachment to user, which are not supported in bot.



来源:https://stackoverflow.com/questions/49489927/how-to-send-attachment-in-bot-framework-in-web-embedded-bot-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!