Exemplo n.º 1
0
        public async Task <HttpResponseMessage> ProcessMessageRequestAsync(HttpRequestMessage request, AlexaAdapter alexaAdapter, Func <ITurnContext, Task> botCallbackHandler, CancellationToken cancellationToken)
        {
            AlexaRequestBody skillRequest;

            byte[] requestByteArray;

            try
            {
                requestByteArray = await request.Content.ReadAsByteArrayAsync();

                skillRequest = await request.Content.ReadAsAsync <AlexaRequestBody>(AlexaMessageMediaTypeFormatters, cancellationToken);
            }
            catch (Exception)
            {
                throw new JsonSerializationException("Invalid JSON received");
            }

            if (skillRequest.Version != "1.0")
            {
                throw new Exception($"Unexpected version of '{skillRequest.Version}' received.");
            }

            if (_alexaOptions.ValidateIncomingAlexaRequests)
            {
                request.Headers.TryGetValues("SignatureCertChainUrl", out var certUrls);
                request.Headers.TryGetValues("Signature", out var signatures);
                var certChainUrl = certUrls.FirstOrDefault();
                var signature    = signatures.FirstOrDefault();
                await AlexaValidateRequestSecurityHelper.Validate(skillRequest, requestByteArray, certChainUrl, signature);
            }

            var alexaResponseBody = await alexaAdapter.ProcessActivity(
                skillRequest,
                _alexaOptions,
                null);

            if (alexaResponseBody == null)
            {
                return(null);
            }

            var alexaResponseBodyJson = JsonConvert.SerializeObject(alexaResponseBody, Formatting.None,
                                                                    new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                ContractResolver  = new DefaultContractResolver {
                    NamingStrategy = new CamelCaseNamingStrategy()
                }
            });

            var response = request.CreateResponse(HttpStatusCode.OK);

            response.Content = new StringContent(alexaResponseBodyJson);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            return(response);
        }
        protected async Task <AlexaResponseBody> ProcessMessageRequestAsync(HttpRequest request, AlexaAdapter alexaAdapter, BotCallbackHandler botCallbackHandler)
        {
            AlexaRequestBody skillRequest;

            var memoryStream = new MemoryStream();

            request.Body.CopyTo(memoryStream);
            var requestBytes = memoryStream.ToArray();

            memoryStream.Position = 0;

            using (var bodyReader = new JsonTextReader(new StreamReader(memoryStream, Encoding.UTF8)))
            {
                skillRequest = AlexaBotMessageSerializer.Deserialize <AlexaRequestBody>(bodyReader);
            }

            if (skillRequest.Version != "1.0")
            {
                throw new Exception($"Unexpected version of '{skillRequest.Version}' received.");
            }

            if (_alexaOptions.ValidateIncomingAlexaRequests)
            {
                request.Headers.TryGetValue("SignatureCertChainUrl", out var certUrls);
                request.Headers.TryGetValue("Signature", out var signatures);
                var certChainUrl = certUrls.FirstOrDefault();
                var signature    = signatures.FirstOrDefault();
                await AlexaValidateRequestSecurityHelper.Validate(skillRequest, requestBytes, certChainUrl, signature);
            }

            var alexaResponseBody = await alexaAdapter.ProcessActivity(
                skillRequest,
                _alexaOptions,
                botCallbackHandler);

            return(alexaResponseBody);
        }
        public async Task ProcessAsync(HttpRequest httpRequest, HttpResponse httpResponse, IBot bot, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (httpRequest == null)
            {
                throw new ArgumentNullException(nameof(httpRequest));
            }

            if (httpResponse == null)
            {
                throw new ArgumentNullException(nameof(httpResponse));
            }

            if (bot == null)
            {
                throw new ArgumentNullException(nameof(bot));
            }

            AlexaRequestBody skillRequest;

            var memoryStream = new MemoryStream();

            httpRequest.Body.CopyTo(memoryStream);
            var requestBytes = memoryStream.ToArray();

            memoryStream.Position = 0;

            using (var bodyReader = new JsonTextReader(new StreamReader(memoryStream, Encoding.UTF8)))
            {
                skillRequest = AlexaBotMessageSerializer.Deserialize <AlexaRequestBody>(bodyReader);
            }

            if (skillRequest.Version != "1.0")
            {
                throw new Exception($"Unexpected version of '{skillRequest.Version}' received.");
            }

            if (ValidateRequests)
            {
                httpRequest.Headers.TryGetValue("SignatureCertChainUrl", out var certUrls);
                httpRequest.Headers.TryGetValue("Signature", out var signatures);
                var certChainUrl = certUrls.FirstOrDefault();
                var signature    = signatures.FirstOrDefault();
                await AlexaValidateRequestSecurityHelper.Validate(skillRequest, requestBytes, certChainUrl, signature);
            }

            var alexaResponse = await ProcessActivity(
                skillRequest,
                bot.OnTurnAsync);

            if (alexaResponse == null)
            {
                throw new ArgumentNullException(nameof(alexaResponse));
            }

            httpResponse.ContentType = "application/json";
            httpResponse.StatusCode  = (int)HttpStatusCode.OK;

            using (var writer = new StreamWriter(httpResponse.Body))
            {
                using (var jsonWriter = new JsonTextWriter(writer))
                {
                    AlexaBotMessageSerializer.Serialize(jsonWriter, alexaResponse);
                }
            }
        }