Example #1
0
 /// <summary>
 /// Gets an image from the Flickr API for the query supplied.
 /// Returns a default RecipyBot image if none found from Flickr.
 /// </summary>
 public static string GetImage(string query)
 {
     try
     {
         string            apiCall       = BotConstants.BotApiSettings.FlickrImageApi + "&method=flickr.photos.search&text=" + HttpContext.Current.Server.UrlEncode(query) + "&format=json&nojsoncallback=1";
         FlickrDataModel   webResponse   = WebApiConnectorService.GenericGetRequest <FlickrDataModel>(apiCall);
         IEnumerable <int> randomNumbers = MiscService.GiveXFromYNumbers(1, webResponse.photos.photo.Count);
         string            _imageUrl     = string.Empty;
         foreach (var value in randomNumbers)
         {
             _imageUrl = "https://farm" + webResponse.photos.photo.ElementAt(value).farm + ".staticflickr.com/" + webResponse.photos.photo.ElementAt(value).server + "/" + webResponse.photos.photo.ElementAt(value).id + "_" + webResponse.photos.photo.ElementAt(value).secret + ".jpg";
         }
         return(_imageUrl);
     }
     catch (NullReferenceException nex)
     {
         System.Diagnostics.Trace.TraceError("Flickr GetImage " + nex.Message);
         return("https://raw.githubusercontent.com/ClydeDz/RecipyBot/master/Docs/recipybot/2.png");
     }
     catch (Exception e)
     {
         System.Diagnostics.Trace.TraceError("Flickr GetImage " + e.Message);
         return("https://raw.githubusercontent.com/ClydeDz/RecipyBot/master/Docs/recipybot/2.png");
     }
 }
Example #2
0
 /// <summary>
 /// Gets an image from the Qwant API for the query supplied.
 /// Returns a default RecipyBot image if none found from Qwant.
 /// </summary>
 public static string GetImage(string query)
 {
     try
     {
         QwantDataModel    webResponse   = WebApiConnectorService.GenericGetRequest <QwantDataModel>(BotConstants.BotApiSettings.QwantImageApi + "" + query);
         IEnumerable <int> randomNumbers = MiscService.GiveXFromYNumbers(1, webResponse.data.result.items.Count());
         string            _imageUrl     = string.Empty;
         foreach (var value in randomNumbers)
         {
             var recipe = webResponse.data.result.items.ElementAt(value);
             _imageUrl = recipe.media.ToString();
         }
         return(_imageUrl);
     }
     catch (NullReferenceException nex)
     {
         System.Diagnostics.Trace.TraceError("QwantService GetImage " + nex.Message);
         return("https://github.com/ClydeDz/RecipyBot/blob/master/Docs/recipybot/3.png");
     }
     catch (Exception e)
     {
         System.Diagnostics.Trace.TraceError("QwantService GetImage " + e.Message);
         return("https://github.com/ClydeDz/RecipyBot/blob/master/Docs/recipybot/3.png");
     }
 }
        public static Activity GetRecipeFor(Activity message, string dish, string defaultResponse)
        {
            BotService.SendATextResponse(message, defaultResponse);
            RecipePuppyDataModel webResponse   = WebApiConnectorService.GenericGetRequest <RecipePuppyDataModel>(BotConstants.BotApiSettings.RecipePuppyWebApiUrlWithQuery + "" + dish);
            IEnumerable <int>    randomNumbers = MiscService.GiveXFromYNumbers(BotConstants.OtherConstants.MaxOptionsGives, webResponse.results.Count());

            return(GenerateRecipeMessage(message, webResponse, randomNumbers, defaultResponse));
        }
        public static Activity GetTopNRecipes(Activity message, int n, string defaultResponse)
        {
            if (!string.IsNullOrEmpty(defaultResponse))
            {
                BotService.SendATextResponse(message, defaultResponse);
            }

            RecipePuppyDataModel webResponse   = WebApiConnectorService.GenericGetRequest <RecipePuppyDataModel>(BotConstants.BotApiSettings.RecipePuppyWebApiUrl);
            IEnumerable <int>    randomNumbers = MiscService.GiveXFromYNumbers(n, webResponse.results.Count());

            return(GenerateRecipeMessage(message, webResponse, randomNumbers, defaultResponse));
        }
        public static Activity GetRecipeWith(Activity message, string[] ingredients, string defaultResponse)
        {
            if (!string.IsNullOrEmpty(defaultResponse))
            {
                BotService.SendATextResponse(message, defaultResponse);
            }

            string listOfIngredients           = string.Join(",", ingredients.Select(item => item).ToArray());
            RecipePuppyDataModel webResponse   = WebApiConnectorService.GenericGetRequest <RecipePuppyDataModel>(BotConstants.BotApiSettings.RecipePuppyWebApiUrlWithIngredients + "" + listOfIngredients);
            IEnumerable <int>    randomNumbers = MiscService.GiveXFromYNumbers(BotConstants.OtherConstants.MaxOptionsGives, webResponse.results.Count());

            return(GenerateRecipeMessage(message, webResponse, randomNumbers, defaultResponse));
        }
        private static Activity GenerateRecipeMessage(Activity message, RecipePuppyDataModel recipes, IEnumerable <int> randomNumbers, string defaultResponse)
        {
            try
            {
                Activity replyToConversation = message.CreateReply();
                replyToConversation.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                replyToConversation.Attachments      = new List <Attachment>();
                Dictionary <string, string> cardContentList = new Dictionary <string, string>();

                foreach (var value in randomNumbers)
                {
                    var    recipe      = recipes.results.ElementAt(value);
                    string recipeImage = FlickrService.GetImage(recipe.title);

                    // Add basic card content details
                    cardContentList.Add(
                        recipe.title,
                        recipe.href
                        );

                    // Add the card image and button element to a thumbnail of the response
                    replyToConversation.Attachments.Add(
                        new HeroCard()
                    {
                        Title    = recipe.title,
                        Subtitle = MiscService.GetRandomTagline(),
                        Text     = MiscService.GetRandomIngredientsPrefix() + recipe.ingredients,
                        Images   = new List <CardImage> {
                            new CardImage(recipeImage)
                        },                                                               //recipe.thumbnail
                        Buttons = new List <CardAction> {
                            new CardAction(ActionTypes.OpenUrl, BotConstants.PreDefinedActions.VisitRecipeActionButton, value: recipe.href)
                        }
                    }.ToAttachment()
                        );
                }

                return(replyToConversation);
            }
            catch (NullReferenceException nex)
            {
                System.Diagnostics.Trace.TraceError("GenerateRecipeMessage " + nex.Message);
                return(message.CreateReply("Apologies, we couldn't find any recipes at this moment."));
            }
            catch (Exception e)
            {
                System.Diagnostics.Trace.TraceError("GenerateRecipeMessage " + e.Message);
                return(message.CreateReply("Apologies, we couldn't find any GIF at this moment."));
            }
        }
