【问题标题】:Is there a way to accept file as an attachment in bot framework?有没有办法在机器人框架中接受文件作为附件?
【发布时间】:2016-11-22 10:42:43
【问题描述】:

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

【问题讨论】:

    标签: c# botframework


    【解决方案1】:

    用户发送的附件最终会出现在 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);
    }
    

    【讨论】:

    • 谢谢。我也在找一样的。
    • 我想要求用户将文件作为附件发送。此对话将在 formflow 中进行。你能帮我解决这个问题吗?
    • 感谢 Ezequiel Jadib。我会通过它。 :)
    猜你喜欢
    • 2020-09-23
    • 2014-05-16
    • 2014-02-10
    • 2021-03-26
    • 2017-05-07
    • 2022-01-24
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多