如何将用户定义的详细信息发送到Bot Framework App中的活动或如何在对话框类中访问BotData

时间:2017-05-18 14:04:59

标签: bots botframework formbuilder

我有两个问题

1)如何将自定义值发送到Bot Framework中的Activity? 我在post方法

中有以下代码
UserDetails usr = new UserDetails();
usr.LoginID = l.LoginID
userData.SetProperty<string>("LoginID", usr.LoginID);
sc.BotState.SetUserData(activity.ChannelId, activity.From.Id, userData);

现在我想在Dialog / FormBuilder类中访问此属性,如何实现此目的?

以下是我的formbuilder类

[Serializable]
public class FlightBooking
{
public static IForm<FlightBooking> BuildForm()
    {
    return new FormBuilder<FlightBooking>().Message("Tell me flight details!")
    .Field(nameof(title))
    ....
    ....
    }
}

2)如何在FormBuilder / Dialog类中访问BotData用户定义的属性?

与上面的代码一样,你可以看到我设置了EmailId属性,如何在formbuilder类中访问该属性值?

1 个答案:

答案 0 :(得分:0)

1)我不明白你在使用BotState做什么。您问的问题是“如何将自定义值发送到活动...”,但实际上您将值保存到UserData。此外,您提供的代码似乎并不完整。

您可以像这样保存UserData

var stateClient = activity.GetStateClient();
BotData userData = stateClient.BotState.GetUserData(activity.ChannelId, activity.From.Id);
userData.SetProperty<string>("LoginID", l.LoginID);
stateClient.BotState.SetUserData(activity.ChannelId, activity.From.Id, userData);

2)您可以将activity.ChannelId和activity.From.Id发送到BuildForm方法:

await Conversation.SendAsync(activity, ()=> FormDialog.FromForm(() => FlightBooking.BuildForm(activity.ChannelId, activity.From.Id)));

并创建StateClient,然后访问UserData

public class FlightBooking
{
    public string title { get; set; }
    public static IForm<FlightBooking> BuildForm(string channelId, string userId)
    {
        string appId = ConfigurationManager.AppSettings["MicrosoftAppId"];
        string password = ConfigurationManager.AppSettings["MicrosoftAppPassword"];
        StateClient stateClient = new StateClient(new MicrosoftAppCredentials(appId, password));
        BotData userData = stateClient.BotState.GetUserData(channelId, userId);
        var loginId = userData.GetProperty<string>("LoginID");

        //do something with loginId?

        return new FormBuilder<FlightBooking>().Message("Tell me flight details!")
            .Field(nameof(title))
            .Build();
    }
}

但是,您也可以将整个活动对象发送到BuildForm方法,而不用担心UserData。