2016-07-28 65 views
2

我想下載文檔/圖像(文檔/圖像在互聯網上,我正在給它的路徑)。但它不工作..如果我只是評論附件部分,我可以從BOT獲得「Hi」。從使用Microsoft bot框架下載文件(pdf /圖像)

讓我們有這樣

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

    } 

回答

2

您此行代碼

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

要添加附件的活動對象,而不是回覆後沒有在你的代碼錯誤。您收到「」作爲迴應,因爲您沒有將附件添加到回覆參考。

我已經修改了你的代碼,它正在工作併成功地在Bot Framework模擬器上顯示圖像。

代碼

 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

+0

感謝它工作正常... – Sharad

1

你很可能得到的附件的空引用異常控制器。你檢查過異常嗎?

嘗試:

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

+0

沒有遺憾,它不工作。 – Sharad

+0

你有沒有例外? – Lars

+0

我的減價存在一個錯誤。新行中缺少「附件」。 – Lars