0
votes

My bot prompts the user for an attachment in a dialog and is suppose to receive an image from the user and save the content name, url and type of that file to variables. However, the file I am receiving is not the file the user sent. Instead its retrieving a file named 'blob' (no extension) from this link:

https://webchat.botframework.com/attachments/GkLhiqJcvH019mP0iGBKvo/0000052/0/blob?t=X5ICiUrhFas.dAA.RwBrAEwAaABpAHEASgBjAHYASAAwADEAOQBtAFAAMABpAEcAQgBLAHYAbwAtADAAMAAwADAAMAA1ADIA.mcEiHxuH0gE.VMcp6Yduqgc.4xT1ZTOvCX-B7A0nLto6eZNFrFi-0xSzGk5AKmA-EPE

That file contains:

{"type":"message","from":{"id":"9n0I1ZqSrLF","name":"You"},"locale":"en-US","timestamp":"2017-02-14T21:12:32.074Z","channelData":{"clientActivityId":"1487106689866.9060198022610071.8"}}

Here is the code for the attachment prompt. This prompt is within a dialog:

private async Task getAttach(IDialogContext context, IAwaitable<IEnumerable<Attachment>> result)
    {
        IEnumerable<Attachment> list = await result;
        Attachment attachment = list.FirstOrDefault();

        string filename = attachment.Name;
        string url = attachment.ContentUrl;
        string contentType = attachment.ContentType;


        //Set attachmentFileNames
        if (attachmentFileNames == null)
        {
            attachmentFileNames = "";
        }

        attachmentFileNames += contentType;
        attachmentFileNames += ",";
        numberOfFiles++;

        //Set attachmentFileURLs
        if (attachmentFileURLs == null)
        {
            attachmentFileURLs = "";
        }

        attachmentFileURLs += url;
        attachmentFileURLs += ",";

        attachmentHasBeenAdded = true;
        await FirstMessageReceivedAsync(context, result);

    }

Here is how I am handling attachments in the message controller. This is within the Post Task:

ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

            if (activity.Attachments != null && activity.Attachments.Any())
            {
                hasaAttachment = true;
                var attachment = activity.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 ((activity.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) || activity.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;

                    Activity reply = activity.CreateReply($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received. URL: {attachment.ContentUrl}");

                    await connector.Conversations.ReplyToActivityAsync(reply);
                }
            }

I am following this example: Receive Attachment Bot Sample

In the emulator it works correctly but the published version on azure does not. The code is very similar to the example so I don't see why the bot is not detecting the user's files. How do I receive the correct file (the file the user replied with)?

1

1 Answers

0
votes

The attachments will be in elements 1 onwards. E.g.

Attachment attachment = list.ElementAt(1);

The webchat (and maybe other platforms) has a weird quirk where it adds a blob file at the start of IEnumerable 'result' when prompting and getting attachments from the user.

private async Task getAttach(IDialogContext context, IAwaitable<IEnumerable<Attachment>> result)
{...}