问题
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