【问题标题】:How to read attachment content from bot framework C#?如何从机器人框架 C# 中读取附件内容?
【发布时间】:2018-01-04 09:46:00
【问题描述】:

我正在编写一个机器人,并希望用户向我发送一个附件,我想阅读该附件并将其翻译成对象。

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

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 内容?

谢谢

【问题讨论】:

    标签: c# excel bots attachment botframework


    【解决方案1】:

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

    查看Receive-Attachments 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);
    } 
    

    【讨论】:

    • 抱歉,这与我上面的代码 sn-p 有何不同?
    • 你正在做的附件。内容是错误的。由于您正在下载附件,因此您应该使用 responseMessage.Content 来访问该文件
    【解决方案2】:

    如果这就是您的意思,您将不得不使用其他一些 dll 来读取 excel 数据。你的意思是在上传后读取excel文件的内容然后你可以使用https://github.com/ExcelDataReader/ExcelDataReader

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多