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 –