3
我已經發布了我的機器人微軟團隊。現在我想包括一個功能,其中,用戶可以上傳文件作爲附件&機器人將上傳到blob存儲,如何在bot框架中處理這個?有沒有辦法在bot框架中接受文件作爲附件?
我已經發布了我的機器人微軟團隊。現在我想包括一個功能,其中,用戶可以上傳文件作爲附件&機器人將上傳到blob存儲,如何在bot框架中處理這個?有沒有辦法在bot框架中接受文件作爲附件?
用戶發送的附件將以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);
}
謝謝。我正在尋找相同的。 – Akshay
我想要求用戶發送文件作爲附件。這個對話將在Formflow中進行。你能幫助我嗎? – Akshay
Akshay,檢查這個http://stackoverflow.com/questions/41853523/microsoft-bot-receive-attachments-from-user-using-formflow/41873343#41873343 –