예제 #1
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "*", WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info("BeardBrosTrivia function running.");

            var          validator = new RequestValidator(log);
            bool         test      = false;
            AlexaRequest alexaRequest;

            log.Info($"Querystring: {req.RequestUri.Query}");

            // Check if the querystring contains test=true
            if (req.RequestUri.Query.IndexOf("test=true", StringComparison.OrdinalIgnoreCase) > -1)
            {
                test = true;
            }

            if (!test)
            {
                try
                {
                    alexaRequest = await validator.ParseAndValidate(req);
                }
                catch (RequestValidationException ex)
                {
                    log.Error("Error validating request.", ex);

                    throw ex;
                }

                // TODO: If your skill supports multiple different intents, use the alexaRequest variable to determine which intent has been requested.
            }
            else
            {
                log.Info("Running function in test mode.");
            }

            Quote  quote         = GetRandomArrayValue(Constants.Quotes);
            string quoteIntro    = GetRandomArrayValue(Constants.QuoteIntroSuffixes.Concat(Constants.QuoteIntroPrefixes));
            bool   introIsSuffix = Constants.QuoteIntroSuffixes.Contains(quoteIntro);
            string by            = quote.Author;
            string message;

            // Choose a fancy name 3/10 times
            if (new Random().Next(0, 10) <= 3)
            {
                switch (by.ToLower())
                {
                case "alex":
                    by = GetRandomArrayValue(Constants.AlexFancyNames);
                    break;

                case "jirard":
                    by = GetRandomArrayValue(Constants.JirardFancyNames);
                    break;
                }
            }

            if (introIsSuffix)
            {
                message = $"{ by} { quoteIntro}";
            }
            else
            {
                message = $"{ quoteIntro} { by}";
            }

            message += $". \"{quote.Text}\"";

            // Replace "Jirard" with "Gerard" to get Alexa to pronounce it correctly
            message = new Regex("jirard", RegexOptions.IgnoreCase | RegexOptions.Multiline).Replace(message, "Gerard");

            var alexa = new MessageBuilder().SetPlainSpeech(message.Trim());

            return(new HttpResponseMessage()
            {
                Content = new StringContent(alexa.BuildMessageJson()),
            });

            // return req.CreateResponse(HttpStatusCode.OK, alexa.BuildMessageJson(), new MediaTypeHeaderValue("application/json"));
        }