示例#1
0
        private AmazonResponse CreateUnauthorizedResponse()
        {
            AmazonResponse response = CreateGenericResponse();

            response.response.outputSpeech.text = "You are not authorized to use this service";
            return(response);
        }
示例#2
0
        private AmazonResponse CreateDidNotUnderstandResponse()
        {
            AmazonResponse response = CreateGenericResponse();

            response.response.outputSpeech.text = "I am sorry, I did not understand this command. Please try again.";
            return(response);
        }
示例#3
0
        private AmazonIntent ValidateIfRequestIsComplete(ref AmazonRequest request, ref AmazonResponse response)
        {
            AmazonIntent intent = ((AmazonIntentRequest)request.request).intent;

            if (!intent.name.Contains("Incomplete") && !intent.name.Contains("FollowUp"))
            {
                intent.RemoveNullValueSlots();
                List <RequiredSlotWithPrompt> reqSlots = siteSettings.FindRequiredSlotsByIntent(intent.name);
                if (reqSlots != null)
                {
                    intent.SortSlots(reqSlots);
                }
                return(intent);
            }
            else
            {
                RequiredSlotWithPrompt slWithPrompt = DetermineNextMissingSlot(ref request, ref response);
                if (slWithPrompt != null)
                {
                    response.response.outputSpeech.text = "OK. " + slWithPrompt.VoicePrompt;
                    response.response.shouldEndSession  = false;
                }
                else
                {
                    return(intent);
                }
            }
            return(null);
        }
示例#4
0
        private AmazonResponse CreateAuthorizedResponse()
        {
            AmazonResponse response = CreateGenericResponse();

            response.response.outputSpeech.text = "Done";
            return(response);
        }
示例#5
0
        public IActionResult ReceiveCommand([FromBody] JObject obj)
        {
            JsonSerializer ser = new JsonSerializer();

            ser.Converters.Add(new AmazonRequestObjectConverter());
            AmazonRequest request = obj.ToObject <AmazonRequest>(ser);

            if (!IsUserAuthorized(request))
            {
                return(new ObjectResult(CreateUnauthorizedResponse()));
            }

            if (request.request.type == "IntentRequest")
            {
                AmazonResponse response = CreateAuthorizedResponse();
                AmazonIntent   intent   = ((AmazonIntentRequest)request.request).intent;
                string         responseStr;

                if (intent.name.StartsWith("QueryState"))
                {
                    responseStr = SendStateVariableQuery(intent);
                }
                else
                {
                    responseStr = SendCommand(request, ref response);
                }
                if (responseStr != null)
                {
                    response.response.outputSpeech.text = responseStr;
                }
                return(new ObjectResult(response));
            }
            return(new ObjectResult(CreateDidNotUnderstandResponse()));
        }
示例#6
0
        private string SendCommand(AmazonRequest request, ref AmazonResponse response)
        {
            ReceivedCommand command = ConvertRequestToCommand(request, ref response);
            string          str     = null;

            if (command != null)
            {
                str = senderHelper.PushRemoteCommand(command, null);
            }
            return(str);
        }
示例#7
0
        private AmazonResponse CreateGenericResponse()
        {
            AmazonResponse response = new AmazonResponse();

            response.version  = "1.0";
            response.response = new AmazonResponseObject();
            response.response.outputSpeech      = new AmazonOutputSpeech();
            response.response.outputSpeech.type = "PlainText";
            response.response.shouldEndSession  = true;
            return(response);
        }
示例#8
0
        /// <summary>
        /// Executes web request with given uri and keywords in async way.
        /// </summary>
        /// <param name="uri">Uri for a request.</param>
        /// <param name="keywords">Keywords to include in a response.</param>
        /// <returns><see cref="string"/> json value.</returns>
        public async Task <AmazonResponse> ExecuteWebRequestAsync(string uri, string keywords)
        {
            AmazonResponse responseJson = null;

            var webRequest = WebRequest.CreateHttp(uri);

            using (var webResponse = await webRequest.GetResponseAsync() as HttpWebResponse)
                using (var dataStream = webResponse.GetResponseStream())
                    using (var reader = new StreamReader(dataStream))
                    {
                        string responseFromServer = reader.ReadToEnd();
                        responseJson = GetWebResponse(keywords, responseFromServer, webResponse);
                    }

            return(responseJson);
        }
