botframework-csharp-graph-explorer icon indicating copy to clipboard operation
botframework-csharp-graph-explorer copied to clipboard

Can you implement PhotoDialog to show how user photo can be shown in Bot using Graph API?

Open Atul-Moghe opened this issue 8 years ago • 1 comments

I've created a bot but i am not able to show photo of the user using Graph API. I was wondering if i could see it implemented in this demo project but it's not yet implemented.

Atul-Moghe avatar Aug 08 '17 11:08 Atul-Moghe

Hi Atul-Moghe, I implemented it if you still need it. In GraphDialog.cs change:

case OperationType.Photo:
                        await opContext.Forward(new PhotoDialog(), OperationComplete, true, CancellationToken.None);                     
                        break;

and in PhotoDialog.cs

        public async Task StartAsync(IDialogContext context)
        {
            var entity = context.ConversationData.GetDialogEntity();
            var bytes = await GetStreamWithAuthAsync(context);
            var pic = "data:image/png;base64," + Convert.ToBase64String(bytes);
            var message = context.MakeMessage();

            var attachment = GetHeroCard(pic, entity.text);
            message.Attachments.Add(attachment);

            await context.PostAsync(message);
            context.Done(message);
            //
        }

private async Task<byte[]> GetStreamWithAuthAsync(IDialogContext context)
        {
            var entity = context.ConversationData.GetDialogEntity();
            HttpClient client = new HttpClient();
            var token = await context.GetAccessToken();
            var results = await client.MSGraphGETByte(token, String.Format("https://graph.microsoft.com/v1.0/users/{0}/photo/$value", entity.id));

            return results;
        }

        private static Attachment GetHeroCard(string url, string text)
        {
            var subtitle = ""; // add whatever you want

            var heroCard = new HeroCard
            {
                Title = text,
                Subtitle = subtitle,
                Text = "This goes directly from Graph API",
                Images = new List<CardImage> { new CardImage(url) }
            };

            return heroCard.ToAttachment();
        }

mateuszorlowski avatar Dec 08 '17 07:12 mateuszorlowski