Exemplo n.º 1
0
        public dynamic RouteRequest(EchoRequest echoRequest)
        {
            //TODO: handling of different intent types
            string url;
            using (var dbo = new EchoEntities())
            {
                var intent = dbo.Intents.Include("Service").First(x => x.Name == ((IntentRequest)echoRequest.request).intent.name &&  x.ApplicationId == echoRequest.session.application.applicationId && x.Active);
                url = string.Format(intent.Service.BaseUrl, intent.Service.Port, intent.Name);
            }                

            var client = new HttpClient();

            HttpResponseMessage response = new HttpResponseMessage();
            switch (echoRequest.request.type)
            {
                case RequestType.IntentRequest:
                    var jobject = JsonConvert.SerializeObject(((IntentRequest)echoRequest.request).intent.slots);
                    var content = new StringContent(jobject, Encoding.UTF8, "application/json");
                    response = client.PostAsync(url, content).Result;
                    break;
                case RequestType.LaunchRequest:
                    response = client.GetAsync(url).Result;
                    break;
                case RequestType.SessionEndRequest:
                    response = client.GetAsync(string.Format("{0}/{1}", url, ((SessionEndRequest)echoRequest.request).reason)).Result;
                    break;
            }

            //TODO: success/fail handling and create a standard response object to map to EchoResponseObject

            var echoReponse = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result) as EchoResponse;

            if (echoReponse != null)
                return echoReponse;

            return response.Content.ReadAsStringAsync().Result;
        }
Exemplo n.º 2
0
        public EchoResponse Post(EchoRequest echoRequest)
        {
            try
            {               
                var router = new RequestRouter();
                var response = router.RouteRequest(echoRequest);

                if (response is EchoResponse)
                    return response;
                else
                {
                    var defaultResponse = new EchoResponse();
                    defaultResponse.version = "1.0.1";
                    defaultResponse.response = new EchoResponse.Response();
                    defaultResponse.response.shouldEndSession = true;
                    defaultResponse.response.outputSpeech = new EchoResponse.Response.OutputSpeech();
                    defaultResponse.response.outputSpeech.type = OutputSpeechType.PlainText;
                    defaultResponse.response.outputSpeech.text = response;

                    return defaultResponse;
                }

            }
            catch (Exception ex)
            {
                var defaultResponse = new EchoResponse();
                defaultResponse.version = "1.0.1";
                defaultResponse.response = new EchoResponse.Response();
                defaultResponse.response.shouldEndSession = true;
                defaultResponse.response.outputSpeech = new EchoResponse.Response.OutputSpeech();
                defaultResponse.response.outputSpeech.type = OutputSpeechType.PlainText;
                defaultResponse.response.outputSpeech.text = string.Format("ECHO Exception! {0} - {1}", ex.Message, ex.StackTrace);

                return defaultResponse;
            }            
        }