/// <summary> /// Handle the intent. /// </summary> /// <param name="req"></param> /// <returns></returns> public override string Handle(ConvRequest req) { var exception = req.Parameters["exception"]; // Throw the requested exception type which will be picked up by Error-Reporting switch (exception.ToLowerInvariant()) { case null: return(DialogflowApp.Tell("Oops, not sure what to throw.")); case "exception": throw new Exception(); case "argumentexception": throw new ArgumentException(); case "invalidoperationexception": throw new InvalidOperationException(); case "notsupportedexception": throw new NotSupportedException(); case "notimplementedexception": throw new NotImplementedException(); case "argumentoutofrangeexception": throw new ArgumentOutOfRangeException(); case "argumentnullexception": throw new ArgumentNullException(); case "nullreferenceexception": throw new NullReferenceException(); case "timeoutexception": throw new TimeoutException(); case "stackoverflowexception": throw new StackOverflowException(); case "formatexception": throw new FormatException(); default: return(DialogflowApp.Tell($"Sorry, I don't know the exception type: '{exception}'")); } }
/// <summary> /// Handle the intent. /// </summary> /// <param name="req">Webhook request</param> /// <returns>Webhook response</returns> public override async Task <WebhookResponse> HandleAsync(WebhookRequest req) { var searchTerm = req.QueryResult.Parameters.Fields["searchterm"].StringValue; DialogflowApp.Show($"<div>Searching for pictures of: {searchTerm}</div><div>Please wait...</div>"); var searchService = CreateSearchClient(); var query = CreateSearchQuery(searchService, searchTerm); var result = await query.ExecuteAsync(); // Store images in state var images = result.Items .Select(x => new ConvState.Image { Title = x.Title, Url = x.Link }) .ToList(); _conversation.State.ImageList = images; var imageList = images.Select(x => $"<li><img src=\"{x.Url}\" alt=\"{WebUtility.HtmlEncode(x.Title)}\" style=\"width:200px\" /></li>"); DialogflowApp.Show($"<ol>{string.Join("", imageList)}</ol>"); return(new WebhookResponse { FulfillmentText = $"Found some pictures of: {searchTerm}. Now, select a picture." }); }
/// <summary> /// Consructor for Conversation controller. /// </summary> /// <param name="exceptionLogger">Exception logger</param> /// <param name="logger">Regular logger</param> /// <param name="tracer">Tracer</param> public ConversationController( IExceptionLogger exceptionLogger, ILogger <ConversationController> logger, IManagedTracer tracer) { _dialogFlowApp = new DialogflowApp(exceptionLogger, logger, tracer); }
/// <summary> /// Handle the intent. /// </summary> /// <param name="req">Conversation request</param> /// <returns></returns> public override async Task <string> HandleAsync(ConvRequest req) { var platform = await Platform.InstanceAsync(); (var spokenDescription, string[] textDescription) = GetDetailedDescription(platform); DialogflowApp.Show(string.Join("", textDescription.Select(x => $"<div>{x}</div>"))); return(DialogflowApp.Tell(spokenDescription)); }
/// <summary> /// Handle the intent. /// </summary> /// <param name="req">Webhook request</param> /// <returns>Webhook response</returns> public override async Task <WebhookResponse> HandleAsync(WebhookRequest req) { var platform = await Platform.InstanceAsync(); (var spokenDescription, string[] textDescription) = GetDetailedDescription(platform); DialogflowApp.Show(string.Join("", textDescription.Select(x => $"<div>{x}</div>"))); return(new WebhookResponse { FulfillmentText = spokenDescription }); }
public override async Task <WebhookResponse> HandleAsync(WebhookRequest req) { var searchTerm = req.QueryResult.Parameters.Fields["searchterm"].StringValue; DialogflowApp.Show($"<div>Searching for pictures of: {searchTerm}</div><div>Please wait...</div>"); var searchService = CreateSearchClient(); var result = await searchService.Images.SearchAsync(query : searchTerm); // Store images in state var images = result.Value .Select(x => new ConvState.Image { Title = x.Name, Url = x.ThumbnailUrl }) .ToList(); conversation.State.ImageList = images; var imageList = images.Select(x => $"<li><img src=\"{x.Url}\" alt=\"{WebUtility.HtmlEncode(x.Title)}\" style=\"width:200px\" /></li>"); DialogflowApp.Show($"<ol>{string.Join("", imageList)}</ol>"); // var response = new WebhookResponse(); var message = new Intent.Types.Message(); message.CarouselSelect = new Intent.Types.Message.Types.CarouselSelect(); foreach (var image in images) { var item = new Intent.Types.Message.Types.CarouselSelect.Types.Item(); item.Title = image.Title; var current = new Intent.Types.Message.Types.Image(); current.ImageUri = image.Url; item.Image = current; message.CarouselSelect.Items.Add(item); } // response.FulfillmentMessages.Add(message); // response.FulfillmentText = $"Found some pictures of: {searchTerm}. Now, select a picture."; // return response; return(new WebhookResponse { FulfillmentMessages = { message }, FulfillmentText = $"Found some pictures of: {searchTerm}. Now, select a picture.", Source = "" }); // return new WebhookResponse // { // FulfillmentText = $"Found some pictures of: {searchTerm}. Now, select a picture." // }; }
/// <summary> /// Handle the Dialogflow intent. /// </summary> /// <param name="req"></param> /// <returns></returns> public override async Task <string> HandleAsync(ConvRequest req) { // Create the client and ask for safe-search info from Vision API ML service. var visionClient = ImageAnnotatorClient.Create(); var safe = await visionClient.DetectSafeSearchAsync(Image.FromUri(_conversation.State.FocusedImage.Url)); // Format safe-search result into an English sentence (string text, int likelyhood) Likely(string s, Likelihood l) { switch (l) { case Likelihood.Likely: case Likelihood.VeryLikely: return(s, 2); case Likelihood.Possible: case Likelihood.Unlikely: return(s, 1); default: return(s, 0); } } var result = new[] { Likely("has medical content", safe.Medical), Likely("is a spoof", safe.Spoof), Likely("is violent", safe.Violence), Likely("has racy content", safe.Racy), Likely("has adult content", safe.Adult) }; var likely = result.Where(x => x.likelyhood == 2).Select(x => x.text).ToList(); var possible = result.Where(x => x.likelyhood == 1).Select(x => x.text).ToList(); if (likely.Count == 0 && possible.Count == 0) { return(DialogflowApp.Tell("Let's see. This picture is fine.")); } string reply = "Let's see. "; if (likely.Count > 0) { reply += CombineList(likely, "It's likely this picture", "") + "."; } if (possible.Count > 0) { reply += CombineList(possible, "It's possible this picture", "") + "."; } return(DialogflowApp.Tell(reply)); }
/// <summary> /// Handle the intent. /// </summary> /// <param name="req"></param> /// <returns></returns> public override string Handle(ConvRequest req) { // Unfocus the image _conversation.State.FocusedImage = null; // Update state with list of pictures var images = _conversation.State.ImageList; var imagesList = images.Select(x => $"<li><img src=\"{x.Url}\" alt=\"{WebUtility.HtmlEncode(x.Title)}\" style=\"width:200px\" /></li>"); DialogflowApp.Show($"<ol>{string.Join("", imagesList)}</ol>"); return(DialogflowApp.Tell("OK, looking at all images now.")); }
/// <summary> /// Handle the intent. /// </summary> /// <param name="req"></param> /// <returns></returns> public override async Task <string> HandleAsync(ConvRequest req) { // Create the client and ask for landmarks from Vision API ML service. var visionClient = ImageAnnotatorClient.Create(); var landmarks = await visionClient.DetectLandmarksAsync(Image.FromUri(_conversation.State.FocusedImage.Url)); // Order landmarks by score, and prepare the landmark descriptions for DialogFlow var toSay = landmarks.OrderByDescending(x => x.Score) .TakeWhile((x, i) => i == 0 || x.Score > 0.75) .Select(x => x.Description) .ToList(); return(DialogflowApp.Tell(CombineList(toSay, "This picture contains: ", "This picture contains no landmarks"))); }
/// <summary> /// Handle the intent. /// </summary> /// <param name="req"></param> /// <returns></returns> public override async Task <string> HandleAsync(ConvRequest req) { // Create the client and ask for labels from Vision API ML service. var visionClient = ImageAnnotatorClient.Create(); var labels = await visionClient.DetectLabelsAsync(Image.FromUri(_conversation.State.FocusedImage.Url), maxResults : 10); // Order labels by score, and prepare the label descriptions for DialogFlow var toSay = labels .OrderByDescending(x => x.Score) .TakeWhile((x, i) => i <= 2 || x.Score > 0.75) .Select(x => x.Description) .ToList(); return(DialogflowApp.Tell(CombineList(toSay, "This picture is labelled", "Nothing at all, apparently. Which is odd."))); }
/// <summary> /// Handle the intent. /// </summary> /// <param name="req">Webhook request</param> /// <returns>Webhook response</returns> public override async Task <WebhookResponse> HandleAsync(WebhookRequest req) { var errorMessage = ExtractAndValidateParameters(req, out string englishPhrase, out string languageCode); if (errorMessage != null) { DialogflowApp.Show($"<div>{errorMessage}</div>"); return(new WebhookResponse { FulfillmentText = errorMessage }); } var client = TranslationClient.Create(); var response = await client.TranslateTextAsync(englishPhrase, languageCode, LanguageCodes.English); DialogflowApp.Show($"<div>'{englishPhrase}' is '{response.TranslatedText}' in {languageCode}</div>"); return(new WebhookResponse { FulfillmentText = response.TranslatedText }); }
/// <summary> /// Handle the Dialogflow intent. /// </summary> /// <param name="req"></param> /// <returns></returns> public override string Handle(ConvRequest req) { if (!int.TryParse(req.Parameters["index"], out int index)) { // Something went wrong with parsing, index is not a number return(DialogflowApp.Tell($"I don't know which picture you're talking about.")); } if (index < 1 || index > _conversation.State.ImageList.Count) { // Tried to select an out-of-range picture. return(DialogflowApp.Tell($"That picture doesn't exist!")); } // Store the selected image in the state var image = _conversation.State.ImageList[index - 1]; _conversation.State.FocusedImage = image; DialogflowApp.Show($"<img src=\"{image.Url}\" alt=\"{WebUtility.HtmlEncode(image.Title)}\" style=\"height:100%;width:100%\" />"); return(DialogflowApp.Tell($"Picture {index} selected. You can describe, show landmarks or ask whether the image is safe.")); }
/// <summary> /// Handle the Dialogflow intent. /// </summary> /// <param name="req">Webhook request</param> /// <returns>Webhook response</returns> public override WebhookResponse Handle(WebhookRequest req) { int index = (int)req.QueryResult.Parameters.Fields["index"].NumberValue; if (index < 1 || index > _conversation.State.ImageList.Count) { // Tried to select an out-of-range picture. return(new WebhookResponse { FulfillmentText = $"That picture doesn't exist!" }); } // Store the selected image in the state var image = _conversation.State.ImageList[index - 1]; _conversation.State.FocusedImage = image; DialogflowApp.Show($"<img src=\"{image.Url}\" alt=\"{WebUtility.HtmlEncode(image.Title)}\" style=\"height:100%;width:100%\" />"); return(new WebhookResponse { FulfillmentText = $"Picture {index} selected. You can describe, show landmarks or ask whether the image is safe." }); }
public WebhookController() { dialogflowapp = new DialogflowApp(); auth = new BearerAuthentication(); }
/// <summary> /// Handle the intent. /// </summary> /// <param name="req"></param> /// <returns></returns> public override async Task <string> HandleAsync(ConvRequest req) { // Extract the DialogFlow date, without the time, that has been requested // Format is "yyyy-mm-dd" var date = req.Parameters["date"]; date = date.Substring(0, Math.Min(10, date.Length)); // Create the BigQuery client with default credentials var bigQueryClient = await BigQueryClient.CreateAsync(Program.AppSettings.GoogleCloudSettings.ProjectId); // Build the parameterized SQL query var table = bigQueryClient.GetTable("bigquery-public-data", "hacker_news", "full"); var sql = $"SELECT title, url \nFROM {table} \n" + "WHERE STARTS_WITH(CAST(timestamp AS STRING), @date) AND type=\"story\" \n" + "ORDER BY score DESC \n" + "LIMIT 10"; // Create the BigQuery parameters var parameters = new[] { new BigQueryParameter("date", BigQueryDbType.String, date) }; // Show SQL query in browser ShowQuery(sql, parameters); // Time the BigQuery execution with a StopWatch var stopwatch = new Stopwatch(); stopwatch.Start(); // Execute BigQuery SQL query. This can take time var result = await bigQueryClient.ExecuteQueryAsync(sql, parameters); // Query finished, stop the StopWatch stopwatch.Stop(); // Get a job reference, for statistics var job = await bigQueryClient.GetJobAsync(result.JobReference); // Get result list, and check that there are some results var resultList = result.ToList(); if (resultList.Count == 0) { return(DialogflowApp.Tell("Sorry, there is no data for that date.")); } // Time and data statistics long processedMb = job.Statistics.TotalBytesProcessed.Value / (1024 * 1024); double secs = stopwatch.Elapsed.TotalSeconds; var titles = resultList.Select(x => x["title"].ToString()).ToList(); // Show SQL query and query results in browser ShowQuery(sql, parameters, (processedMb, secs, titles)); // Send spoken response to DialogFlow return(DialogflowApp.Tell($"Scanned {processedMb} mega-bytes in {secs:0.0} seconds. " + $"The top title on hacker news was titled: {titles.First()}")); }
/// <summary> /// Handle the intent. /// </summary> /// <param name="req"></param> /// <returns></returns> public override async Task <string> HandleAsync(ConvRequest req) { var errorMessage = ExtractAndValidateParameters(req, out string temp, out string year, out string countryCode2, out string countryName, out string fipsCountry); if (errorMessage != null) { return(DialogflowApp.Tell(errorMessage)); } var bigQueryClient = await BigQueryClient.CreateAsync(Program.AppSettings.GoogleCloudSettings.ProjectId); // Build the parameterized SQL query var tableGsod = bigQueryClient.GetTable("bigquery-public-data", "noaa_gsod", $"gsod*"); var tableStations = bigQueryClient.GetTable("bigquery-public-data", "noaa_gsod", "stations"); var sql = $"SELECT (gsod.temp - 32)*5/8 AS celcius, stations.name AS name, gsod.year AS y, gsod.mo AS m, gsod.da AS d \n" + $"FROM {tableGsod} AS gsod \n" + $"JOIN {tableStations} AS stations ON gsod.stn = stations.usaf AND gsod.wban = stations.wban \n" + $"WHERE stations.country=@fipsCountry and gsod.year=@year \n" + $"ORDER BY gsod.temp{(temp == "hottest" ? " DESC" : "")} \n" + "limit 10"; // Create the BigQuery parameters var parameters = new[] { new BigQueryParameter("fipsCountry", BigQueryDbType.String, fipsCountry), new BigQueryParameter("year", BigQueryDbType.String, year) }; // Show SQL query in browser ShowQuery(sql, parameters); // Time the BigQuery execution with a StopWatch var stopwatch = new Stopwatch(); stopwatch.Start(); // Execute BigQuery SQL query. This can take time var result = await bigQueryClient.ExecuteQueryAsync(sql, parameters); // Query finished, stop the StopWatch stopwatch.Stop(); // Get a job reference, for statistics var job = await bigQueryClient.GetJobAsync(result.JobReference); // Get result list, and check that there are some results var resultList = result.ToList(); if (resultList.Count == 0) { return(DialogflowApp.Tell($"Sorry, there is no data for country '{countryName}'")); } // Time and data statistics long processedMb = job.Statistics.TotalBytesProcessed.Value / (1024 * 1024); double secs = stopwatch.Elapsed.TotalSeconds; var temperatures = resultList .Select(x => $"{(double)x["celcius"]:0.0}°C at {x["name"]} on {x["y"]}-{x["m"]}-{x["d"]}") .ToList(); // Show SQL query and query results in browser ShowQuery(sql, parameters, (processedMb, secs, temperatures)); // Send spoken response to DialogFlow var top = resultList[0]; return(DialogflowApp.Tell($"Scanned {processedMb} mega-bytes in {secs:0.0} seconds. " + $"The {temp} temperature in {countryName} in the year {year} was " + $"{(double)top["celcius"]:0.0} degrees celcius, at the {top["name"]} monitoring station.")); }
public ConversationController(ILogger <ConversationController> logger) { dialogFlowApp = new DialogflowApp(logger); }