Exemple #1
0
        /// <summary>
        /// Execute the turn of the bot. The functionality here closely resembles that which is found in the
        /// IBot.OnTurnAsync method in an implementation that is using the regular BotFrameworkAdapter.
        /// Also here in this example the focus is explicitly on Dialogs but the pattern could be adapted
        /// to other conversation modeling abstractions.
        /// </summary>
        /// <param name="rootDialog">The dialog to be run.</param>
        /// <param name="turnContext">The ITurnContext instance to use. Note this is not the one passed into the IBot OnTurnAsync.</param>
        /// <param name="state">The existing or old state of the dialog.</param>
        /// <returns>The updated or new state of the dialog.</returns>
        private static async Task <JObject> RunTurnAsync(Dialog rootDialog, TurnContext turnContext, JObject state)
        {
            // For this example we are only interested in Message Activities.
            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                // If we have some state, deserialize it. (This mimics the shape produced by BotState.cs.)
                var dialogStateProperty = state?[nameof(DialogState)];
                var dialogState         = dialogStateProperty?.ToObject <DialogState>(StateJsonSerializer);

                // A custom accessor is used to pass a handle on the state to the dialog system.
                var accessor = new RefAccessor <DialogState>(dialogState);

                // The following is regular dialog driver code.
                var dialogs = new DialogSet(accessor);
                dialogs.Add(rootDialog);

                var dialogContext = await dialogs.CreateContextAsync(turnContext);

                var results = await dialogContext.ContinueDialogAsync();

                if (results.Status == DialogTurnStatus.Empty)
                {
                    await dialogContext.BeginDialogAsync("root");
                }

                // Serialize the result (available as Value on the accessor), and put its value back into a new JObject.
                return(new JObject {
                    { nameof(DialogState), JObject.FromObject(accessor.Value, StateJsonSerializer) }
                });
            }

            return(state);
        }
Exemple #2
0
        /// <summary>
        /// Execute the turn of the bot. The functionality here closely resembles that which is found in the
        /// IBot.OnTurnAsync method in an implementation that is using the regular BotFrameworkAdapter.
        /// Also here in this example the focus is explicitly on Dialogs but the pattern could be adapted
        /// to other conversation modeling abstractions.
        /// </summary>
        /// <param name="dialog">The dialog to be run.</param>
        /// <param name="turnContext">The ITurnContext instance to use. Note this is not the one passed into the IBot OnTurnAsync.</param>
        /// <param name="state">The existing or old state of the dialog.</param>
        /// <returns>The updated or new state of the dialog.</returns>
        private static async Task <JObject> RunTurnAsync(Dialog dialog, ITurnContext turnContext, JObject state, CancellationToken cancellationToken)
        {
            // If we have some state, deserialize it. (This mimics the shape produced by BotState.cs.)
            var dialogStateProperty = state?[nameof(DialogState)];
            var dialogState         = dialogStateProperty?.ToObject <DialogState>(StateJsonSerializer);

            // A custom accessor is used to pass a handle on the state to the dialog system.
            var accessor = new RefAccessor <DialogState>(dialogState);

            // Run the dialog.
            await dialog.Run(turnContext, accessor, cancellationToken);

            // Serialize the result (available as Value on the accessor), and put its value back into a new JObject.
            return(new JObject {
                { nameof(DialogState), JObject.FromObject(accessor.Value, StateJsonSerializer) }
            });
        }