使用 Microsoft bot 框架下载文件 (pdf/image)

downloading file (pdf/image) from using Microsoft bot framework

我想下载 document/image(Document/image 在互联网上,我给出了它的路径)。但它不起作用.. 如果我只是评论附件部分,我可以从 BOT 获得 "Hi"。

让控制器像这样

  [BotAuthentication]
  public class MessagesController : ApiController
  {
    /// <summary>
    /// POST: api/Messages
    /// Receive a message from a user and reply to it
    /// </summary>
    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {

               ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
               Activity reply = activity.CreateReply("Hi");
               activity.Attachments.Add(new Attachment()
                { 
                    ContentUrl =   "https://upload.wikimedia.org/wikipedia/en/a/a6/Bender_Rodriguez.png",
                    ContentType = "Image/png",
                    Name = "Bender_Rodriguez.png"
                });

                await connector.Conversations.ReplyToActivityAsync(reply);
    }

    }

您可能会在附件中遇到空引用异常。您检查过异常情况吗?

尝试:

reply.Attachments = new List< Attachment >();

这行代码之后你的代码有误

Activity reply = activity.CreateReply("Hi");

您正在将附件添加到 activity 对象而不是 回复。您收到“Hi”的回复,因为您没有将附件添加到 回复 参考。

我已经修改了你的代码,它可以正常工作并在 Bot Framework Emulator 上成功显示图像。

代码

        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
        Activity reply = activity.CreateReply("Hi");
        reply.Recipient = activity.From;
        reply.Type = "message";
        reply.Attachments = new List<Attachment>();
        reply.Attachments.Add(new Attachment()
        {
            ContentUrl = "https://upload.wikimedia.org/wikipedia/en/a/a6/Bender_Rodriguez.png",
            ContentType = "image/png",
            Name = "Bender_Rodriguez.png"
        });

        await connector.Conversations.ReplyToActivityAsync(reply);
        //var reply = await connector.Conversations.SendToConversationAsync(replyToConversation);
        return new HttpResponseMessage(System.Net.HttpStatusCode.Accepted);
    }

-基肖尔