首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在启动时将外部状态传递给对话框?

如何在启动时将外部状态传递给对话框?
EN

Stack Overflow用户
提问于 2019-12-10 14:12:03
回答 1查看 35关注 0票数 0

例如,我有一个包含城市名称的州,我将其存储在会话状态中。

当我启动该对话框时,该对话框要求用户输入城市名称并结束该对话框。

在下一轮中,对话框再次启动,但我希望这一次对话框已经从外部作用域获得了城市名称,而不是再次询问用户。

我知道瀑布对话框可以做到这一点,每一步都可以得到最后一步的结果。但我想知道如何在对话之间实现它。

请给我看更多的细节和样品。谢谢!我的问题类似于this question

EN

回答 1

Stack Overflow用户

发布于 2019-12-10 15:28:19

每次用户输入消息时,您都需要保存您的状态。

首先在BotStateService.cs.中生成状态变量

DialogBot.cs中,每次用户输入消息时,它都会进入OnTurnAsync方法,并将您的状态保存在该方法中。

你可以试试这条路。

BotStateService.cs :

代码语言:javascript
复制
namespace CoreBot.Services
{
    public class BotStateService
    {
        //state variables
        public UserState _userState { get; }
        public ConversationState _conversationState { get; }
        public DialogState _dialogState { get; }

        //IDs
        public static string UserProfileId { get; } = $"{nameof(BotStateService)}.UserProfile";
        public static string ConversationDataId { get; } = $"{nameof(ConversationState)}.ConversationData";
        public static string DialogStateId { get; } = $"{nameof(DialogState)}.DialogState";

        //Accessors
        public IStatePropertyAccessor<UserProfile> UserProfileAccessors { get; set; }
        public IStatePropertyAccessor<ConversationData> ConversationDataAccessors { get; set; }
        public IStatePropertyAccessor<DialogState> DialogStateAccessors { get; set; }


        public BotStateService(UserState userState, ConversationState conversationState)
        {
            _userState = userState ?? throw new ArgumentNullException(nameof(userState));
            _conversationState = conversationState ?? throw new ArgumentNullException(nameof(conversationState));

            InitializeAccessors();
        }

        private void InitializeAccessors()
        {
            UserProfileAccessors = _userState.CreateProperty<UserProfile>(UserProfileId);

            ConversationDataAccessors = _conversationState.CreateProperty<ConversationData>(ConversationDataId);
            DialogStateAccessors = _conversationState.CreateProperty<DialogState>(DialogStateId);

        }
    }
}

DialogBot.cs :

代码语言:javascript
复制
    public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
            {
// Save your state each time user enters the conversation or enters a message
                await base.OnTurnAsync(turnContext, cancellationToken);
                await _botStateService._userState.SaveChangesAsync(turnContext);
                await _botStateService._conversationState.SaveChangesAsync(turnContext);
    }

GreetingDialog.cs :

代码语言:javascript
复制
    private async Task<DialogTurnResult> InitialStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
            {

                UserProfile userProfile = await _botStateService.UserProfileAccessors.GetAsync(stepContext.Context, () => new UserProfile());

                    if (String.IsNullOrEmpty(userProfile.city))
                    {
                        return await stepContext.PromptAsync($"{nameof(GreetingDialog)}.city",
                            new PromptOptions
                            {
                                Prompt = MessageFactory.Text("Please tell me name of your city.")
                            }, cancellationToken);
                    }
                    else
                    {
                        return await stepContext.NextAsync(null, cancellationToken);
                    }
            }

private async Task<DialogTurnResult> FinalStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{

UserProfile userProfile = await _botStateService.UserProfileAccessors.GetAsync(stepContext.Context, () => new UserProfile());
                if (String.IsNullOrEmpty(userProfile.city))
                {
                    userProfile.city = (String)stepContext.Result;
                    await _botStateService.UserProfileAccessors.SetAsync(stepContext.Context, userProfile);
                }

return await stepContext.EndDialogAsync(null, cancellationToken);
}

UserProfile.cs :

代码语言:javascript
复制
using System;

    namespace CoreBot.Profiles
    {
        public class UserProfile
        {

            public string city { get; set; }
        }
    }

希望这能有所帮助!如果有任何疑问,请询问。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59261215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档