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

扶醉桌前 提交于 2020-01-02 17:19:26

问题


I wanted to download document/image ( Document/image is on internet and I am giving path of it). But it ins not working.. How ever if I just comment the attachment part, I am able to get "Hi" from BOT.

Lets have the controller like this

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

    }

回答1:


You did mistake in your code after this line of code

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

You are adding the attachments to the activity object instead of reply. You are getting “Hi” in response because you did not added the attachments to reply reference.

I have modified your code, it’s working and displayed image on Bot Framework Emulator successfully.

Code

        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




回答2:


You are likely getting a null reference exception on the Attachment. Have you checked for exceptions?

Try:

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



来源:https://stackoverflow.com/questions/38643808/downloading-file-pdf-image-from-using-microsoft-bot-framework

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