Example #1
0
        public override async Task <DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (options is CancellationToken)
            {
                throw new ArgumentException($"{nameof(options)} cannot be a cancellation token");
            }

            var handled = false;

            if (eventValue != null)
            {
                var(value, valueError) = this.eventValue.TryEvaluate(dc.GetState());
                if (valueError == null)
                {
                    handled = await dc.EmitEventAsync(EventName, value, BubbleEvent, false, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    throw new Exception($"Expression evaluation resulted in an error. Expression: {eventValue.ToString()}. Error: {valueError}");
                }
            }
            else
            {
                handled = await dc.EmitEventAsync(EventName, EventValue, BubbleEvent, false, cancellationToken).ConfigureAwait(false);
            }

            return(await dc.EndDialogAsync(handled, cancellationToken).ConfigureAwait(false));
        }
        /// <summary>
        /// Called when the dialog is started and pushed onto the dialog stack.
        /// </summary>
        /// <param name="dc">The <see cref="DialogContext"/> for the current turn of conversation.</param>
        /// <param name="options">Optional, initial information to pass to the dialog.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public override async Task <DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (options is CancellationToken)
            {
                throw new ArgumentException($"{nameof(options)} cannot be a cancellation token");
            }

            if (Disabled != null && Disabled.GetValue(dc.State))
            {
                return(await dc.EndDialogAsync(cancellationToken : cancellationToken).ConfigureAwait(false));
            }

            bool   handled;
            var    eventName   = EventName?.GetValue(dc.State);
            var    bubbleEvent = BubbleEvent.GetValue(dc.State);
            object value       = null;

            if (EventValue != null)
            {
                value = this.EventValue.GetValue(dc.State);
            }

            if (dc.Parent != null)
            {
                handled = await dc.Parent.EmitEventAsync(eventName, value, bubbleEvent, false, cancellationToken).ConfigureAwait(false);
            }
            else
            {
                handled = await dc.EmitEventAsync(eventName, value, bubbleEvent, false, cancellationToken).ConfigureAwait(false);
            }

            return(await dc.EndDialogAsync(handled, cancellationToken).ConfigureAwait(false));
        }
Example #3
0
        public async Task <DialogTurnResult> ProcessDefaultResponse(DialogContext context, object data, bool isBusy)
        {
            //Get Top QnA result
            var results = await Services.LeadQualQnA.GetAnswersAsync(context.Context, Services.QnAOptions);

            var topResult = results.FirstOrDefault();


            if (topResult != null && topResult.Score > 0.5)
            {
                //Convert Metadata tags to dictionary for comparison
                var resultTags = topResult.Metadata.ToDictionary(m => m.Name.ToLower(), m => m.Value);
                if (resultTags.ContainsKey("event"))
                {
                    //emit arbitrary events based on an "event" metadata record
                    await context.EmitEventAsync(resultTags["event"]);
                }
                else if (resultTags.ContainsKey("card"))
                {
                    //emit card display event, based on the value of the "card" tag, if present
                    //this causes the card to be displayed "independently" through the custom event handler
                    if (Constants.DialogEventTriggers.ContainsKey(resultTags["card"]))
                    {
                        //Remap the card event value from it's shorthand form, so we can reuse the residual interest logic
                        //eg. ('financing') to full name ('Explore Financing')
                        await SendInterest(context, Constants.DialogEventTriggers[resultTags["card"]]);
                    }
                }

                //We always send the response from QnA
                await context.Context.SendActivityAsync(MessageFactory.Text(topResult.Answer));

                //and since the user has asked for a specific intent, we don't wait for further input. Call
                //the turn complete
                return(new DialogTurnResult(DialogTurnStatus.Complete));
            }
            else
            {
                if (isBusy)
                {
                    //If we are in a dialog, we want to 'Complete' to resume the let the dialog itself take control
                    return(new DialogTurnResult(DialogTurnStatus.Complete));
                }
                else
                {
                    //Otherwise a user entered an invalid root-level


                    if (topResult != null)
                    {
                        Console.WriteLine($"Rejected low-confidence response to text '{context.Context.Activity.Text}' - [{topResult.Score}] {topResult.Source}:'{topResult.Answer}'");
                    }
                }
            }

            await context.Context.SendActivityAsync(MessageFactory.Text("I'm not quite sure what you meant..."));

            return(new DialogTurnResult(DialogTurnStatus.Waiting));
        }
Example #4
0
        public async Task <DialogTurnResult> PerformIntent(string Intent, RecognizerResult result, DialogContext context)
        {
            switch (Intent)
            {
            case "CheckInventory":
                await SendInterest(context, Constants.INTEREST_Inventory);

                break;

            case "FindVehicle":
                await SendInterest(context, Constants.INTEREST_Inventory);

                break;

            case "GetFinanced":
                await SendInterest(context, Constants.INTEREST_Financing);

                break;

            case "Utilities_Cancel":
                await context.EmitEventAsync(Constants.Event_Cancel);

                break;

            case "Utilities_GoBack":
                await context.EmitEventAsync(Constants.Event_Cancel);

                break;

            case "Utilities_Reject":
                await context.EmitEventAsync(Constants.Event_Reject);

                break;

            case "ValueTrade":
                await SendInterest(context, Constants.INTEREST_TradeIn);

                break;

            default:
                //New intent?
                break;
            }

            return(new DialogTurnResult(DialogTurnStatus.Complete, null));
        }
Example #5
0
        public override async Task <DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (options is CancellationToken)
            {
                throw new ArgumentException($"{nameof(options)} cannot be a cancellation token");
            }

            var eventValue = (this.EventValue != null) ? dc.State.GetValue <object>(this.EventValue) : null;
            var handled    = await dc.EmitEventAsync(EventName, eventValue, BubbleEvent, false, cancellationToken).ConfigureAwait(false);

            return(await dc.EndDialogAsync(handled, cancellationToken).ConfigureAwait(false));
        }
Example #6
0
        public override async Task <DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (options is CancellationToken)
            {
                throw new ArgumentException($"{nameof(options)} cannot be a cancellation token");
            }

            var dcState = dc.GetState();

            if (this.Disabled != null && this.Disabled.GetValue(dcState) == true)
            {
                return(await dc.EndDialogAsync(cancellationToken : cancellationToken).ConfigureAwait(false));
            }

            bool handled;
            var  eventName   = EventName?.GetValue(dcState);
            var  bubbleEvent = BubbleEvent.GetValue(dcState);

            if (EventValue != null)
            {
                var(value, valueError) = this.EventValue.TryGetValue(dcState);
                if (valueError == null)
                {
                    handled = await dc.EmitEventAsync(eventName, value, bubbleEvent, false, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    throw new Exception($"Expression evaluation resulted in an error. Expression: {EventValue.ToString()}. Error: {valueError}");
                }
            }
            else
            {
                handled = await dc.EmitEventAsync(eventName, EventValue, bubbleEvent, false, cancellationToken).ConfigureAwait(false);
            }

            return(await dc.EndDialogAsync(handled, cancellationToken).ConfigureAwait(false));
        }
Example #7
0
 private async Task SendInterest(DialogContext Context, string InterestName)
 {
     Context.GetState().SetValue("conversation.interest", InterestName);
     await Context.EmitEventAsync(Constants.Event_Interest);
 }