/// <summary> /// Creates a response to Dialogflow /// </summary> /// <param name="intentName">Intent Name fired</param> /// <param name="dialogflowRequest">The Dialogflow Request</param> /// <returns>A Dialogflow Response</returns> private static DialogflowResponse CreateResponse(string intentName, dynamic dialogflowRequest) { DialogflowResponse dialogflowResponse = null; if (!string.IsNullOrWhiteSpace(intentName)) { switch (intentName.ToLower()) { case "welcome": // Return a HTML response with the URL of your Interactive Canvas HTML Page dialogflowResponse = CreateDialogflowHtmlResponse("ok, lets play! What colour box should I reveal, red, green or blue?", INTERACTIVECANVAS_URL); break; case "trigger_whatcolorbox": // No colour specified, or colour already revealed so prompt user again dialogflowResponse = CreateDialogflowHtmlResponse("What colour box should I reveal, red, green or blue?"); break; case "trigger_correctchoice": // User selected a colour and it had not been previously revealed so prompt for next choice dialogflowResponse = CreateDialogflowHtmlResponse("Great choice! What colour box should I reveal next?"); break; case "revealbox": // Return a HTML response that triggers an action on the canvas + passes in an argument // Not passing a message to user from here as 'session / game state' handled by HTML page var updatedState = new UpdatedState() { Command = "revealbox", Arguments = new string[] { dialogflowRequest.queryResult.parameters.Color } }; dialogflowResponse = CreateDialogflowHtmlResponse(null, null, updatedState); break; case "trigger_youwon": // Trigger intent that has been triggered within the Interactive canvas dialogflowResponse = CreateDialogflowHtmlResponse("you won the game, congratulations!", null, null, false); break; } } if (dialogflowResponse == null) { // If none of the above return the fallback intent dialogflowResponse = CreateFallbackResponse(); } return(dialogflowResponse); }
/// <summary> /// Create Dialogflow Html Response /// </summary> /// <param name="textMessageToUser">The Text Message To User</param> /// <param name="interactiveCanvasUrl">The Interactive Canvas Url</param> /// <param name="updatedState">The Updated State</param> /// <param name="expectUserResponse">If we expect a user response</param> /// <returns>Dialogflow Response</returns> private static DialogflowResponse CreateDialogflowHtmlResponse(string textMessageToUser = null, string interactiveCanvasUrl = null, UpdatedState updatedState = null, bool expectUserResponse = true) { DialogflowResponse dialogflowResponse = CreateDialogflowBasicResponse(expectUserResponse, textMessageToUser); // Regardless of whether there is anything in it, always return a HTML object in DF response // to keep the interactive canvas open var htmlResponseHolder = new HtmlResponseWrapper(); if (!string.IsNullOrWhiteSpace(interactiveCanvasUrl) || updatedState != null) { // Do we have an interactive canavas url? (To kick off interaction) if (!string.IsNullOrWhiteSpace(interactiveCanvasUrl)) { // Tag end of URL with a GUID when in Dev to force IFrame reload htmlResponseHolder.HtmlResponse.Url = $"{interactiveCanvasUrl}?t={Guid.NewGuid().ToString()}"; } // Is there a state update? if (updatedState != null) { htmlResponseHolder.HtmlResponse.UpdatedState = updatedState; } } dialogflowResponse.Payload.Google.RichResponse.Items.Add(htmlResponseHolder); return(dialogflowResponse); }