在bot框架中是否可以接受文件作为附件? [英] Is there a way to accept file as an attachment in bot framework?

查看:79
本文介绍了在bot框架中是否可以接受文件作为附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Microsoft团队中发布了我的机器人.现在,我想包括一个功能,用户可以在其中上传文件作为附件& bot会将其上传到blob存储中,如何在bot框架中处理该问题?

I have published my bot on Microsoft teams. Now I want to include a functionality in which, a user can upload a file as an attachment & bot will upload it on blob storage, How to handle this in bot framework?

推荐答案

用户发送的附件将最终位于IMessageActivity的Attachments集合中.在那里,您将找到用户发送的附件的URL.

The attachments sent by the user will end up in the Attachments collection of the IMessageActivity. There you will find the URL of the attachment the user sent.

然后,您将必须下载附件并添加逻辑以将其上传到Blob存储或您要使用的任何其他存储.

Then, you will have to download the attachment and add your logic to upload it to Blob storage or any other storage you would like to use.

此处是一个C#示例,显示了如何访问并下载用户发送的附件.添加了以下代码供您参考:

Here is a C# example showing how to access and download the attachments sent by the user. Added the code below for your reference:

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

这篇关于在bot框架中是否可以接受文件作为附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