Esempio n. 1
0
        public SkillResponse GetFunctionResponse(SkillRequest input, ILambdaContext context, IXeroService xeroService)
        {
            _xeroService = xeroService;

            LambdaLogger.Log($"SkillRequest: {JsonConvert.SerializeObject(input.Request)}");
            LambdaLogger.Log($"User: {JsonConvert.SerializeObject(input.Session.User)}");

            Response response = new Response();

            IOutputSpeech innerResponse = null;

            var requestType = input.Request.Type;

            if (input.Request.Type == Constants.LaunchRequest)
            {
                LambdaLogger.Log($"Default LaunchRequest made");

                innerResponse = new PlainTextOutputSpeech();
                (innerResponse as PlainTextOutputSpeech).Text = Constants.DefaultLaunchRequestText;

                response.ShouldEndSession = false;
            }
            else if (input.Request.Type == Constants.IntentRequest)
            {
                var intent = input.Request.Intent.Name;

                LambdaLogger.Log($"Intent Requested {intent}");

                var responseText = "";

                if (intent == Constants.ConnectionsIntent)
                {
                    try
                    {
                        responseText = _xeroService.GetConnections();
                    }
                    catch (UnauthorisedException)
                    {
                        responseText  = Constants.UnauthorisedResponse;
                        response.Card = new LinkAccountCard();
                    }

                    response.ShouldEndSession = true;
                }
                else if (intent == Constants.BalancesIntent)
                {
                    try
                    {
                        responseText = _xeroService.GetBalances();
                    }
                    catch (UnauthorisedException)
                    {
                        responseText  = Constants.UnauthorisedResponse;
                        response.Card = new LinkAccountCard();
                    }

                    response.ShouldEndSession = true;
                }
                else if (intent == Constants.VersionIntent)
                {
                    responseText = $"Deployed version is {context.FunctionVersion}";
                    response.ShouldEndSession = true;
                }
                else if (intent == Constants.HelpIntent)
                {
                    responseText = Constants.HelpRequestText;
                    response.ShouldEndSession = false;
                }
                else if (intent == Constants.FallbackIntent)
                {
                    responseText = Constants.FallbackText;
                    response.ShouldEndSession = false;
                }
                else if (intent == Constants.StopIntent)
                {
                    response.ShouldEndSession = true;
                }
                else if (intent == Constants.CancelIntent)
                {
                    response.ShouldEndSession = true;
                }

                LambdaLogger.Log($"Response: {responseText} - Session should end: {response.ShouldEndSession}");

                innerResponse = new PlainTextOutputSpeech();
                (innerResponse as PlainTextOutputSpeech).Text = responseText;
            }

            response.OutputSpeech = innerResponse;

            var skillResponse = new SkillResponse
            {
                Response = response,
                Version  = "1.0"
            };

            return(skillResponse);
        }
 public XeroController(IXeroService xeroService)
 {
     _xeroService = xeroService;
 }