我想下载文档/图像(文档/图像位于互联网上,我给出了它的路径)。但它不起作用。但是,如果我只是评论附件部分,我就能从 BOT 那里得到“嗨”。
让 Controller 像这样
[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);
}
-基肖尔
关于.net - 使用 Microsoft bot 框架下载文件(pdf/图像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38643808/