/// <summary>
        /// Processes Alexa request AND validates request signature
        /// </summary>
        /// <param name="httpRequest"></param>
        /// <returns></returns>
        public static async Task <HttpResponseMessage> GetResponseAsync(this SpeechletService service, HttpRequestMessage httpRequest)
        {
            string chainUrl = null;

            if (httpRequest.Headers.Contains(Sdk.SIGNATURE_CERT_URL_REQUEST_HEADER))
            {
                chainUrl = httpRequest.Headers.GetValues(Sdk.SIGNATURE_CERT_URL_REQUEST_HEADER).FirstOrDefault(x => !string.IsNullOrEmpty(x));
            }

            string signature = null;

            if (httpRequest.Headers.Contains(Sdk.SIGNATURE_REQUEST_HEADER))
            {
                signature = httpRequest.Headers.GetValues(Sdk.SIGNATURE_REQUEST_HEADER).FirstOrDefault(x => !string.IsNullOrEmpty(x));
            }

            var content = await httpRequest.Content.ReadAsStringAsync();

            try {
                var alexaRequest = await service.GetRequestAsync(content, chainUrl, signature);

                var alexaResponse = await service.ProcessRequestAsync(alexaRequest);

                var json = alexaResponse?.ToJson();

                return((json == null) ?
                       new HttpResponseMessage(HttpStatusCode.InternalServerError) :
                       new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(json, Encoding.UTF8, "application/json")
                });
            }
            catch (SpeechletValidationException ex) {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    ReasonPhrase = ex.ValidationResult.ToString()
                });
            }
        }
Esempio n. 2
0
 protected SpeechletBase()
 {
     Service = new SpeechletService(this);
 }