public void RenderTemplateDirective_BodyTemplate1()
        {
            const string TestDataFile       = @"TestData\ResponseWithRenderTemplateDirectiveAndBodyTemplate1.json";
            var          expectedJSONObject = JObject.Parse(File.ReadAllText(TestDataFile));

            var renderTemplateDirective = new RenderTemplateDirective();

            renderTemplateDirective.Template = new BodyTemplate1
            {
                Title       = "Body Template Title",
                TextContent = new TextContent
                {
                    PrimaryText = new TextContentItem
                    {
                        Text = "Hello world"
                    }
                }
            };

            var testResponse = new SpeechletResponse();

            testResponse.Directives.Add(renderTemplateDirective);

            var responseEnvelope = new SpeechletResponseEnvelope();

            responseEnvelope.Response = testResponse;
            var jsonResponse = responseEnvelope.ToJson();

            Assert.Equal(JsonConvert.SerializeObject(expectedJSONObject), jsonResponse);
        }
        public void RenderTemplateDirective_ListTemplate1()
        {
            const string TestDataFile       = @"TestData\ResponseWithRenderTemplateDirectiveAndListTemplate1.json";
            var          expectedJSONObject = JObject.Parse(File.ReadAllText(TestDataFile));

            var renderTemplateDirective = new RenderTemplateDirective();
            var listTemplate            = new ListTemplate1
            {
                Title = "List Template 1 Title",
                Token = "listtemplate1token"
            };

            AddListItemPlaceHolder(listTemplate, 1);
            AddListItemPlaceHolder(listTemplate, 2);
            AddListItemPlaceHolder(listTemplate, 3);
            renderTemplateDirective.Template = listTemplate;

            var testResponse = new SpeechletResponse();

            testResponse.Directives.Add(renderTemplateDirective);

            var responseEnvelope = new SpeechletResponseEnvelope();

            responseEnvelope.Response = testResponse;
            var jsonResponse = responseEnvelope.ToJson();

            Assert.Equal(JsonConvert.SerializeObject(expectedJSONObject), jsonResponse);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="requestContent"></param>
        /// <returns></returns>
        public async virtual Task <string> DoProcessRequestAsync(SpeechletRequestEnvelope requestEnvelope)
        {
            Session           session  = requestEnvelope.Session;
            SpeechletResponse response = null;

            // verify timestamp is within tolerance
            var diff = DateTime.UtcNow - requestEnvelope.Request.Timestamp;

            Debug.WriteLine("Request was timestamped {0:0.00} seconds ago.", diff.TotalSeconds);
            if (Math.Abs((decimal)diff.TotalSeconds) > Sdk.TIMESTAMP_TOLERANCE_SEC)
            {
                return(String.Empty);
            }

            // process launch request
            if (requestEnvelope.Request is LaunchRequest)
            {
                var request = requestEnvelope.Request as LaunchRequest;
                if (requestEnvelope.Session.IsNew)
                {
                    await OnSessionStartedAsync(
                        new SessionStartedRequest(request.RequestId, request.Timestamp), session);
                }
                response = await OnLaunchAsync(request, session);
            }

            // process intent request
            else if (requestEnvelope.Request is IntentRequest)
            {
                var request = requestEnvelope.Request as IntentRequest;

                // Do session management prior to calling OnSessionStarted and OnIntentAsync
                // to allow dev to change session values if behavior is not desired
                DoSessionManagement(request, session);

                if (requestEnvelope.Session.IsNew)
                {
                    await OnSessionStartedAsync(
                        new SessionStartedRequest(request.RequestId, request.Timestamp), session);
                }
                response = await OnIntentAsync(request, session);
            }

            // process session ended request
            else if (requestEnvelope.Request is SessionEndedRequest)
            {
                var request = requestEnvelope.Request as SessionEndedRequest;
                await OnSessionEndedAsync(request, session);
            }

            var responseEnvelope = new SpeechletResponseEnvelope {
                Version           = requestEnvelope.Version,
                Response          = response,
                SessionAttributes = session.Attributes
            };

            return(responseEnvelope.ToJson());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="requestEnvelope"></param>
        /// <returns></returns>
        private string DoProcessRequest(SpeechletRequestEnvelope requestEnvelope)
        {
            Session           session  = requestEnvelope.Session;
            Context           context  = requestEnvelope.Context;
            SpeechletResponse response = null;

            // process launch request
            if (requestEnvelope.Request is LaunchRequest)
            {
                var request = requestEnvelope.Request as LaunchRequest;
                if (requestEnvelope.Session.IsNew)
                {
                    OnSessionStarted(
                        new SessionStartedRequest(request.RequestId, request.Timestamp), session);
                }
                response = OnLaunch(request, session);
            }
            else if (requestEnvelope.Request is AudioPlayerRequest)
            {
                var request = requestEnvelope.Request as AudioPlayerRequest;
                response = OnAudioIntent(request, context);
            }
            // process intent request
            else if (requestEnvelope.Request is IntentRequest)
            {
                var request = requestEnvelope.Request as IntentRequest;

                // Do session management prior to calling OnSessionStarted and OnIntentAsync
                // to allow dev to change session values if behavior is not desired
                DoSessionManagement(request, session);

                if (requestEnvelope.Session.IsNew)
                {
                    OnSessionStarted(
                        new SessionStartedRequest(request.RequestId, request.Timestamp), session);
                }
                response = OnIntent(request, session, requestEnvelope.Context);
            }

            // process session ended request
            else if (requestEnvelope.Request is SessionEndedRequest)
            {
                var request = requestEnvelope.Request as SessionEndedRequest;
                OnSessionEnded(request, session);
            }

            var responseEnvelope = new SpeechletResponseEnvelope {
                Version           = requestEnvelope.Version,
                Response          = response,
                SessionAttributes = session?.Attributes ?? new Dictionary <string, string>()//requestEnvelope.Session?.Attributes
            };

            return(responseEnvelope.ToJson());
        }
示例#5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="requestEnvelope"></param>
        /// <returns></returns>
        private async Task <string> DoProcessRequestAsync(SpeechletRequestEnvelope requestEnvelope)
        {
            Session           session  = requestEnvelope.Session;
            SpeechletResponse response = null;

            // process launch request
            if (requestEnvelope.Request is LaunchRequest)
            {
                var request = requestEnvelope.Request as LaunchRequest;
                if (requestEnvelope.Session.IsNew)
                {
                    await OnSessionStartedAsync(
                        new SessionStartedRequest(request.RequestId, request.Timestamp), session);
                }
                response = await OnLaunchAsync(request, session);
            }

            // process intent request
            else if (requestEnvelope.Request is IntentRequest)
            {
                var request = requestEnvelope.Request as IntentRequest;

                // Do session management prior to calling OnSessionStarted and OnIntentAsync
                // to allow dev to change session values if behavior is not desired
                DoSessionManagement(request, session);

                if (requestEnvelope.Session.IsNew)
                {
                    await OnSessionStartedAsync(
                        new SessionStartedRequest(request.RequestId, request.Timestamp), session);
                }
                response = await OnIntentAsync(request, session);
            }

            // process session ended request
            else if (requestEnvelope.Request is SessionEndedRequest)
            {
                var request = requestEnvelope.Request as SessionEndedRequest;
                await OnSessionEndedAsync(request, session);
            }

            var responseEnvelope = new SpeechletResponseEnvelope {
                Version           = requestEnvelope.Version,
                Response          = response,
                SessionAttributes = session.Attributes
            };

            return(responseEnvelope.ToJson());
        }
        public void RenderTemplateDirective_BodyTemplate3()
        {
            const string TestDataFile       = @"TestData\ResponseWithRenderTemplateDirectiveAndBodyTemplate3.json";
            var          expectedJSONObject = JObject.Parse(File.ReadAllText(TestDataFile));

            var renderTemplateDirective = new RenderTemplateDirective();

            renderTemplateDirective.Template = new BodyTemplate3
            {
                Title       = "Body Template Title",
                TextContent = new TextContent
                {
                    PrimaryText = new TextContentItem
                    {
                        Text = "Hello world"
                    }
                },
                Image = new ImageContent
                {
                    ContentDescription = "TestImageBodyTemplate3",
                    Sources            = new List <ImageItem>
                    {
                        new ImageItem
                        {
                            HeightPixels = 100,
                            WidthPixels  = 200,
                            Url          = "urlplaceholder"
                        }
                    }
                },
                Token = "bodytemplate3token"
            };

            var testResponse = new SpeechletResponse();

            testResponse.Directives.Add(renderTemplateDirective);

            var responseEnvelope = new SpeechletResponseEnvelope();

            responseEnvelope.Response = testResponse;
            var jsonResponse = responseEnvelope.ToJson();

            Assert.Equal(JsonConvert.SerializeObject(expectedJSONObject), jsonResponse);
        }
        public void HintDirective()
        {
            const string TestDataFile       = @"TestData\ResponseWithHintDirective.json";
            var          expectedJSONObject = JObject.Parse(File.ReadAllText(TestDataFile));

            var hintDirective = new HintDirective();

            hintDirective.Hint = new HintItem {
                Text = "your hint text"
            };

            var testResponse = new SpeechletResponse();

            testResponse.Directives.Add(hintDirective);

            var responseEnvelope = new SpeechletResponseEnvelope();

            responseEnvelope.Response = testResponse;
            var jsonResponse = responseEnvelope.ToJson();

            Assert.Equal(JsonConvert.SerializeObject(expectedJSONObject), jsonResponse);
        }
示例#8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="requestEnvelope"></param>
        /// <returns></returns>
        private async Task <string> DoProcessRequestAsync(SpeechletRequestEnvelope requestEnvelope)
        {
            Session            session  = requestEnvelope.Session;
            var                context  = requestEnvelope.Context;
            var                request  = requestEnvelope.Request;
            ISpeechletResponse response = null;

            if (session != null)
            {
                // Do session management prior to calling OnSessionStarted and OnIntentAsync
                // to allow dev to change session values if behavior is not desired
                DoSessionManagement(request as IntentRequest, session);

                if (session.IsNew)
                {
                    await OnSessionStartedAsync(
                        new SessionStartedRequest(request.RequestId, request.Timestamp, request.Locale), session);
                }
            }

            // process launch request
            if (requestEnvelope.Request is LaunchRequest)
            {
                response = await OnLaunchAsync(request as LaunchRequest, session);
            }

            // process audio player request
            else if (requestEnvelope.Request is AudioPlayerRequest)
            {
                response = await OnAudioPlayerAsync(request as AudioPlayerRequest, context);
            }

            // process playback controller request
            else if (requestEnvelope.Request is PlaybackControllerRequest)
            {
                response = await OnPlaybackControllerAsync(request as PlaybackControllerRequest, context);
            }

            // process display request
            else if (requestEnvelope.Request is DisplayRequest)
            {
                response = await OnDisplayAsync(request as DisplayRequest, context);
            }

            // process system request
            else if (requestEnvelope.Request is SystemExceptionEncounteredRequest)
            {
                await OnSystemExceptionEncounteredAsync(request as SystemExceptionEncounteredRequest, context);
            }

            // process intent request
            else if (requestEnvelope.Request is IntentRequest)
            {
                response = await OnIntentAsync(request as IntentRequest, session, context);
            }

            // process session ended request
            else if (requestEnvelope.Request is SessionEndedRequest)
            {
                await OnSessionEndedAsync(request as SessionEndedRequest, session);
            }

            var responseEnvelope = new SpeechletResponseEnvelope {
                Version           = requestEnvelope.Version,
                Response          = response,
                SessionAttributes = session?.Attributes
            };

            return(responseEnvelope.ToJson());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="requestEnvelope"></param>
        /// <returns></returns>
        private async Task <string> DoProcessRequestAsync(SpeechletRequestEnvelope requestEnvelope)
        {
            Session           session  = requestEnvelope.Session;
            SpeechletResponse response = null;

            Trace.TraceInformation("In DoProcessRequestAsync");
            // process launch request
            if (requestEnvelope.Request is LaunchRequest)
            {
                var request = requestEnvelope.Request as LaunchRequest;
                if (requestEnvelope.Session.IsNew)
                {
                    await OnSessionStartedAsync(
                        new SessionStartedRequest(request.RequestId, request.Timestamp), session);
                }
                Trace.TraceInformation("request is LaunchRquest--about to call OnLaunchAsync");

                response = await OnLaunchAsync(request, session);
            }

            // process intent request
            else if (requestEnvelope.Request is IntentRequest)
            {
                var request = requestEnvelope.Request as IntentRequest;
                Trace.TraceInformation("In DoProcessRequestAsync, request is IntentRequest");

                // Do session management prior to calling OnSessionStarted and OnIntentAsync
                // to allow dev to change session values if behavior is not desired
                DoSessionManagement(request, session);

                if (requestEnvelope.Session.IsNew)
                {
                    await OnSessionStartedAsync(
                        new SessionStartedRequest(request.RequestId, request.Timestamp), session);
                }
                Trace.TraceInformation("--about to call OnIntentAsync");

                response = await OnIntentAsync(request, session);
            }


            // perform enqueue next audio request (to-do: others as well)
            else if (requestEnvelope.Request is AudioIntentRequest)
            {
                var     request = requestEnvelope.Request as AudioIntentRequest;
                Context context = requestEnvelope.Context;
                response = await OnAudioIntentAsync(request, context);

                session = new Session();
            }

            // perform enqueue next audio request (to-do: others as well)
            else if (requestEnvelope.Request is AudioPlayerRequest)
            {
                var     request = requestEnvelope.Request as AudioPlayerRequest;
                Context context = requestEnvelope.Context;
                response = await OnAudioPlayerAsync(request, context);

                session = new Session();
            }

            // process session ended request
            else if (requestEnvelope.Request is SessionEndedRequest)
            {
                var request = requestEnvelope.Request as SessionEndedRequest;
                await OnSessionEndedAsync(request, session);
            }

            var responseEnvelope = new SpeechletResponseEnvelope
            {
                Version           = requestEnvelope.Version,
                Response          = response,
                SessionAttributes = session.Attributes
            };

            Trace.TraceInformation($"Reached the bottom of DoProcessRequestAsync, requestEnvelope.Request is {requestEnvelope.Request.ToString()}");

            return(responseEnvelope.ToJson());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="requestEnvelope"></param>
        /// <returns></returns>
        private string DoProcessRequest(SpeechletRequestEnvelope requestEnvelope)
        {
            Session           session  = requestEnvelope.Session;
            Context           context  = requestEnvelope.Context;
            SpeechletResponse response = null;

            switch (requestEnvelope.Request)
            {
            case LaunchRequest request:
            {
                if (requestEnvelope.Session.IsNew)
                {
                    OnSessionStarted(requestEnvelope);
                }
                response = OnLaunch(request, session, context);
            }
            break;

            case AudioPlayerRequest request:
            {
                response = OnAudioIntent(request, session, context);
            }
            break;

            // process intent request
            case IntentRequest request:
            {
                // Do session management prior to calling OnSessionStarted and OnIntentAsync
                // to allow dev to change session values if behavior is not desired
                DoSessionManagement(request, session);

                if (requestEnvelope.Session.IsNew)
                {
                    OnSessionStarted(requestEnvelope);
                }
                response = OnIntent(request, session, context);
            }
            break;

            // process session ended request
            case SessionEndedRequest request:
            {
                OnSessionEnded(request, session, context);
            }
            break;
            }

            if (response == null)
            {
                return(new SpeechletResponseEnvelope
                {
                    Version = requestEnvelope.Version,
                    Response = new SpeechletResponse()
                    {
                        ShouldEndSession = null
                    }
                }.ToJson());
            }


            var responseEnvelope = new SpeechletResponseEnvelope
            {
                Version           = requestEnvelope.Version,
                Response          = response,
                SessionAttributes = requestEnvelope.Session?.Attributes ?? new Dictionary <string, string>()
            };

            return(responseEnvelope.ToJson());
        }