【问题标题】:How to send attachment in bot framework in web embedded bot using c#如何使用 C# 在 Web 嵌入式机器人的机器人框架中发送附件
【发布时间】:2018-09-04 12:10:36
【问题描述】:

我想向机器人中的用户发送一个 csv 文件附件。我正在使用 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 文件?

【问题讨论】:

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


    【解决方案1】:

    您可以使用 herocard 来实现这一点。您的代码的问题是您没有指定文件名和位置,即如果您的文件名是 ProjectFile,那么按钮中的值必须包含 ProjectFile.csv

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

    例如:

        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】:

      您的 csv 文件似乎存储在您的项目文件夹中,您希望将其作为附件从您的机器人发送给网络客户端用户。我使用 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
              };
          }
      }
      

      网络聊天测试:

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

      【讨论】:

        【解决方案3】:
        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}",
                };
            }
        

        【讨论】:

          猜你喜欢
          • 2020-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-25
          • 2012-10-09
          • 2018-01-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多