如何从不同对话框访问保存在bot.cs中的Bot状态

时间:2019-07-18 13:43:38

标签: c# botframework

我用bot.builder 4.4.3创建了bot。我正在使用对话访问器以Bot状态存储来自Directline的网站的url。我试图使用对话访问器从一个对话框中访问它,但无法访问它。

这是我从bot.cs文件中的直线获得价值并将其存储在对话状态中的方式

public AdvisorBot(BotServices services, UserState userState, ConversationState conversationState, ILoggerFactory loggerFactory)
    {
        _services = services ?? throw new ArgumentNullException(nameof(services));
        _userState = userState ?? throw new ArgumentNullException(nameof(userState));
        _conversationState = conversationState ?? throw new ArgumentNullException(nameof(conversationState));

        _adviseStateAccessor = _conversationState.CreateProperty<AdviseState>(nameof(AdviseState));
        _dialogStateAccessor = _conversationState.CreateProperty<DialogState>(nameof(DialogState));

        Dialogs = new DialogSet(_dialogStateAccessor);
        Dialogs.Add(new AdviseDialog(_adviseStateAccessor,loggerFactory));
    }

public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken)
    {
        Activity activity = turnContext.Activity;

        // Create a dialog context
        DialogContext dc = await Dialogs.CreateContextAsync(turnContext);


        if (activity.Name == "country")
        {
            AdviseState adviseState = new AdviseState();
            dynamic deserializedData = JsonConvert.DeserializeObject(activity.Value.ToString());
            JObject jobject = deserializedData;
            var url= jobject["url"];

            adviseState.Country = url.ToString();
            await _adviseStateAccessor.SetAsync(turnContext, adviseState);

        }

在AdviseDialog.cs中,这就是我试图从Bot State提取值的方式。

public  AdviseDialog(IStatePropertyAccessor<AdviseState> adviseStateAccessor, ILoggerFactory loggerFactory)
        : base(nameof(AdviseDialog))
    {
        AdviseAccessor = adviseStateAccessor ?? throw new ArgumentNullException(nameof(adviseStateAccessor));

        // Add control flow dialogs
        WaterfallStep[] waterfallSteps = new WaterfallStep[]
         {
                 InitializeStateStepAsync,
                 // water fall steps
         };
        AddDialog(new WaterfallDialog(BrushDialog, waterfallSteps));

    }

    public IStatePropertyAccessor<AdviseState> AdviseAccessor { get; }

    private async Task<DialogTurnResult> InitializeStateStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        AdviseState adviseState = await AdviseAccessor.GetAsync(stepContext.Context, () => null);
        if (adviseState == null)
        {
            AdviseState adviseStateOpt = stepContext.Options as AdviseState;
            if (adviseStateOpt != null)
            {
                await AdviseAccessor.SetAsync(stepContext.Context, adviseStateOpt);
            }
            else
            {
                await AdviseAccessor.SetAsync(stepContext.Context, new AdviseState());
            }
        }

        baseUrl = adviseState.Country;

        return await stepContext.NextAsync();
    }

0 个答案:

没有答案