Example #1
0
        private static SpaceRequestType GetSpaceRequestType(RequestAttributes reqAttributes)
        {
            SpaceRequestType spaceReqType = SpaceRequestType.Help;

            switch (reqAttributes.Type)
            {
            case RequestType.LaunchRequest:
                spaceReqType = SpaceRequestType.GetFact;
                break;

            case RequestType.IntentRequest:

                string intent = reqAttributes.Intent.Name;

                if (intent.Equals("GetNewFactIntent", StringComparison.OrdinalIgnoreCase))
                {
                    spaceReqType = SpaceRequestType.GetFact;
                }
                else if (intent.Equals("AMAZON.HelpIntent", StringComparison.OrdinalIgnoreCase))
                {
                    spaceReqType = SpaceRequestType.Help;
                }
                else if (intent.Equals("AMAZON.Cancel", StringComparison.OrdinalIgnoreCase) ||
                         intent.Equals("AMAZON.Stop", StringComparison.OrdinalIgnoreCase))
                {
                    spaceReqType = SpaceRequestType.Stop;
                }
                break;
            }

            return(spaceReqType);
        }
        private static SpaceRequestType GetSpaceRequestType(string intentName)
        {
            SpaceRequestType spaceReqType = SpaceRequestType.Help;


            if (intentName.Equals("Default Welcome Intent", StringComparison.OrdinalIgnoreCase) ||
                intentName.Equals("GetNewFactIntent", StringComparison.OrdinalIgnoreCase))
            {
                spaceReqType = SpaceRequestType.GetFact;
            }

            return(spaceReqType);
        }
Example #3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");



#if !DEBUG
            //https://developer.amazon.com/docs/custom-skills/host-a-custom-skill-as-a-web-service.html
            IAlexaRequestVerifier reqVerifier = new AlexaCertificateVerifier();
            bool isValid = false;

            try
            {
                isValid = await reqVerifier.IsCertificateValidAsync(req);
            }
            catch (Exception ex)
            {
                log.LogError(ex, "Error processing certificate");
            }

            if (!isValid)
            {
                return(new BadRequestResult());
            }
#endif

            string       textContent  = null;
            AlexaRequest alexaRequest = null;
            try
            {
                using (StreamReader sr = new StreamReader(req.Body))
                {
                    //This allows you to do one Read operation.
                    textContent = sr.ReadToEnd();
                }

                alexaRequest = JsonConvert.DeserializeObject <AlexaRequest>(textContent);
            }
            catch (Exception ex)
            {
                log.LogError(ex, $"Error processing alexa request: {textContent}");
            }

            if (alexaRequest == null)
            {
                return(new BadRequestResult());
            }

            SpaceRequestType spaceReqType = GetSpaceRequestType(alexaRequest.Request);



            AlexaResponse alexaResp = null;
            switch (spaceReqType)
            {
            case SpaceRequestType.GetFact:
                //   ProgressiveResponseManager progressiveManager = new ProgressiveResponseManager(log);
                //   await progressiveManager.SendProgressiveResponseAsync(alexaRequest, "Processing request");

                Whetstone.Alexa.Security.AlexaUserDataManager userData = new AlexaUserDataManager(log);

                string givenName         = null;
                bool   isPermissionGiven = true;
                try
                {
                    givenName = await userData.GetAlexaUserGivenNameAsync(alexaRequest.Context.System.ApiEndpoint, alexaRequest.Context.System.ApiAccessToken);
                }
                catch (Exception ex)
                {
                    isPermissionGiven = false;
                }


                string spaceFact      = GetRandomFact();
                string storageAccount = Environment.GetEnvironmentVariable("StorageAccount");
                string spaceUrl       = GetMediaAudioUrl(SCIFISOUNDFILE, storageAccount);

                string audioTag = $"<audio src=\"{spaceUrl}\"/>";

                string factText = null;

                if (string.IsNullOrWhiteSpace(givenName))
                {
                    factText = string.Concat(audioTag, GET_FACT_MESSAGE, spaceFact);
                }
                else
                {
                    factText = string.Concat(audioTag, givenName, "  ", GET_FACT_MESSAGE, spaceFact);
                }


                alexaResp = GetAlexaResponse(factText);

                if (!isPermissionGiven)
                {
                    AddPermissionRequest(alexaResp);
                }
                break;

            case SpaceRequestType.Help:
                alexaResp = GetAlexaResponse(HELP_MESSAGE, HELP_REPROMPT);
                break;

            case SpaceRequestType.Stop:
                alexaResp = GetAlexaResponse(STOP_MESSAGE);
                break;
            }

            return(new OkObjectResult(alexaResp));
        }