2016-11-21 54 views

回答

5

用戶發送的附件將以IMessageActivity的Attachments集合爲結尾。在那裏你會找到用戶發送的附件的URL。

然後,您將不得不下載附件並添加您的邏輯以將其上傳到Blob存儲或您想要使用的任何其他存儲。

Here是一個C#示例,顯示如何訪問和下載用戶發送的附件。添加以下代碼供您參考:

public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument) 
{ 
    var message = await argument; 

    if (message.Attachments != null && message.Attachments.Any()) 
    { 
     var attachment = message.Attachments.First(); 
     using (HttpClient httpClient = new HttpClient()) 
     { 
      // Skype attachment URLs are secured by a JwtToken, so we need to pass the token from our bot. 
      if (message.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) && new Uri(attachment.ContentUrl).Host.EndsWith("skype.com")) 
      { 
       var token = await new MicrosoftAppCredentials().GetTokenAsync(); 
       httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); 
      } 

      var responseMessage = await httpClient.GetAsync(attachment.ContentUrl); 

      var contentLenghtBytes = responseMessage.Content.Headers.ContentLength; 

      await context.PostAsync($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received."); 
     } 
    } 
    else 
    { 
     await context.PostAsync("Hi there! I'm a bot created to show you how I can receive message attachments, but no attachment was sent to me. Please, try again sending a new message including an attachment."); 
    } 

    context.Wait(this.MessageReceivedAsync); 
} 
+0

謝謝。我正在尋找相同的。 – Akshay

+0

我想要求用戶發送文件作爲附件。這個對話將在Formflow中進行。你能幫助我嗎? – Akshay

+0

Akshay,檢查這個http://stackoverflow.com/questions/41853523/microsoft-bot-receive-attachments-from-user-using-formflow/41873343#41873343 –

相關問題