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

时间:2016-07-28 18:09:12

标签: .net asp.net-web-api bots botframework

我想下载文档/图像(文档/图像在互联网上,我正在给它的路径)。但它没有工作..如果我只是评论附件部分,我可以从BOT得到“嗨”。

让我们拥有像这样的控制器

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

    }

2 个答案:

答案 0 :(得分:2)

在这行代码

之后,你的代码出错了
Activity reply = activity.CreateReply("Hi");

您要将附件添加到活动对象,而不是回复。您收到“”,因为您没有将附件添加到回复引用。

我已经修改了你的代码,它在Bot框架模拟器上成功运行并显示了图像。

<强>代码

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

-Kishore

答案 1 :(得分:1)

您可能会在附件上获得空引用异常。你检查过例外吗?

尝试:

  

reply.Attachments = new List&lt;附件&gt;();