我现在使用Microsoft Bot Builder .NET SDK通过Emulator接收附件 [英] I am using Microsoft Bot Builder .NET SDK for receiving an attachment through Emulator for now

查看:76
本文介绍了我现在使用Microsoft Bot Builder .NET SDK通过Emulator接收附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目使用Microsoft的Bot builder SDK for .NET,我想从Emulator接收附件并继续进行流程.

I am using Bot builder SDK for .NET by Microsoft for my project and i want to receive an attachment from Emulator and proceed with the flow.

当我从模拟器附加任何文件时,我遇到问题,但我没有收到文件上传的内容,并且Content URL也是一些我无法继续运行的localhost URL.

I am getting issue when i attach any file from emulator i am not getting content of the file uploaded and Content URL is also some localhost URL which i am not able to proceed.

代码:

public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
    var message = await result;
    var reply = context.MakeMessage();
}

var邮件中附件的值:

Value of attachment in var message:

我的问题是如何获取我上传的文件的内容和内容URL.

My question is how to get Content and Content URL of file which i have uploaded.

推荐答案

您不会获得内容. Yoy需要使用ContentUrl下载它.看看 core-ReceiveAttachment 示例了解如何去做.

You won't get the content. Yoy need to download it by using the ContentUrl. Take a look at the core-ReceiveAttachment sample to understand how to do it.

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

这篇关于我现在使用Microsoft Bot Builder .NET SDK通过Emulator接收附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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