Пример #1
0
        private static CrunchResponse FindOneToManyWithVariance(List <CrunchItem <Transaction> > bankItems, List <CrunchItem <Transaction> > glItems)
        {
            CrunchRequest <Transaction> request = new CrunchRequest <Transaction>();

            request.One    = glItems.ToArray();
            request.Two    = bankItems.ToArray();
            request.Filter = new Filter()
            {
                Expression = @"$1.AccountIdentification == $2.AccountIdentification AND $1.TransactionValueDate == $2.TransactionValueDate AND 
                $1.TransactionType == $2.TransactionType AND $1.AccountServingInstitution == $2.AccountServingInstitution"
            };
            request.Crunch = new Core.Entities.Crunch()
            {
                Type = "1ToMany", Direction = "$1To$2", FieldName = "Amount", CreateVariance = true, VarianceOperator = "<", VarianceValue = 20M
            };
            request.FieldTypes = typeof(Transaction).GetProperties()
                                 .SelectMany(p => p.GetCustomAttributes(typeof(JsonPropertyAttribute), false).Cast <JsonPropertyAttribute>())
                                 .ToDictionary(p => p.PropertyName, p => p.PropertyName == "Amount" ? "Decimal" : "String");

            CrunchResponse response = Task.Run(async() =>
            {
                ApiClient api = new ApiClient();

                return(await api.SendAsync <CrunchRequest <Transaction>, CrunchResponse>(request, "/api/1.0/crunch/run"));
            }).GetAwaiter().GetResult();

            return(response);
        }
Пример #2
0
        private static CrunchResponse BuildResponse(IOutputSpeech outputSpeech, bool shouldEndSession, Session sessionAttributes, Reprompt reprompt, ICard card)
        {
            CrunchResponse response = new CrunchResponse {
                Version = "1.0"
            };

            if (sessionAttributes != null)
            {
                response.SessionAttributes = sessionAttributes.Attributes;
            }

            ResponseBody body = new ResponseBody
            {
                ShouldEndSession = shouldEndSession,
                OutputSpeech     = outputSpeech
            };

            if (reprompt != null)
            {
                body.Reprompt = reprompt;
            }
            if (card != null)
            {
                body.Card = card;
            }

            response.Response = body;

            return(response);
        }
Пример #3
0
        /// <summary>
        /// Creates a SkillResponse based on an Activity and original SkillRequest.
        /// </summary>
        /// <param name="mergedActivityResult">The Merged Activity Result to use to create the SkillResponse</param>
        /// <param name="alexaRequest">Original SkillRequest received from Alexa Skills service. This is used
        /// to check if the original request was a SessionEndedRequest which should not return a response.</param>
        /// <returns>SkillResponse</returns>
        public CrunchResponse ActivityToResponse(MergedActivityResult mergedActivityResult, CrunchRequest alexaRequest)
        {
            var response = new CrunchResponse()
            {
                Version  = "1.0",
                Response = new ResponseBody()
            };

            var activity = mergedActivityResult?.MergedActivity;

            if (activity == null || activity.Type != ActivityTypes.Message)
            {
                response.Response.ShouldEndSession = true;
                response.Response.OutputSpeech     = new PlainTextOutputSpeech
                {
                    Text = string.Empty
                };
                return(response);
            }

            if (!string.IsNullOrEmpty(activity.Speak))
            {
                response.Response.OutputSpeech = new SsmlOutputSpeech(activity.Speak);
            }
            else
            {
                response.Response.OutputSpeech = new PlainTextOutputSpeech(activity.Text);
            }

            ProcessActivityAttachments(activity, response);

            if (ShouldSetEndSession(response))
            {
                // If end of conversation was flagged use that, othwerwise look at the InputHint.
                if (mergedActivityResult.EndOfConversationFlagged)
                {
                    response.Response.ShouldEndSession = true;
                }
                else
                {
                    switch (activity.InputHint)
                    {
                    case InputHints.IgnoringInput:
                        response.Response.ShouldEndSession = true;
                        break;

                    case InputHints.ExpectingInput:
                        response.Response.ShouldEndSession = false;
                        response.Response.Reprompt         = new Reprompt(activity.Text);
                        break;

                    default:
                        response.Response.ShouldEndSession = _options.ShouldEndSessionByDefault;
                        break;
                    }
                }
            }

            return(response);
        }
