/// <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());
        }
Example #3
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());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="requestEnvelope"></param>
        /// <returns></returns>
        public async Task <SpeechletResponseEnvelope> ProcessRequestAsync(SpeechletRequestEnvelope requestEnvelope)
        {
            var session = requestEnvelope.Session;
            var context = requestEnvelope.Context;
            var request = requestEnvelope.Request;

            var response = !(request is ExtendedSpeechletRequest) ?
                           await HandleStandardRequestAsync(request, session, context) :
                           await HandleExtendedRequestAsync(request as ExtendedSpeechletRequest, context);

            if (response == null)
            {
                response = new SpeechletResponse();
            }

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

            return(responseEnvelope);
        }
		/*
        public override ValidationResponse OnRequestValidation(ValidationRequest request)
        {
            var response = new ValidationResponse
            {
                ValidationResult = SpeechletRequestValidationResult.OK,
                Success = true
            };

            var alexaBytes = AsyncHelpers.RunSync<byte[]>(() => request.HttpRequest.Content.ReadAsByteArrayAsync());
            Debug.WriteLine(request.HttpRequest.ToLogString());

            GetRequest(alexaBytes, ref response);

            return response;
        }
		*/
		
        /**
         * Creates and returns the visual and spoken response with shouldEndSession flag
         * 
         * @param title
         *            title for the companion application home card
         * @param output
         *            output content for speech and companion application home card
         * @param shouldEndSession
         *            should the session be closed
         * @return SpeechletResponse spoken and visual response for the given input
         */
        private SpeechletResponse BuildSpeechletResponse(string title, string output, bool shouldEndSession) {
            // Create the Simple card content.
            SimpleCard card = new SimpleCard();
            card.Title = String.Format("SessionSpeechlet - {0}", title);
            card.Subtitle = String.Format("SessionSpeechlet - Sub Title");
            card.Content = String.Format("SessionSpeechlet - {0}", output);

            // Create the plain text output.
            PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
            speech.Text = output;

            // Create the speechlet response.
            SpeechletResponse response = new SpeechletResponse();
            response.ShouldEndSession = shouldEndSession;
            response.OutputSpeech = speech;
            response.Card = card;
            return response;
        }
        /// <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());
        }
        public override SpeechletResponse OnIntent(IntentRequest request, Session session)
        {
            var response = new SpeechletResponse();

            return response;
        }
        public override SpeechletResponse OnLaunch(LaunchRequest request, Session session)
        {
            var response = new SpeechletResponse();

            return response;
        }
        /**
         * Creates and returns the visual and spoken response with shouldEndSession flag
         * 
         * @param title
         *            title for the companion application home card
         * @param output
         *            output content for speech and companion application home card
         * @param shouldEndSession
         *            should the session be closed
         * @return SpeechletResponse spoken and visual response for the given input
         */
        private SpeechletResponse BuildSpeechletResponse(string title, string output, bool shouldEndSession)
        {
            // Create the Simple card content.
            var card = new SimpleCard
                {
                    Title = $"SessionSpeechlet - {title}",
                    Content = $"SessionSpeechlet - {output}"
                };

            // Create the plain text output.
            var speech = new PlainTextOutputSpeech
                {
                    Text = output
                };

            // Create the speechlet response.
            var response = new SpeechletResponse
                {
                    ShouldEndSession = shouldEndSession,
                    OutputSpeech = speech,
                    Card = card
                };
            return response;
        }