Beispiel #1
0
        /// <summary>
        /// Get the User Badges
        /// </summary>
        /// <returns>UserBadges Object</returns>
        public async Task <UserBadges> GetUserBadgesAsync()
        {
            string jsonResponse = await GetFitBitDataAsync(Constants.UserBadges);

            UserBadges userBadges = JsonConvert.DeserializeObject <UserBadges>(jsonResponse);

            return(userBadges);
        }
Beispiel #2
0
        public static Attachment CreateUserBadgesCard(UserBadges userBadges)
        {
            List <Dictionary <string, string> > dictlist = new List <Dictionary <string, string> >();

            string badgeJsonSchema             = File.ReadAllText(@".\Resources\AdaptiveCards\UserBadges\Badge.json");
            string badgeAdaptiveCardJsonSchema = File.ReadAllText(@".\Resources\AdaptiveCards\UserBadges\UserBadgesAdapativeCard.json");

            foreach (var badge in userBadges.badges)
            {
                Dictionary <string, string> dict = new Dictionary <string, string>()
                {
                    { "{badgeImage}", badge.image100px },
                    { "{badgeName}", badge.name },
                    { "{badgeType}", badge.category },
                    { "{earnedOn}", DateTime.ParseExact(badge.dateTime, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture).ToString("dd MMM, yyyy") },
                    { "{earnedMessage}", badge.marketingDescription },
                    { "{timesAchieved}", Convert.ToString(badge.timesAchieved) }
                };
                dictlist.Add(dict);
            }

            StringBuilder builder = new StringBuilder();

            foreach (var dict in dictlist)
            {
                string temp = badgeJsonSchema;
                foreach (var pair in dict)
                {
                    temp = temp.Replace(pair.Key, pair.Value);
                }
                builder.Append(temp);
                builder.Append(",");
            }

            badgeAdaptiveCardJsonSchema = badgeAdaptiveCardJsonSchema.Replace("\"{badges}\"", builder.ToString());

            var adaptiveCardAttachment = new Attachment()
            {
                ContentType = "application/vnd.microsoft.card.adaptive",
                Content     = JsonConvert.DeserializeObject(badgeAdaptiveCardJsonSchema),
            };

            return(adaptiveCardAttachment);
        }
Beispiel #3
0
        private async Task <DialogTurnResult> ProcessStepAsync(WaterfallStepContext step, CancellationToken cancellationToken)
        {
            if (step.Result != null)
            {
                var tokenResponse = step.Result as TokenResponse;

                // If we have the token use the user is authenticated so we may use it to make API calls.
                if (tokenResponse?.Token != null)
                {
                    var    parts   = _accessors.CommandState.GetAsync(step.Context, () => string.Empty, cancellationToken: cancellationToken).Result.Split(' ');
                    string command = parts[0].ToLowerInvariant();

                    if (command == "myprofile")
                    {
                        FitBitApiHelper fitBitApiHelper = new FitBitApiHelper(tokenResponse.Token);
                        UserProfile     userProfile     = await fitBitApiHelper.GetUserProfileAsync();

                        var reply = step.Context.Activity.CreateReply();
                        reply.Attachments = new List <Attachment>()
                        {
                            DialogHelpers.CreateUserProfileAdaptiveCard(userProfile),
                            DialogHelpers.CreateBotFunctionsHeroCard("What would you like to do next? Select one").ToAttachment()
                        };

                        await step.Context.SendActivityAsync(reply, cancellationToken);
                    }
                    else if (command == "mybadges")
                    {
                        FitBitApiHelper fitBitApiHelper = new FitBitApiHelper(tokenResponse.Token);
                        UserBadges      userBadges      = await fitBitApiHelper.GetUserBadgesAsync();

                        var reply = step.Context.Activity.CreateReply();
                        reply.Attachments = new List <Attachment>()
                        {
                            DialogHelpers.CreateUserBadgesCard(userBadges),
                            DialogHelpers.CreateBotFunctionsHeroCard("What would you like to do next? Select one").ToAttachment()
                        };

                        await step.Context.SendActivityAsync(reply, cancellationToken);
                    }
                    else
                    {
                        var reply = step.Context.Activity.CreateReply();
                        reply.Attachments = new List <Attachment>()
                        {
                            DialogHelpers.CreateBotFunctionsHeroCard("This is what I can do for you.").ToAttachment()
                        };

                        await step.Context.SendActivityAsync(reply, cancellationToken);
                    }

                    //else
                    //{
                    //    await step.Context.SendActivityAsync($"Your token is: {tokenResponse.Token}", cancellationToken: cancellationToken);
                    //}
                }
            }
            else
            {
                await step.Context.SendActivityAsync("We couldn't log you in. Please try again later.", cancellationToken : cancellationToken);
            }
            await _accessors.CommandState.DeleteAsync(step.Context, cancellationToken);

            return(await step.EndDialogAsync(cancellationToken : cancellationToken));
        }