示例#9
0
        private ReceivedCommand ConvertRequestToCommand(AmazonRequest request, ref AmazonResponse response)
        {
            AmazonIntent intent = ValidateIfRequestIsComplete(ref request, ref response);

            if (intent == null)
            {
                return(null);
            }

            //Now we need to merge parameters for date and time. We also need to assign the correct
            //parameter type which can then be interpreted by the service.
            //At the moment this assumes that every time will have a date as well, but you may have a date
            //with no time
            intent.AssignParameterTypeToSlotValues();

            //The aim is to hard code as little as possible. <VOICE> prefix tells the service to look for
            //a voice prompt instead of a technical command name.
            //PAR prefix on the intent slot tells us that it needs to be a parameter to the command,
            //not a part of the voice prompt
            return(intent.ConvertIntentToCommand());
        }
示例#10
0
        public static List <AmazonItem> GetAmazonItems()
        {
            try
            {
                int page = 1;
                List <AmazonItem> amazonItems    = new List <AmazonItem>();
                AmazonResponse    amazonResponse = new AmazonResponse();

                do
                {
                    amazonResponse = SendAmazonRequest(page);
                    amazonItems.AddRange(amazonResponse.result.minimalRegistryItems);
                    page++;
                } while (amazonResponse.result.filteredItemTotal > amazonItems.Count);

                return(amazonItems);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
        }
示例#11
0
        private RequiredSlotWithPrompt DetermineNextMissingSlot(ref AmazonRequest request, ref AmazonResponse response)
        {
            AmazonIntent intent = ((AmazonIntentRequest)request.request).intent;

            //First we determine the list of required slots
            //We also need to remove the suffix
            List <RequiredSlotWithPrompt> reqSlots = siteSettings.FindRequiredSlotsByIntent(intent.name);

            //This is an incomplete request - i.e. first one issued
            //It will tell us all the other parameters we need to determine
            //First we add them all to session attributes on the response
            if (intent.name.Contains("Incomplete"))
            {
                //Slots seem to come in a completely random order so we need to arrange them before passing them
                //on
                intent.SortSlots(reqSlots);

                return(response.CreateAttributesFromSlots(intent.slots, reqSlots));
            }

            //This is a follow up request so will come with the value for next parameter(s)
            if (intent.name.Contains("FollowUp"))
            {
                foreach (KeyValuePair <string, AmazonSlot> kv in intent.slots)
                {
                    string searchStr = kv.Key.Insert(3, request.session.attributes["NextSlot"].ToString());
                    if (request.session.attributes[searchStr] == null)
                    {
                        request.session.attributes[searchStr] = kv.Value.value;
                    }
                }
            }

            //Now we need to sort the session attributes as it appears Amazon randomizes them again :(
            request.session.SortSessionAttributes(reqSlots);

            //Check if all parameters are now present
            RequiredSlotWithPrompt missingSlot = request.session.CheckIfAllSlotsArePresent(reqSlots);

            if (missingSlot != null)
            {
                response.CopySessionAttributesFromRequest(request);
                return(missingSlot);
            }

            //First we need to combine all the currently known slots from session
            List <AmazonSlot> slotsPresent = request.session.ConvertSessionAttributesToSlots();

            //We will replace slots in the main intent object witht the slots we found
            intent.ReplaceSlots(slotsPresent);

            //Now we need to sort them in the order in which they are defined in the configuration file
            intent.SortSlots(reqSlots);

            intent.name.Remove(intent.name.IndexOf("FollowUp"));

            return(null);
        }