public AlexaFunction(IAlexaRequestVerifier reqVerifier, IAdventureSampleProcessor adventureProcessor)
        {
            _reqVerifier = reqVerifier ?? throw new ArgumentNullException($"{nameof(reqVerifier)} cannot be null");

            _adventureProcessor = adventureProcessor ??
                                  throw new ArgumentNullException($"{nameof(adventureProcessor)} cannot be null");
        }
        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));
        }
Exemplo n.º 3
0
        private async Task SendAlexaRequestAsync(HttpRequest req, ILogger logger, IAlexaRequestVerifier alexaVerifier, IAdventureSampleProcessor advSampleProcessor)
        {
            try
            {
                AlexaFunction alexaFunc = new AlexaFunction(alexaVerifier, advSampleProcessor);

                IActionResult res = await alexaFunc.Run(req, logger);

                ProcessActionResult(res);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw;
            }
        }
Exemplo n.º 4
0
 public AlexaController(IAdventureSampleProcessor adventureProcessor, ILogger <AlexaController> logger)
 {
     _adventureProcessor = adventureProcessor;
     _logger             = logger;
 }