简体   繁体   中英

Cannot add attachment to message in MS bot framework: object reference not set to an instance of an object

I am writing a facebook bot using MS Bot Framework. I am able to send text replies successfully, but I can't seem to send a picture.

Microsoft has the following example here - http://docs.botframework.com/en-us/csharp/builder/sdkreference/attachments.html

replyMessage.Attachments.Add(new Attachment()
{
    ContentUrl = "https://upload.wikimedia.org/wikipedia/en/a/a6/Bender_Rodriguez.png",
    ContentType = "image/png",
    Name = "Bender_Rodriguez.png"      
});

That code doesn't work for me - it throws this error:

Object reference not set to an instance of an object.

Bot_Application1.MessagesController.d__0.MoveNext()
in C:\\Users------\\Dropbox\\code\\Bot Application1\\Bot
Application1\\Controllers\\MessagesController.cs:line 92

Line 92 is where I call the .Attachments.Add() method.

I've tried to modify the code to make sure it has no null properties inside, so I added non-null Content and ThumbnailUrl, but this also doesn't work (with or without these two parameters). It's exactly the same error as above when calling the Add() method.

Activity reply3 = activity.CreateReply("blah");

Attachment pic = new Attachment();
pic.ContentUrl = "https://upload.wikimedia.org/wikipedia/en/a/a6/Bender_Rodriguez.png";
pic.ContentType = "image/png";
pic.Name = "Bender_Rodriguez.png";
pic.Content = "Test";
pic.ThumbnailUrl = pic.ContentUrl;

reply3.Attachments.Add(pic);

await connector.Conversations.ReplyToActivityAsync(reply3);

What am I doing wrong?

Turns out you need to add a line that was not mentioned in the documentation:

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

After initializing the Attachments it works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM