botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

[Teams] Received attachment has a NULL ContentUrl

Open mveroukis opened this issue 9 years ago • 5 comments

Bot Info

  • App ID: 14e8d9d8-227e-4b70-8948-78b4e4c8a55a
  • SDK Platform: .NET
  • SDK Version: 3.11.0
  • Active Channels: MS Teams
  • Deployment Environment: Azure App Service

Issue Description

Attachments on MS Teams have a NULL ContentURL.

Using VS2017 Enterprise I was able to attach a debugger to my bot and when I attach a file (PDF, images, etc) what I noticed is that the Attachment has a NULL ContentUrl, Name, ThumbnailUrl and an empty Properties. However, the ContentType is "text/html" and the Content is some html which contains the name of my bot. It seems to me that the attachment is not the file I attached to the channel but the mention of the bot itself (and no, there are no other attachments associated with the message, just the one).

Interestingly, I can still retrieve the JWT however without a ContentUrl it's not particularly useful. Am I doing something wrong or is this a bug with Teams/Bot Framework?

Also, the attached PDF does appear under the Files tab in my Teams channel. So the file is uploaded and does exist on Teams.

I should also mention that when I use the emulator, it seems to work as expected: The ContentUrl is valid and I can download the file no problem. Also, Slack seems to work just fine.

Code Example

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())
                {
                    await HandleJwtIfRequiredAsync(message, (token) => {
                        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                    });


                    if (attachment.ContentUrl != null)
                    {
                        var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);

                        if (responseMessage.IsSuccessStatusCode)
                        {
                            var contentLenghtBytes = responseMessage.Content.Headers.ContentLength;

                            await context.PostAsync($"Attachment {attachment.Name} of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received.");

                            Stream attachmentStream = await responseMessage.Content.ReadAsStreamAsync();
                            attachmentStream.Position = 0;

                            var botAttachment = new BotAttachment()
                            {
                                Name = attachment.Name,
                                ContentStream = attachmentStream
                            };

                            context.Done(botAttachment);
                        }
                        else
                        {
                            await context.PostAsync($"Something went wrong with your upload.\r\n{responseMessage.ReasonPhrase}\r\n\r\nPlease try again.");
                            context.Wait(this.MessageReceivedAsync);
                        }
                    }
                    else
                    {
                        // This really should never happen, however, Teams seems to screw this up and sends us an attachment without a URL.
                        await context.PostAsync($"I'm sorry Dave, I'm afraid I can't do that");
                        context.Wait(this.MessageReceivedAsync);
                    }
                }
            }
            else
            {
                await context.PostAsync("No attachment was sent to me. Please, try again sending a new message including an attachment.");
                context.Wait(this.MessageReceivedAsync);
            }
        }

        private async Task HandleJwtIfRequiredAsync(IMessageActivity message, Action<String>jwtHandler)
        {
            if (CheckRequiresJwt(message) && jwtHandler != null)
            {
                var jwt = await GetJwtFromRequestAsync();

                jwtHandler?.Invoke(jwt);
            }
        }


        private async Task<string> GetJwtFromRequestAsync()
        {
            var token = await new MicrosoftAppCredentials().GetTokenAsync();

            return token;
        }


        private bool CheckRequiresJwt(IMessageActivity message)
        {
            return message.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) || message.ChannelId.Equals("msteams", StringComparison.InvariantCultureIgnoreCase);
        }

Reproduction Steps

  1. Create bot on Teams that accepts a file attachment
  2. Attach the file to a Team channel (while mentioning the bot, otherwise it will never get it)

Expected Behavior

The message's attachment object should contain a valid URL to the uploaded file. Also the Name should contain the file name and the ContentType should reflect the content type of the file. Content should probably be null or empty.

Actual Results

The message's attachment has a NULL ContentUrl, Name, ThumbnailUrl and an empty Properties. However, the ContentType is "text/html" and the Content is some html which contains the name of my bot.

mveroukis avatar Oct 31 '17 18:10 mveroukis

+1. I am facing this issue as well on node.js. On bot emulator the attachments returns url and content type, but when integrated with teams, the content type is returned as "text/html" with the content as the name of the bot.

sudheer-ranga avatar Nov 07 '17 18:11 sudheer-ranga

@mveroukis, @sudheer-ranga - Unfortunately this is not supported in Microsoft Teams yet. This is already part of our backlog. Alternatively, when user copy pastes (not upload via attachment) an image in chat compose box, it is sent as attachment. Please note that only image files can be pasted into the compose box.

Wajeed-msft avatar Nov 14 '17 04:11 Wajeed-msft

☹

mveroukis avatar Nov 15 '17 15:11 mveroukis

Was trying to convince my boss to use the Microsoft teams, encountered the same issue here.

HainanZhao avatar Apr 05 '18 04:04 HainanZhao

Teams Channel now can accept Attachment in BOT Applications. You can accept the file using Jwt and also you can download the file using the FileDownloadInfo Type ( available in Teams.Models dll)

nareshpolu avatar Jul 17 '18 22:07 nareshpolu