I am using Microsoft Bot Builder .NET SDK for receiving an attachment through Emulator for now

删除回忆录丶 提交于 2019-12-24 11:12:22

问题


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.

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.

Code:

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

Value of attachment in var message:

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


回答1:


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


来源:https://stackoverflow.com/questions/46470208/i-am-using-microsoft-bot-builder-net-sdk-for-receiving-an-attachment-through-em

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!