public static async Task <IActionResult> RunEntitySearch(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log,
            ExecutionContext executionContext)
        {
            log.LogInformation("Entity Search Custom Skill: C# HTTP trigger function processed a request.");

            string skillName = executionContext.FunctionName;
            IEnumerable <WebApiRequestRecord> requestRecords = WebApiSkillHelpers.GetRequestRecords(req);

            if (requestRecords == null)
            {
                return(new BadRequestObjectResult($"{skillName} - Invalid request record array."));
            }

            string bingApiKey = Environment.GetEnvironmentVariable(bingApiKeySetting, EnvironmentVariableTarget.Process);

            WebApiSkillResponse response = await WebApiSkillHelpers.ProcessRequestRecordsAsync(skillName, requestRecords,
                                                                                               async (inRecord, outRecord) => {
                var entityName = inRecord.Data["name"] as string;
                string uri     = bingApiEndpoint + "?q=" + Uri.EscapeDataString(entityName) + "&mkt=en-us&count=10&offset=0&safesearch=Moderate";

                IEnumerable <BingEntity> entities =
                    await WebApiSkillHelpers.FetchAsync <BingEntity>(uri, "Ocp-Apim-Subscription-Key", bingApiKey, "entities.value");

                ExtractTopEntityMetadata(entities, outRecord.Data);
                outRecord.Data["entities"] = entities;
                return(outRecord);
            });

            return(new OkObjectResult(response));
        }
        public static async Task <IActionResult> RunAnalyzeForm(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log,
            ExecutionContext executionContext)
        {
            log.LogInformation("Analyze Form Custom Skill: C# HTTP trigger function processed a request.");

            string skillName = executionContext.FunctionName;
            IEnumerable <WebApiRequestRecord> requestRecords = WebApiSkillHelpers.GetRequestRecords(req);

            if (requestRecords == null)
            {
                return(new BadRequestObjectResult($"{skillName} - Invalid request record array."));
            }

            string formsRecognizerApiKey = Environment.GetEnvironmentVariable(formsRecognizerApiKeySetting, EnvironmentVariableTarget.Process);
            string modelId = Environment.GetEnvironmentVariable(modelIdSetting, EnvironmentVariableTarget.Process);

            WebApiSkillResponse response = await WebApiSkillHelpers.ProcessRequestRecordsAsync(skillName, requestRecords,
                                                                                               async (inRecord, outRecord) => {
                var formUrl      = inRecord.Data["formUrl"] as string;
                var formSasToken = inRecord.Data["formSasToken"] as string;

                // Fetch the document
                byte[] documentBytes;
                using (var webClient = new WebClient())
                {
                    documentBytes = await webClient.DownloadDataTaskAsync(new Uri(formUrl + formSasToken));
                }

                string uri = formsRecognizerApiEndpoint + "/models/" + Uri.EscapeDataString(modelId) + "/analyze";

                List <Page> pages =
                    (await WebApiSkillHelpers.FetchAsync <Page>(
                         uri,
                         apiKeyHeader: "Ocp-Apim-Subscription-Key",
                         apiKey: formsRecognizerApiKey,
                         collectionPath: "pages",
                         method: HttpMethod.Post,
                         postBody: documentBytes,
                         contentType))
                    .ToList();

                foreach (KeyValuePair <string, string> kvp in fieldMappings)
                {
                    var value = GetField(pages, kvp.Key);
                    if (!string.IsNullOrWhiteSpace(value))
                    {
                        outRecord.Data[kvp.Value] = value;
                    }
                }
                return(outRecord);
            });

            return(new OkObjectResult(response));
        }
        public static async Task <IActionResult> RunGeoPointFromName(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log,
            ExecutionContext executionContext)
        {
            log.LogInformation("Geo Point From Name Custom skill: C# HTTP trigger function processed a request.");

            string skillName = executionContext.FunctionName;
            IEnumerable <WebApiRequestRecord> requestRecords = WebApiSkillHelpers.GetRequestRecords(req);

            if (requestRecords == null)
            {
                return(new BadRequestObjectResult($"{skillName} - Invalid request record array."));
            }

            string azureMapsKey = Environment.GetEnvironmentVariable(azureMapsKeySetting, EnvironmentVariableTarget.Process);

            WebApiSkillResponse response = await WebApiSkillHelpers.ProcessRequestRecordsAsync(skillName, requestRecords,
                                                                                               async (inRecord, outRecord) => {
                var address = inRecord.Data["address"] as string;
                string uri  = azureMapsUri
                              + "?api-version=1.0&query=" + Uri.EscapeDataString(address)
                              + "&subscription-key=" + Uri.EscapeDataString(azureMapsKey);

                IEnumerable <Geography> geographies =
                    await WebApiSkillHelpers.FetchAsync <Geography>(uri, "results");

                if (geographies.FirstOrDefault() is Geography mainGeoPoint)
                {
                    outRecord.Data["mainGeoPoint"] = new {
                        Type        = "Point",
                        Coordinates = new double[] { mainGeoPoint.Position.Lon, mainGeoPoint.Position.Lat }
                    };
                }
                outRecord.Data["results"] = geographies;
                return(outRecord);
            });

            return(new OkObjectResult(response));
        }