如何从bot框架C#读取附件内容? [英] How to read attachment content from bot framework C#?

查看:31
本文介绍了如何从bot框架C#读取附件内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个机器人,并希望用户向我发送一个附件,我想阅读该附件并将其转换为对象.

I am writing a bot and expecting the user to send me an attachment, which I want to read and translate into objects.

到目前为止,我有以下代码:

I have the following code so far:

if (message.Attachments != null && message.Attachments.Any())
{
    var attachment = message.Attachments.First();
    using (HttpClient httpClient = new HttpClient())
    {
        if ((message.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) || message.ChannelId.Equals("msteams", 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; // this is populated correctly

        if(attachment.Name.ToLower().Equals("opportunity.xlsx"))
        {
            var temp = attachment.Content; // This Content is always null, even though everything else is populated.
        }
    }
}

谁能建议我如何阅读附件 xlsx 内容?

Anyone can suggest how can I read the attachment xlsx content please?

谢谢

推荐答案

附件在 Content 属性中不可用.您首先需要使用 ContentUrl 下载附件,然后使用下载文件后的响应消息执行您想要的任何操作.

The attachment is not available in the Content property. You first need to download the attachment using the ContentUrl and then perform whatever you want, using the response message after downloading the file.

查看接收附件 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 & MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
            if ((message.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) || message.ChannelId.Equals("msteams", 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框架C#读取附件内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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