Example #7
0
 /// <summary>
 /// Returns the username greeting string.
 /// If an empty or default username is supplied, it will return an empty string.
 /// If an actual username is supplied, it returns the username+greetings.
 /// </summary>
 public static string GetUsernameValue(string username)
 {
     return(MiscService.IsUserNameDefaultOrBlank(username) ? "" :  string.Format("Hi {0}! ", username.Split(' ')[0]));
 }
Example #8
0
 /// <summary>
 /// Returns true if user message is a pre-defined feedback request.
 /// </summary>
 public static bool IsFeedbackRequest(string userMessage)
 {
     return(MiscService.CompareTwoStrings(userMessage, BotConstants.PreDefinedActions.Feedback));
 }
Example #9
0
 /// <summary>
 /// Returns true if user message is a pre-defined get started request.
 /// </summary>
 public static bool IsHelpOrGetStartedRequest(string userMessage)
 {
     return(MiscService.CompareTwoStrings(userMessage, BotConstants.PreDefinedActions.GetStarted) || MiscService.CompareTwoStrings(userMessage, BotConstants.PreDefinedActions.Help));
 }
Example #10
0
 /// <summary>
 /// Returns true if user message is a pre-defined top N recipes request.
 /// </summary>
 public static bool IsTopNRecipesRequest(string userMessage)
 {
     return(MiscService.CompareTwoStrings(userMessage, BotConstants.PreDefinedActions.TopNRecipes));
 }
Example #11
0
 /// <summary>
 /// Returns true if user message is a pre-defined random recipe request.
 /// </summary>
 public static bool IsRandomRecipeRequest(string userMessage)
 {
     return(MiscService.CompareTwoStrings(userMessage, BotConstants.PreDefinedActions.NewRandomRecipe));
 }
