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



我想下载文档/图像(文档/图像在互联网上,我给出了它的路径)。但是它不起作用。。如果我只是评论附件部分,我怎么能从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);
    }
    }

在这行代码之后,您的代码出现了错误

Activity reply = activity.CreateReply("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);
    }

-Kishore

您可能会在Attachment上得到一个null引用异常。你检查过异常情况吗?

尝试:

reply.Attachments=新列表<附件>();

最新更新