Exemplo n.º 1
0
        /// <summary>
        /// Searches for a dialog with a given ID.
        /// Emits a named event for the current dialog, or someone who started it, to handle.
        /// </summary>
        /// <param name="name">Name of the event to raise.</param>
        /// <param name="value">Value to send along with the event.</param>
        /// <param name="bubble">Flag to control whether the event should be bubbled to its parent if not handled locally. Defaults to a value of `true`.</param>
        /// <param name="fromLeaf">Whether the event is emitted from a leaf node.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>True if the event was handled.</returns>
        public async Task <bool> EmitEventAsync(string name, object value = null, bool bubble = true, bool fromLeaf = false, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                // Initialize event
                var dialogEvent = new DialogEvent()
                {
                    Bubble = bubble,
                    Name   = name,
                    Value  = value,
                };

                var dc = this;

                // Find starting dialog
                if (fromLeaf)
                {
                    while (true)
                    {
                        var childDc = dc.Child;

                        if (childDc != null)
                        {
                            dc = childDc;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                // Dispatch to active dialog first
                var instance = dc.ActiveDialog;

                if (instance != null)
                {
                    var dialog = dc.FindDialog(instance.Id);

                    if (dialog != null)
                    {
                        await this.DebuggerStepAsync(dialog, name, cancellationToken).ConfigureAwait(false);

                        return(await dialog.OnDialogEventAsync(dc, dialogEvent, cancellationToken).ConfigureAwait(false));
                    }
                }

                return(false);
            }
            catch (Exception err)
            {
                SetExceptionContextData(err);
                throw;
            }
        }
Exemplo n.º 2
0
        protected override async Task <bool> OnPreBubbleEventAsync(DialogContext dc, DialogEvent e, CancellationToken cancellationToken)
        {
            if (e.Name == DialogEvents.ActivityReceived && dc.Context.Activity.Type == ActivityTypes.Message)
            {
                // Perform base recognition
                var state      = dc.ActiveDialog.State;
                var recognized = await OnRecognizeAsync(dc.Context, (IDictionary <string, object>) state[PersistedState], (PromptOptions)state[PersistedOptions]).ConfigureAwait(false);

                return(recognized.Succeeded);
            }

            return(false);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Called when an event has been raised, using `DialogContext.emitEvent()`, by either the current dialog or a dialog that the current dialog started.
        /// </summary>
        /// <param name="dc">The dialog context for the current turn of conversation.</param>
        /// <param name="e">The event being raised.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>True if the event is handled by the current dialog and bubbling should stop.</returns>
        public override async Task <bool> OnDialogEventAsync(DialogContext dc, DialogEvent e, CancellationToken cancellationToken)
        {
            var handled = await base.OnDialogEventAsync(dc, e, cancellationToken).ConfigureAwait(false);

            // Trace unhandled "versionChanged" events.
            if (!handled && e.Name == DialogEvents.VersionChanged)
            {
                var traceMessage = $"Unhandled dialog event: {e.Name}. Active Dialog: {dc.ActiveDialog.Id}";

                dc.Dialogs.TelemetryClient.TrackTrace(traceMessage, Severity.Warning, null);

                await dc.Context.TraceActivityAsync(traceMessage, cancellationToken : cancellationToken)
                .ConfigureAwait(false);
            }

            return(handled);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Called when an event has been raised, using `DialogContext.emitEvent()`, by either the current dialog or a dialog that the current dialog started.
        /// </summary>
        /// <param name="dc">The dialog context for the current turn of conversation.</param>
        /// <param name="e">The event being raised.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>True if the event is handled by the current dialog and bubbling should stop.</returns>
        public virtual async Task <bool> OnDialogEventAsync(DialogContext dc, DialogEvent e, CancellationToken cancellationToken)
        {
            // Before bubble
            var handled = await this.OnPreBubbleEventAsync(dc, e, cancellationToken).ConfigureAwait(false);

            // Bubble as needed
            if (!handled && e.Bubble && dc.Parent != null)
            {
                handled = await dc.Parent.EmitEventAsync(e.Name, e.Value, true, false, cancellationToken).ConfigureAwait(false);
            }

            // Post bubble
            if (!handled)
            {
                handled = await this.OnPostBubbleEventAsync(dc, e, cancellationToken).ConfigureAwait(false);
            }

            return(handled);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Called after an event was bubbled to all parents and wasn't handled.
 /// </summary>
 /// <remarks>
 /// This is a good place to perform default processing logic for an event. Returning `true` will
 /// prevent any processing of the event by child dialogs.
 /// </remarks>
 /// <param name="dc">The dialog context for the current turn of conversation.</param>
 /// <param name="e">The event being raised.</param>
 /// <param name="cancellationToken">Cancellation token.</param>
 /// <returns> Whether the event is handled by the current dialog and further processing should stop.</returns>
 protected virtual Task <bool> OnPostBubbleEventAsync(DialogContext dc, DialogEvent e, CancellationToken cancellationToken)
 {
     return(Task.FromResult(false));
 }
Exemplo n.º 6
0
 protected override async Task <bool> OnPreBubbleEventAsync(DialogContext dc, DialogEvent e, CancellationToken cancellationToken)
 {
     return(await Task.FromResult(false).ConfigureAwait(false));
 }