Example #12
0
        /// <summary>
        /// Handles other non-pre-defined messages in the bot.
        /// These messages are passed to the DialogFlow API to process.
        /// </summary>
        public static Activity HandleNaturalInput(Activity message)
        {
            try
            {
                var response = SendRequestToApiAi(message);
                switch (response.Result.Action)
                {
                // Returns the recipe for an item searched
                case BotConstants.ApiAiActionConstants.RecipyCookFor:
                    var entityRecipyCookFor = MiscService.GetFoodEntities(response.Result.Parameters, BotConstants.FoodEntitiesEnum.Recipe);
                    if (string.IsNullOrEmpty(entityRecipyCookFor))
                    {
                        return(message.CreateReply(response.Result.Fulfillment.Speech));
                    }
                    return(RecipePuppyService.GetRecipeFor(message, entityRecipyCookFor, response.Result.Fulfillment.Speech));

                // Returns the recipe of the day
                case BotConstants.ApiAiActionConstants.RecipyOfTheDay:
                    return(RecipePuppyService.GetRandomRecipe(message, response.Result.Fulfillment.Speech));

                // Returns a random recipe
                case BotConstants.ApiAiActionConstants.RecipyRandom:
                    return(RecipePuppyService.GetRandomRecipe(message, response.Result.Fulfillment.Speech));

                // Return a recipe for the ingredients queried
                case BotConstants.ApiAiActionConstants.RecipyCookWith:
                    var entityRecipyCookWith = MiscService.GetFoodEntities(response.Result.Parameters, BotConstants.FoodEntitiesEnum.FoodItem);
                    if (string.IsNullOrEmpty(entityRecipyCookWith))
                    {
                        return(message.CreateReply(response.Result.Fulfillment.Speech));
                    }
                    return(RecipePuppyService.GetRecipeWith(message, JsonConvert.DeserializeObject <string[]>(entityRecipyCookWith), response.Result.Fulfillment.Speech));

                // Returns a GIF recipe
                case BotConstants.ApiAiActionConstants.RecipyShowGif:
                    return(RecipePuppyService.GetRecipeGif(message, response.Result.Fulfillment.Speech));

                // Returns top N recipes where N is a number
                case BotConstants.ApiAiActionConstants.RecipyTopN:
                    return(RecipePuppyService.GetTopNRecipes(message, MiscService.GetNumericEntity(response.Result.Parameters), response.Result.Fulfillment.Speech));

                // Returns a generic about response
                case BotConstants.ApiAiActionConstants.GeneralAbout:
                    return(BotHelperService.AboutResponse(message, response.Result.Fulfillment.Speech));

                // Returns a generic help/get started response
                case BotConstants.ApiAiActionConstants.GeneralGetStarted:
                    return(BotHelperService.GetStartedResponse(message, response.Result.Fulfillment.Speech));

                // Returns a generic help/get started response
                case BotConstants.ApiAiActionConstants.GeneralHelp:
                    return(BotHelperService.GetStartedResponse(message, response.Result.Fulfillment.Speech));

                // Returns a generic version response
                case BotConstants.ApiAiActionConstants.GeneralVersion:
                    return(BotHelperService.VersionResponse(message));

                // Returns a generic feedback response
                case BotConstants.ApiAiActionConstants.GeneralFeedback:
                    return(BotHelperService.FeedbackResponse(message, response.Result.Fulfillment.Speech));

                // Returns a response from API.AI for general smalltalk
                case BotConstants.ApiAiActionConstants.GeneralSmallTalk:
                    return(message.CreateReply(response.Result.Fulfillment.Speech));

                // Returns a response from API.AI directly
                default:
                    return(message.CreateReply(response.Result.Fulfillment.Speech));
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Trace.TraceError("Class ApiAiService | HandleNaturalInput() | Oops, something happened " + e.Message);
                System.Diagnostics.Trace.TraceError(e.StackTrace);
                return(message.CreateReply("Well, that's unexpected. Apologies, but looks like one of our systems is having some trouble digesting that request. Maybe try a differnt request or try again later?"));
            }
        }
Example #13
0
        public static Activity GetRecipeGif(Activity message, string defaultResponse)
        {
            try
            {
                if (!string.IsNullOrEmpty(defaultResponse))
                {
                    BotService.SendATextResponse(message, defaultResponse);
                }

                GifRecipesDataModel webResponse   = WebApiConnectorService.GenericGetRequest <GifRecipesDataModel>(BotConstants.BotApiSettings.GifRecipes);
                IEnumerable <int>   randomNumbers = MiscService.GiveXFromYNumbers(1, webResponse.data.children.Where(p => p.data.domain == BotConstants.OtherConstants.GifImgurKeyword).Count());

                Activity replyToConversation = message.CreateReply("Here's your GIF recipe");
                replyToConversation.Attachments      = new List <Attachment>();
                replyToConversation.AttachmentLayout = AttachmentLayoutTypes.List;
                //replyToConversation.Text = "kjfjefkwek";

                foreach (var value in randomNumbers)
                {
                    var thisData = webResponse.data.children.Where(k => k.data.domain == BotConstants.OtherConstants.GifImgurKeyword).ElementAt(value);

                    BotService.SendATextResponse(message, MiscService.MakeGif(thisData.data.url));

                    replyToConversation.Attachments.Add(
                        new AnimationCard
                    {
                        Title     = thisData.data.title,
                        Subtitle  = "by @" + thisData.data.author + ". Perfect for " + thisData.data.link_flair_text.ToString().ToLower().Trim(),
                        Autostart = true,
                        Shareable = true,
                        Image     = new ThumbnailUrl
                        {
                            Url = thisData.data.thumbnail
                        },
                        Media = new List <MediaUrl>
                        {
                            new MediaUrl()
                            {
                                Url = MiscService.MakeGif(thisData.data.url), Profile = "image/gif"
                            }
                        },
                        Buttons = new List <CardAction>
                        {
                            new CardAction()
                            {
                                Title = "Learn More",
                                Type  = ActionTypes.OpenUrl,
                                Value = "https://peach.blender.org/"
                            }
                        }
                    }.ToAttachment()
                        );
                }

                return(replyToConversation);
            }
            catch (NullReferenceException nex)
            {
                System.Diagnostics.Trace.TraceError("GIF Recipes " + nex.Message);
                return(message.CreateReply("Apologies, we couldn't find any GIF recipes at this moment."));
            }
            catch (Exception e)
            {
                System.Diagnostics.Trace.TraceError("GIF Recipes " + e.Message);
                return(message.CreateReply("Apologies, we couldn't find any GIF recipes at this moment."));
            }
        }