Пример #4
0
        /// <summary>
        /// Processes any attachments on the Activity in order to amend the SkillResponse appropriately.
        /// The current process for processing activity attachments is;
        /// 1. Check for an instance of a SigninCard. Set the Card property on the SkillResponse to a LinkAccountCard.
        /// 2. If no SigninCard is found, check for an instance of a HeroCard. Transform the first instance of a HeroCard
        /// into an Alexa Card and set the Card property on the response.
        /// 3. If no Signin or HeroCard instances were found, check for Alexa specific CardAttachment and
        /// set the content of the Card property on the response.
        /// 4. Look for any instances of DirectiveAttachments and add the appropriate directives to the response.
        /// </summary>
        /// <param name="activity">The Activity for which to process activities.</param>
        /// <param name="response">The SkillResponse to be modified based on the attachments on the Activity object.</param>
        private void ProcessActivityAttachments(Activity activity, CrunchResponse response)
        {
            var bfCard = activity.Attachments?.FirstOrDefault(a => a.ContentType == HeroCard.ContentType || a.ContentType == SigninCard.ContentType);

            if (bfCard != null)
            {
                switch (bfCard.Content)
                {
                case SigninCard signinCard:
                    response.Response.Card = new LinkAccountCard();
                    break;

                case HeroCard heroCard:
                    response.Response.Card = CreateAlexaCardFromHeroCard(heroCard);
                    break;
                }
            }
            else
            {
                var cardAttachment = activity.Attachments?.FirstOrDefault(a => a.ContentType == AlexaAttachmentContentTypes.Card);
                if (cardAttachment != null)
                {
                    response.Response.Card = cardAttachment.Content as ICard;
                }
            }

            var directiveAttachments = activity.Attachments?.Where(a => a.ContentType == AlexaAttachmentContentTypes.Directive).ToList();

            if (directiveAttachments != null && directiveAttachments.Any())
            {
                response.Response.Directives = directiveAttachments.Select(d => d.Content as IDirective).ToList();
            }
        }
Пример #5
0
        /// <summary>
        /// Under certain circumstances, such as the inclusion of certain types of directives
        /// on a response, should force the 'ShouldEndSession' property not be included on
        /// an outgoing response. This method determines if this property is allowed to have
        /// a value assigned.
        /// </summary>
        /// <param name="response">Boolean indicating if the 'ShouldEndSession' property can be populated on the response.'</param>
        /// <returns>bool</returns>
        private bool ShouldSetEndSession(CrunchResponse response)
        {
            if (response.Response.Directives.Any(d => d is IEndSessionDirective))
            {
                return(false);
            }

            return(true);
        }
Пример #6
0
        private static CrunchResponse RunTemplate(List <CrunchItem <Transaction> > bankItems, List <CrunchItem <Transaction> > glItems)
        {
            CrunchRequest <Transaction> request = new CrunchRequest <Transaction>();

            request.One = glItems.ToArray();
            request.Two = bankItems.ToArray();

            // In case you want to run a template with multiple rules set the TemplateId property to the Id of the template.
            // Otherwise if you run a single rule set the RuleId property. You can either have the TemplateId or the RuleId set at any given time not both.
            request.TemplateId = 2;

            CrunchResponse response = Task.Run(async() =>
            {
                ApiClient api = new ApiClient();

                return(await api.SendAsync <CrunchRequest <Transaction>, CrunchResponse>(request, "/api/1.0/crunch/run"));
            }).GetAwaiter().GetResult();

            return(response);
        }