Example #1
0
        private async Task <AlexaResponse> GetIntentResponseAsync(AlexaRequest request)
        {
            string intentName = request?.Request?.Intent?.Name;

            if (string.IsNullOrWhiteSpace(intentName))
            {
                throw new Exception("No intent name found");
            }



            Adventure adv = await _adventureRep.GetAdventureAsync();

            AdventureNode curNode = await _curNodeRep.GetCurrentNodeAsync(request, adv.Nodes);

            string        nextNodeName = null;
            AlexaResponse resp         = null;

            if (intentName.Equals("BeginIntent", StringComparison.OrdinalIgnoreCase) || intentName.Equals(BuiltInIntents.StartOverIntent))
            {
                AdventureNode startNode = adv.GetStartNode();

                if (startNode == null)
                {
                    throw new Exception($"Start node {adv.StartNodeName} not found");
                }

                nextNodeName = startNode.Name;
                resp         = startNode.ToAlexaResponse(_linkProcessor, adv.VoiceId);
            }
            else if (intentName.Equals(BuiltInIntents.CancelIntent) || intentName.Equals(BuiltInIntents.StopIntent))
            {
                AdventureNode stopNode = adv.GetStopNode();

                if (stopNode == null)
                {
                    throw new Exception($"Start node {adv.StopNodeName} not found");
                }

                resp = stopNode.ToAlexaResponse(_linkProcessor, adv.VoiceId);
            }
            else if (intentName.Equals("ResumeIntent", StringComparison.OrdinalIgnoreCase))
            {
                resp = curNode.ToAlexaResponse(_linkProcessor, adv.VoiceId);
            }
            else if (intentName.Equals(BuiltInIntents.HelpIntent))
            {
                AdventureNode helpNode = adv.GetHelpNode();

                if (helpNode == null)
                {
                    throw new Exception($"Help node {helpNode.Name} not found");
                }

                resp = MergeNodeResponses(helpNode, curNode, adv.VoiceId);
            }
            else if (intentName.Equals(BuiltInIntents.FallbackIntent))
            {
                resp = GenerateFallbackResponse(adv, curNode);
            }
            else
            {
                if (curNode != null)
                {
                    // Process the route

                    NodeRoute selectedRoute = curNode.NodeRoutes.FirstOrDefault(x => x.IntentName.Equals(intentName, StringComparison.OrdinalIgnoreCase));

                    if (selectedRoute != null)
                    {
                        nextNodeName = selectedRoute.NextNodeName;

                        if (!string.IsNullOrWhiteSpace(nextNodeName))
                        {
                            AdventureNode nextNode = adv.GetNode(nextNodeName);

                            if (nextNode != null)
                            {
                                resp = nextNode.ToAlexaResponse(_linkProcessor, adv.VoiceId);
                            }
                            else
                            {
                                _logger.LogWarning($"Next node {nextNodeName} on node {curNode} not found for intent {intentName}");
                            }
                        }
                        else
                        {
                            _logger.LogWarning($"Node name missing for node route for {intentName} provided on node {curNode.Name}");
                        }
                    }
                    else
                    {
                        resp = GenerateFallbackResponse(adv, curNode);
                        // unsupported intent. Send reprompt.
                        _logger.LogWarning($"Node route not found for {intentName} provided on node {curNode.Name}");
                    }
                }
                else
                {
                    resp = GenerateFallbackResponse(adv, null);
                    // unsupported intent. Send reprompt.
                    _logger.LogWarning($"Node {curNode.Name} on session attribute {AdventureNode.CURNODE_ATTRIB} not found");
                }
            }

            // If the next node is known, then set it. Otherwise, keep the user on the current node.
            if (string.IsNullOrWhiteSpace(nextNodeName))
            {
                if (curNode != null)
                {
                    await _curNodeRep.SetCurrentNodeAsync(request, resp, curNode.Name);
                }
            }
            else
            {
                await _curNodeRep.SetCurrentNodeAsync(request, resp, nextNodeName);
            }

            return(resp);
        }