public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string textContent = null;

            IAlexaRequestVerifier reqVerifier = new AlexaCertificateVerifier();
            bool isValid = false;

            try
            {
                isValid = await reqVerifier.IsCertificateValidAsync(req);
            }
            catch (Exception ex)
            {
                log.LogError(ex, "Error processing certificate");
            }

            if (!isValid)
            {
                return(new BadRequestResult());
            }

            AlexaRequest alexaRequest = null;

            try
            {
                using (StreamReader sr = new StreamReader(req.Body))
                {
                    //This allows you to do one Read operation.
                    textContent = sr.ReadToEnd();
                }

                alexaRequest = JsonConvert.DeserializeObject <AlexaRequest>(textContent);
            }
            catch (Exception ex)
            {
                log.LogError(ex, $"Error processing alexa request: {textContent}");
            }


            IAdventureSampleProcessor adventureProcessor = null;

            var alexaResponse = await adventureProcessor.ProcessAdventureRequestAsync(alexaRequest);

            return((ActionResult) new OkObjectResult(alexaResponse));
        }
Ejemplo n.º 2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");



#if !DEBUG
            //https://developer.amazon.com/docs/custom-skills/host-a-custom-skill-as-a-web-service.html
            IAlexaRequestVerifier reqVerifier = new AlexaCertificateVerifier();
            bool isValid = false;

            try
            {
                isValid = await reqVerifier.IsCertificateValidAsync(req);
            }
            catch (Exception ex)
            {
                log.LogError(ex, "Error processing certificate");
            }

            if (!isValid)
            {
                return(new BadRequestResult());
            }
#endif

            string       textContent  = null;
            AlexaRequest alexaRequest = null;
            try
            {
                using (StreamReader sr = new StreamReader(req.Body))
                {
                    //This allows you to do one Read operation.
                    textContent = sr.ReadToEnd();
                }

                alexaRequest = JsonConvert.DeserializeObject <AlexaRequest>(textContent);
            }
            catch (Exception ex)
            {
                log.LogError(ex, $"Error processing alexa request: {textContent}");
            }

            if (alexaRequest == null)
            {
                return(new BadRequestResult());
            }

            SpaceRequestType spaceReqType = GetSpaceRequestType(alexaRequest.Request);



            AlexaResponse alexaResp = null;
            switch (spaceReqType)
            {
            case SpaceRequestType.GetFact:
                //   ProgressiveResponseManager progressiveManager = new ProgressiveResponseManager(log);
                //   await progressiveManager.SendProgressiveResponseAsync(alexaRequest, "Processing request");

                Whetstone.Alexa.Security.AlexaUserDataManager userData = new AlexaUserDataManager(log);

                string givenName         = null;
                bool   isPermissionGiven = true;
                try
                {
                    givenName = await userData.GetAlexaUserGivenNameAsync(alexaRequest.Context.System.ApiEndpoint, alexaRequest.Context.System.ApiAccessToken);
                }
                catch (Exception ex)
                {
                    isPermissionGiven = false;
                }


                string spaceFact      = GetRandomFact();
                string storageAccount = Environment.GetEnvironmentVariable("StorageAccount");
                string spaceUrl       = GetMediaAudioUrl(SCIFISOUNDFILE, storageAccount);

                string audioTag = $"<audio src=\"{spaceUrl}\"/>";

                string factText = null;

                if (string.IsNullOrWhiteSpace(givenName))
                {
                    factText = string.Concat(audioTag, GET_FACT_MESSAGE, spaceFact);
                }
                else
                {
                    factText = string.Concat(audioTag, givenName, "  ", GET_FACT_MESSAGE, spaceFact);
                }


                alexaResp = GetAlexaResponse(factText);

                if (!isPermissionGiven)
                {
                    AddPermissionRequest(alexaResp);
                }
                break;

            case SpaceRequestType.Help:
                alexaResp = GetAlexaResponse(HELP_MESSAGE, HELP_REPROMPT);
                break;

            case SpaceRequestType.Stop:
                alexaResp = GetAlexaResponse(STOP_MESSAGE);
                break;
            }

            return(new OkObjectResult(alexaResp));
        }