从 Chain.From() 清除机器人状态数据。Do() 返回"Sorry, my bot code is having an issue"



试图在此代码中设置用户数据保留返回的"对不起,我的bot代码正在遇到问题"

internal static IDialog<SandwichOrder> MakeRootDialog()
    {
        return Chain.From(() => FormDialog.FromForm(SandwichOrder.BuildForm))
            .Do(async (context, order) =>
            {
                try
                {
                    var activity = context.Activity;
                    var channelId = activity.ChannelId;
                    var fromId = activity.From.Id;
                    var stateClient = activity.GetStateClient();
                    **var userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);
                    // modify a property within user data 
                    userData.SetProperty<string>("SerialNumber", "");
                    // save updated user data
                    await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);**
                    var completed = await order;
                    // Actually process the sandwich order...
                    await context.PostAsync("Defect data submitted!");
                }
                catch (HttpOperationException err)
                {
                    // handle error with HTTP status code 412 Precondition Failed
                }
                catch (FormCanceledException<SandwichOrder> e)
                {
                    string reply;
                    if (e.InnerException == null)
                    {
                        reply = $"You quit on {e.Last}--maybe you can finish next time!";
                    }
                    else
                    {
                        reply = "Sorry, I've had a short circuit.  Please try again.";
                    }
                    await context.PostAsync(reply);
                }
            });
    }

它实际上在工作并清除属性,但我不想看到该错误消息

而不是使用activation.getStateclient(),使用对话框上下文的方法来操纵对话框方法内部的状态。

正在加载对话框并在对话框完成时自动保存时,发生的情况是应对状态进行序列化。如果您在对话框中手动创建状态客户端并操纵状态,则在自动保存发生时会发生冲突。


编辑:这正如预期的。

在控制器中:

var stateClient = activity.GetStateClient(); // this is the default state client (recommended only for prototyping)
var userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);
userData.SetProperty<string>("SerialNumber", "test serial number");
await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);
await Conversation.SendAsync(activity, TestForm.MakeRootDialog);
userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id); //after the form completes, SerialNumber is empty here

表格:

public class TestForm
{
    internal static IDialog<SandwichOrder> MakeRootDialog()
    {
        return Chain.From(() => FormDialog.FromForm(SandwichOrder.BuildForm))
            .Do(async (context, order) =>
            {
                    var serialNumber = context.UserData.GetValue<string>("SerialNumber");
                    context.UserData.SetValue("SerialNumber", "");
                    var completed = await order;
                    // Actually process the sandwich order...
                    await context.PostAsync("Defect data submitted!");                    
            });
    }
}

相关内容

最新更新