2 回答

TA貢獻1831條經驗 獲得超10個贊
注意:Basic Bot 示例已被 Core Bot 示例替換,因此此答案已過時
查看在示例存儲庫中或通過在 Azure 中從模板創建基本機器人來設置基本機器人的方式。
狀態屬性訪問器在BasicBot
類中聲明:
private readonly IStatePropertyAccessor<GreetingState> _greetingStateAccessor;
然后在BasicBot
構造函數中將其分配給:
_greetingStateAccessor = _userState.CreateProperty<GreetingState>(nameof(GreetingState));
然后將其傳遞給GreetingDialog
構造函數:
Dialogs.Add(new GreetingDialog(_greetingStateAccessor, loggerFactory));
然后將其分配給GreetingDialog
類的屬性:
UserProfileAccessor = userProfileStateAccessor ?? throw new ArgumentNullException(nameof(userProfileStateAccessor));
然后在整個GreetingDialog
類的許多地方使用GetAsync
和SetAsync
方法。例如:
var greetingState = await UserProfileAccessor.GetAsync(stepContext.Context, () => null);

TA貢獻1735條經驗 獲得超5個贊
瀑布對話框的最后兩個步驟可能如下所示:
public async Task<DialogTurnResult> AskForLocation(WaterfallStepContext sc, CancellationToken cancellationToken)
{
// the previous step asked for the Email so now bot is going to save it in botstate
_state = await _accessor.GetAsync(sc.Context, () => new MyApplicationState());
var email = _state.Email = (string)sc.Result;
// this is not in the template because it is saving in a different manner
// just being explicit about saving here
await _accessor.SetAsync(sc.Context, _state);
await sc.Context.SendActivityAsync("Got your email!");
var prompt = new PromptOptions
{
Prompt = MessageFactory.Text($"Please specify location."),
};
return await stepContext.PromptAsync(locationPrompt, prompt);
}
public async Task<DialogTurnResult> FinishDialog(WaterfallStepContext sc, CancellationToken cancellationToken)
{
_state = await _accessor.GetAsync(sc.Context);
_state.Location = (string)sc.Result;
// save location this time
await _accessor.SetAsync(sc.Context, _state);
await sc.Context.SendActivityAsync("Got your location!");
return await sc.EndDialogAsync();
}
如果您退出了上面的對話框,并假設您實現了 StateBotAccessors并正在使用 UserProfile 屬性,那么您可以通過以下方式檢索它:
var _state = await stateBotAccessors.UserState.UserProfileAccessor.GetAsync(context);
或者,如果您想從子對話框中傳遞它,您可以以以下方式結束:
return await sc.EndDialogAsync(_state);
- 2 回答
- 0 關注
- 162 瀏覽
添加回答
舉報