Last active
February 28, 2021 15:40
-
-
Save poornimanayar/c67817f0feca80b5b8fadc63ae064cb4 to your computer and use it in GitHub Desktop.
Action on Google & Umbraco Heartcore Azure Function Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.Logging; | |
using Newtonsoft.Json; | |
using Google.Cloud.Dialogflow.V2; | |
using Google.Protobuf; | |
using Umbraco.Headless.Client.Net.Delivery; | |
using System.Linq; | |
using System.Collections.Generic; | |
using Umbraco.Headless.Client.Net.Delivery.Models; | |
namespace AzureFunction.GoogleAPI | |
{ | |
public static class ActionOnGoogleWebhook | |
{ | |
[FunctionName("ActionOnGoogleWebhook")] | |
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,ILogger log) | |
{ | |
//Ignore unknown fields to futureproof the webhook against new fields added to the Webhook object | |
var jsonParser = new JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true)); | |
string requestBody; | |
//Read the request body | |
using (var reader = new StreamReader(req.Body)) | |
{ | |
requestBody = await reader.ReadToEndAsync(); | |
} | |
//Parse the request body into a strongly-typed WebhookRequest object | |
WebhookRequest request = jsonParser.Parse<WebhookRequest>(requestBody); | |
//Get the name of the character from the parameter field "person" | |
var searchTerm = request.QueryResult.Parameters.Fields["person"].StructValue.Fields["name"].StringValue; | |
//create a HeartCore ContentDeliveryService object passing in the alias of the Heartcore instance | |
var cdnService = new ContentDeliveryService("poornima-test-graphql"); | |
var filterProperties = new List<ContentFilterProperties>(); | |
//create and add filter properties | |
filterProperties.Add(new ContentFilterProperties("characterName", searchTerm , ContentFilterMatch.Contains)); | |
//filter out only the character content type | |
var filter = new ContentFilter("character", filterProperties.ToArray()); | |
//call the Filter endpoint passing in the filter | |
var items = await cdnService.Content.Filter(filter); | |
var fulfillmentText = "Sorry, I could not find anything about " + searchTerm; | |
if (items != null && items.Content!=null && items.Content.Items !=null && items.Content.Items.Any()) | |
{ | |
fulfillmentText = items.Content.Items.FirstOrDefault().Properties["iotDescription"].ToString(); | |
} | |
//form the webhook response object passing in the fulfillmenttext | |
WebhookResponse response = new WebhookResponse | |
{ | |
FulfillmentText = fulfillmentText | |
}; | |
//return the result with the response which will served to the user | |
return new OkObjectResult(response); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment