我在使用MS bot框架构建的机器人中有一个子对话框,其开头如下 - 标准方式:
public async Task StartAsync(IDialogContext context)
{
var msg = "Let's find your flights! Tell me the flight number, city or airline.";
var reply = context.MakeMessage();
reply.Text = msg;
//add quick replies here
await context.PostAsync(reply);
context.Wait(UserInputReceived);
}
使用两种不同的方式调用此对话框,具体取决于在上一个屏幕中用户是否点击了一个按钮,其中显示了" Flights"或立即输入航班号。以下是父对话框中的代码:
else if (response.Text == MainOptions[2]) //user tapped a button
{
context.Call(new FlightsDialog(), ChildDialogComplete);
}
else //user entered some text instead of tapping a button
{
await context.Forward(new FlightsDialog(), ChildDialogComplete,
activity, CancellationToken.None);
}
问题:我如何知道(来自FlightsDialog
)是否使用context.Call()
或context.Forward()
调用了该对话框?这是因为在context.Forward()
的情况下,StartAsync()
不应该输出要求用户输入航班号的提示 - 他们已经这样做了。
我最好的想法是在ConversationData
或用户数据中保存一个标志,如下所示,并从IDialog访问它,但我认为可能有更好的方法?
public static void SetUserDataProperty(Activity activity, string PropertyName, string ValueToSet)
{
StateClient client = activity.GetStateClient();
BotData userData = client.BotState.GetUserData(activity.ChannelId, activity.From.Id);
userData.SetProperty<string>(PropertyName, ValueToSet);
client.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);
}
答案 0 :(得分:4)
不幸的是Forward
实际上会调用Call
(然后再执行其他操作),因此您的对话框无法区分。
void IDialogStack.Call<R>(IDialog<R> child, ResumeAfter<R> resume)
{
var callRest = ToRest(child.StartAsync);
if (resume != null)
{
var doneRest = ToRest(resume);
this.wait = this.fiber.Call<DialogTask, object, R>(callRest, null, doneRest);
}
else
{
this.wait = this.fiber.Call<DialogTask, object>(callRest, null);
}
}
async Task IDialogStack.Forward<R, T>(IDialog<R> child, ResumeAfter<R> resume, T item, CancellationToken token)
{
IDialogStack stack = this;
stack.Call(child, resume);
await stack.PollAsync(token);
IPostToBot postToBot = this;
await postToBot.PostAsync(item, token);
}