public JsonLink GetLink(string id) { // Second API Call to get recipe links string url = "https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/"; // Request query string. var queryString2 = HttpUtility.ParseQueryString(string.Empty); url += id; url += "/information?includeNutrition=false"; HttpResponse <MemoryStream> responseAPI2 = Unirest.get(url) .header("X-Mashape-Key", APIKeys.Mashape_Key_Gabby) .header("Accept", "application/json") .asJson <MemoryStream>(); // System.IO.MemoryStream encoding to string StreamReader reader2 = new StreamReader(responseAPI2.Body); string json2 = reader2.ReadToEnd(); // Remove extra brackets on JSON string // Unsure if API is returning extra brackets or if StreamReader is adding json2 = json2.TrimStart('['); json2 = json2.TrimEnd(']'); // Deserialize json string into an object instance JsonLink recipeLink = new JsonLink(); JsonConvert.PopulateObject(json2, recipeLink); return(recipeLink); }
partial void FromToken_PreCondition(JsonLink instance, ref JToken json) { JsonRelationType_UnitTests.PopulateLinkTypes(); JObject jObject = CannedMessages.SampleWorkItemsWithRelations; JToken workItem = jObject["value"].Value <JArray>()[1]; var relations = workItem["relations"].Value <JArray>(); json = relations[2]; }
internal static JsonLink GetInstance([CallerMemberName] string callerName = "") { JToken json = new JObject(); JsonSource(ref json); JsonLink instance = JsonLink.FromToken(json); InstanceFactory(ref instance, callerName); return(instance); }
public void Url_UnitTest() { ExecuteProperty( () => // Create Test Instance { JsonLink instance = GetInstance(); return(instance); }, null, null, null, // No Set Accessor // Invoke Getter instance => { return(instance.Url); }, // Validate Get Operation (instance, setValue, getValue) => { }); }
public void FromToken_UnitTest() { JToken content = default(JToken); JsonLink _retVal = default(JsonLink); ExecuteMethod( () => { return(GetInstance()); }, instance => { content = default(JToken); //No Constructor FromToken_PreCondition(instance, ref content); }, instance => { return(_retVal = JsonLink.FromToken(content)); }, instance => { FromToken_PostValidate(instance, content, _retVal); }); }
public void Contains_UnitTest() { JsonLink item = default(JsonLink); Boolean _retVal = default(Boolean); ExecuteMethod( () => { return(GetInstance()); }, instance => { item = default(JsonLink); //No Constructor Contains_PreCondition(instance, ref item); }, instance => { return(_retVal = instance.Contains(item)); }, instance => { Contains_PostValidate(instance, item, _retVal); }); }
public void IndexOf_UnitTest() { JsonLink item = default(JsonLink); Int32 _retVal = default(Int32); ExecuteMethod( () => { return(GetInstance()); }, instance => { item = default(JsonLink); //No Constructor IndexOf_PreCondition(instance, ref item); }, instance => { return(_retVal = instance.IndexOf(item)); }, instance => { IndexOf_PostValidate(instance, item, _retVal); }); }
public void Insert_UnitTest() { Int32 index = default(Int32); JsonLink item = default(JsonLink); ExecuteMethod( () => { return(GetInstance()); }, instance => { index = default(Int32); //No Constructor item = default(JsonLink); //No Constructor Insert_PreCondition(instance, ref index, ref item); }, instance => { instance.Insert(index, item); }, instance => { Insert_PostValidate(instance, index, item); }); }
partial void Remove_PostValidate(JsonLinkCollection instance, JsonLink item, Boolean _retVal);
partial void Insert_PostValidate(JsonLinkCollection instance, Int32 index, JsonLink item);
partial void Insert_PreCondition(JsonLinkCollection instance, ref Int32 index, ref JsonLink item);
partial void IndexOf_PostValidate(JsonLinkCollection instance, JsonLink item, Int32 _retVal);
partial void IndexOf_PreCondition(JsonLinkCollection instance, ref JsonLink item);
/// <summary> /// POST: api/Messages /// Receive a message from a user and reply to it /// </summary> public async Task <HttpResponseMessage> Post([FromBody] Activity activity) { StateClient stateClient = activity.GetStateClient(); BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id); if (activity.Type == ActivityTypes.Message) { ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); // holds response to displayto user string answer = "I'm sorry. I didn't understand that. Try: \"What can we cook for dinner?\" or \"What ingredients are we missing?\""; Rootobject luisObj = await LUISClient.ParseUserInput(activity.Text); if (luisObj.intents.Length > 0) { switch (luisObj.intents[0].intent) // assumption: first intent is highest score { case "": answer = "I'm sorry. I didn't understand that. You can add and remove ingredients from your list or try to find a recipe!"; break; case "greeting": answer = "Hello! Feeling hungry and adventurous? You can add and remove ingredients from your list or try to find a recipe!"; break; case "addList": // add ingredient(s) to the list // create array to hold entities string[] items = new string[luisObj.entities.Length]; // parse the entities and pass in for (int i = 0; i < luisObj.entities.Length; i++) { items[i] = luisObj.entities[i].entity; } // Store the values into a local text file var IngredientsListAdd = new StateList(); IngredientsListAdd.AddIngredients(items); answer = "Successfully added ingredients."; break; case "removeList": // remove ingredient(s) to the list // create array to hold entities string[] removeItems = new string[luisObj.entities.Length]; // parse the entities and pass in for (int i = 0; i < luisObj.entities.Length; i++) { removeItems[i] = luisObj.entities[i].entity; } // Remove the values from the local text file var IngredientsListRemove = new StateList(); IngredientsListRemove.RemoveIngredients(removeItems); answer = "Sucesfully removed ingredients."; break; case "newList": // clear the current list to start new var IngredientsListClear = new StateList(); IngredientsListClear.clearIngredients(); answer = "Sucesfully clearned the ingredients list. Add more ingredients back!"; break; case "displayList": // read current ingredients list var IngredientsListRead = new StateList(); var list = IngredientsListRead.ReadIngredients(); var listItems = ""; for (int i = 0; i < list.Length; i++) { listItems += (i + 1) + ". " + list[i] + '\n'; } answer = "You currently have: \n" + listItems; break; case "findRecipe": // read current ingredients list var IngredientsListAPI = new StateList(); var arg = IngredientsListAPI.ReadIngredients(); // empty list check if (arg.Length == 0) { answer = "It looks like your ingredients list is empty. Try adding some ingredients then searching for a recipe!"; break; } var API_arg = "I have"; for (int i = 0; i < arg.Length; i++) { if ((i + 1) == arg.Length) { // last item in array API_arg += " " + arg[i]; } else { API_arg += " " + arg[i] + ","; } } var caller = new CallAPI(); if (!caller.isLimitHit()) { // Initialize recipeResult to hold json info // Call first API to obtain recipe from ingredients var recipeResult = new JsonRecipe(); recipeResult = caller.GetRecipe(API_arg); // Initialize recipeResult to hold json info // Call second API to obtain recipe link from recipeId var recipeLink = new JsonLink(); recipeLink = caller.GetLink(recipeResult.id.ToString()); // reply back to user with the recipe options Activity replyMessage = activity; Activity replyToConversation = replyMessage.CreateReply("May I interest you in.."); replyToConversation.Recipient = replyMessage.From; replyToConversation.Type = "message"; replyToConversation.Attachments = new List <Attachment>(); List <CardImage> cardImages = new List <CardImage>(); cardImages.Add(new CardImage(url: recipeResult.image)); List <CardAction> cardButtons = new List <CardAction>(); CardAction plButton = new CardAction() { Value = recipeLink.sourceUrl, Type = "openUrl", Title = "Let's Get Cooking!" }; cardButtons.Add(plButton); HeroCard plCard = new HeroCard() { Title = recipeResult.title, Subtitle = "Recommended by " + recipeResult.likes.ToString() + " others!", Images = cardImages, Buttons = cardButtons }; Attachment plAttachment = plCard.ToAttachment(); replyToConversation.Attachments.Add(plAttachment); var replyCard = await connector.Conversations.SendToConversationAsync(replyToConversation); // Load our current ingredients list we we can cross check and mark what we already have. var IngredientsListGrocery = new StateList(); var inventoryList = IngredientsListGrocery.ReadIngredients(); // GetIngredients - complete ingredients from the API response // Load the list into a sorted dictionary with the aisle for user's convenience // Before we load into the dictionary, cross-check to see if we already have the item int counter = 0; var dict = new SortedDictionary <string, string>(); foreach (var item in recipeLink.extendedIngredients) { for (int searchIndex = 0; searchIndex < inventoryList.Length; searchIndex++) { if (inventoryList[searchIndex] == item.name) { item.aisle = "At Home!"; } } dict.Add(item.name, item.aisle); counter++; } // create two arrays, keys = ingredeints, value = aisle. var keyArray = new string[counter]; var valueArray = new string[counter]; int index = 0; foreach (KeyValuePair <string, string> item in dict.OrderBy(key => key.Value)) { keyArray[index] = item.Key; valueArray[index] = item.Value; index++; } userData.SetProperty <string[]>("foodList", keyArray); userData.SetProperty <string[]>("aisleList", valueArray); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); } answer = "It looks like you may be missing some ingredients! Try: \"Send me a grocery list!\""; break; case "getShoppingList": // Retrieve ShoppingList from state var foodList = userData.GetProperty <string[]>("foodList"); var aisleList = userData.GetProperty <string[]>("aisleList"); var toBuy = ""; // print the cross-checked grocery list for the user for (int i = 0; i < foodList.Length; i++) { toBuy += (i + 1) + ". " + foodList[i] + " (" + aisleList[i] + ")" + '\n'; } answer = "Shopping List: \n" + toBuy; break; } } else { //run out of calls.. try again tomorrow... order out?..pizza # answer = "Sorry! Kitchen is closed.. Time to order out! (425) 453-7200 [Domino's Pizza Bellevue Square]"; } // Return response back to the user Activity reply = activity.CreateReply(answer); await connector.Conversations.ReplyToActivityAsync(reply); } else { HandleSystemMessage(activity); } var response = Request.CreateResponse(HttpStatusCode.OK); return(response); }
static partial void InstanceFactory(ref JsonLink instance, [CallerMemberName] string callerName = "");
partial void Equals_PreCondition(JsonLink instance, ref Object obj);
/// <summary> /// POST: api/Messages /// Receive a message from a user and reply to it /// </summary> public async Task <HttpResponseMessage> Post([FromBody] Activity activity) { StateClient stateClient = activity.GetStateClient(); BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id); if (activity.Type == ActivityTypes.Message) { ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); // holds response to displayto user string answer = "I'm sorry. I didn't understand that. Try: \"What can we cook for dinner?\" or \"What ingredients are we missing?\""; Rootobject luisObj = await LUISClient.ParseUserInput(activity.Text); if (luisObj.intents.Length > 0) { switch (luisObj.intents[0].intent) // assumption: first intent is highest score { case "": answer = "I'm sorry. I didn't understand that. Try: \"What can we cook for dinner?\" or \"What ingredients are we missing?\""; break; case "Greeting": answer = "Hello! Feeling hungry and adventurous? Try: \"What can we cook for dinner?\" or \"I'm hungry!\""; break; case "FindRecipe": answer = "Could you please provide me with a list of ingredeints you have or wish to use?"; break; case "PassIngredients": // take in ingredients list and format correctly to pass into API url activity.Text.Remove(0, 6); // remove "I have " activity.Text.Replace(",", "%2C"); // replace all commas with '%2C' activity.Text.Replace(" ", ""); // remove all spaces if there any var argIngredients = activity.Text; var caller = new CallAPI(); if (!caller.isLimitHit()) { // Initialize recipeResult to hold json info // Call first API to obtain recipe from ingredients var recipeResult = new JsonRecipe(); recipeResult = caller.GetRecipe(argIngredients); // Initialize recipeResult to hold json info // Call second API to obtain recipe link from recipeId var recipeLink = new JsonLink(); recipeLink = caller.GetLink(recipeResult.id.ToString()); // reply back to user with the recipe options Activity replyMessage = activity; Activity replyToConversation = replyMessage.CreateReply("May I interest you in.."); replyToConversation.Recipient = replyMessage.From; replyToConversation.Type = "message"; replyToConversation.Attachments = new List <Attachment>(); List <CardImage> cardImages = new List <CardImage>(); cardImages.Add(new CardImage(url: recipeResult.image)); List <CardAction> cardButtons = new List <CardAction>(); CardAction plButton = new CardAction() { Value = recipeLink.sourceUrl, Type = "openUrl", Title = "Let's Get Cooking!" }; cardButtons.Add(plButton); HeroCard plCard = new HeroCard() { Title = recipeResult.title, Subtitle = "Recommended by " + recipeResult.likes.ToString() + " others!", Images = cardImages, Buttons = cardButtons }; Attachment plAttachment = plCard.ToAttachment(); replyToConversation.Attachments.Add(plAttachment); var replyCard = await connector.Conversations.SendToConversationAsync(replyToConversation); //GetIngredients var dict = new SortedDictionary <string, string>(); foreach (var item in recipeLink.extendedIngredients) { dict.Add(item.name, item.aisle); } ShoppingList = "Shopping List: \n"; foreach (KeyValuePair <string, string> item in dict.OrderBy(key => key.Value)) { ShoppingList += String.Format("{0} ({1}).\n", item.Key, item.Value); } // Store the shoppingList string in the current client state // Allows the string to be accessed in preceeding reply userData.SetProperty <string>("ShoppingList", ShoppingList); await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData); } answer = "It looks like you may be missing some ingredients! Try: \"Send me a grocery list!\""; break; case "GetIngredients": // Retrieve ShoppingList from state answer = userData.GetProperty <string>("ShoppingList"); break; } } else { //run out of calls.. try again tomorrow... order out?..pizza # answer = "Sorry! Kitchen is closed.. Time to order out! (425) 453-7200 [Domino's Pizza Bellevue Square]"; } // Return response back to the user Activity reply = activity.CreateReply(answer); await connector.Conversations.ReplyToActivityAsync(reply); } else { HandleSystemMessage(activity); } var response = Request.CreateResponse(HttpStatusCode.OK); return(response); }
partial void Url_SetCondition(ref JsonLink instance, ref String setValue);
partial void GetType_PostValidate(JsonLink instance, Type _retVal);
partial void ToString_PreCondition(JsonLink instance);
partial void ToString_PostValidate(JsonLink instance, String _retVal);
partial void JsonValue_SetCondition(ref JsonLink instance, ref JToken setValue);
partial void Remove_PreCondition(JsonLinkCollection instance, ref JsonLink item);
partial void GetHashCode_PreCondition(JsonLink instance);
partial void FromToken_PreCondition(JsonLink instance, ref JToken content);
partial void Contains_PreCondition(JsonLinkCollection instance, ref JsonLink item);
partial void FromToken_PostValidate(JsonLink instance, JToken content, JsonLink _retVal);
partial void GetType_PreCondition(JsonLink instance);
public Dictionary <string, object> getLinksUserWise([FromBody] JsonLink LinkData) { return(ObjDAL.GetLinks(LinkData)); }
partial void GetHashCode_PostValidate(JsonLink instance, Int32 _retVal);