1
votes

After authenticating against Azure AD, my bot is able to retrieve the current user photo from Microsoft Graph through the following code, which adds the photo to the response message as an attachment:

            HttpClient client2 = new HttpClient();
            client2.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");
            var response2 = await client2.GetByteArrayAsync("https://graph.microsoft.com/v1.0/me/photo/$value");

            Activity replyToConversation = (Activity)context.MakeMessage();

            replyToConversation.Type = "message";
            replyToConversation.Attachments.Add(new Attachment()
            {
                Content = response2,
                ContentType = "image/jpeg"
            });

            await context.PostAsync(replyToConversation);
            context.Wait(MessageReceivedAsync);

Everything works as expected in the Web Chat channel, but for some reason the picture is not displayed in the Microsoft Teams channel and the bot answers the default error message: "Sorry, my bot code is having an issue."

Please, any ideas?

1

1 Answers

0
votes

The very first thing I would check if the Attachments array is not null. Usually it is and thus you might be getting a null reference exception. So add the following before adding your attachment to the list:

replyToConversation.Attachments = new List<Attachment>();

If after that you are still not seeing the image, you could try building an "url" converting the bytes to a base64 string representation and set that as the ContentUrl instead of setting the byte array as the attachment's content.

string url = "data:image/jpeg;base64," + Convert.ToBase64String(response2)

var replyToConversation = context.MakeMessage();
replyToConversation.Attachments = new List<Attachment>();
replyToConversation.Attachments.Add(new Attachment()
{
     ContentUrl = url,
     ContentType = "image/jpeg"
});