Exemplo n.º 1
0
 /// <summary>
 /// Handles the incoming message from the user.
 /// Checks if its a standard pre-defined message from the user and handles accordingly.
 /// If not, then it passes the message to the Dialogflow (API.AI) service to decode.
 /// </summary>
 public static Activity HandleBotRequest(Activity userMessage)
 {
     try
     {
         if (BotHelperService.IsGifRecipyRequest(userMessage.Text))
         {
             // Sends a GIF recipe
             return(RecipePuppyService.GetRecipeGif(userMessage, string.Empty));
         }
         else if (BotHelperService.IsRandomRecipeRequest(userMessage.Text))
         {
             // Sends a random recipe
             return(RecipePuppyService.GetRandomRecipe(userMessage, string.Empty));
         }
         else if (BotHelperService.IsNewRecipeForRequest(userMessage.Text))
         {
             // Sends a recipe for a particular item queried
             return(RecipePuppyService.GetRecipeFor(userMessage, BotConstants.OtherConstants.DefaultRecipeDish, string.Empty));
         }
         else if (BotHelperService.IsNewRecipeWithRequest(userMessage.Text))
         {
             // Sends a recipe for the ingredients queried
             return(RecipePuppyService.GetRecipeWith(userMessage, BotConstants.OtherConstants.DefaultIngredients, string.Empty));
         }
         else if (BotHelperService.IsTopNRecipesRequest(userMessage.Text))
         {
             // Send the top N recipes where N is a number
             return(RecipePuppyService.GetTopNRecipes(userMessage, BotConstants.OtherConstants.DefaultTopN, string.Empty));
         }
         else if (BotHelperService.IsAboutRequest(userMessage.Text))
         {
             // Return the about us message
             return(BotHelperService.AboutResponse(userMessage, string.Empty));
         }
         else if (BotHelperService.IsHelpOrGetStartedRequest(userMessage.Text))
         {
             // Returns the get started/help generic message
             return(BotHelperService.GetStartedResponse(userMessage, string.Empty));
         }
         else if (BotHelperService.IsVersionRequest(userMessage.Text))
         {
             // Returns the version response
             return(BotHelperService.VersionResponse(userMessage));
         }
         else if (BotHelperService.IsFeedbackRequest(userMessage.Text))
         {
             // Returns the feedback response
             return(BotHelperService.FeedbackResponse(userMessage, string.Empty));
         }
         else
         {
             // Send the queries message to the API.AI handler for further processing
             return(ApiAiService.HandleNaturalInput(userMessage));
         }
     }
     catch (Exception e)
     {
         System.Diagnostics.Trace.TraceError("Class BotService | HandleBotRequest() | Oops, something happened " + e.Message);
         System.Diagnostics.Trace.TraceError(e.StackTrace);
         return(userMessage.CreateReply("Gosh, this is embarrassing. Looks like we're having some trouble with that request. Maybe try sending a different request or try again later?"));
     }
 }
Exemplo n.º 2
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?"));
            }
        }