我有我的TaskOnTurn-方法的问题。 当我启动机器人,它发出的欢迎消息和一个新的对话框启动,因为的,
if(!turnContext.Responded)
现在,而在IM对话再次一跃而起,最后if语句和一个新的对话已经开始,而IM于一体。 请问该怎么做!turnContext.Responded工作? 然后我试图启动的对话框中,如果(turnContext.Activity.MembersAdded!= NULL)下等待SendWecomeMessage。 没有工作。 然后,它发送1个欢迎消息,然后开始2个对话框。 这也让我感到困惑。
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
var activity = turnContext.Activity;
var dc = await _dialogs.CreateContextAsync(turnContext, cancellationToken);
if (turnContext.Activity.Type == ActivityTypes.Message)
{
await dc.ContinueDialogAsync(cancellationToken);
}
else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
{
if (turnContext.Activity.MembersAdded != null)
{
await SendWelcomeMessageAsync(turnContext, cancellationToken);
}
}
else
{
await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected", cancellationToken: cancellationToken);
}
if (!turnContext.Responded)
{
await dc.BeginDialogAsync(ReservationDialog, cancellationToken);
}
}
TurnContext.Responded指示至少一个响应是否被用于当前送转。 OnTurnAsync
每一步瀑布之间的火灾,因此,如果ReservationDialog
有一个提示,一旦用户应答提示, OnTurnAsync
被解雇,因为机器人已经不是StepContext内得到回复, TurnContext.Responded
是false
。 这就是为什么你在另一个对话框中间得到一个对话框。
有不同的路线,你可以去内的很多OnTurnAsync
。 我绝对推荐检查出一些样品 ,看看他们是如何做到的事情-他们大多是相当良好注释。
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
var activity = turnContext.Activity;
var dc = await Dialogs.CreateContextAsync(turnContext);
// Execute on incoming messages
if (activity.Type == ActivityTypes.Message)
{
if (string.IsNullOrWhiteSpace(activity.Text) && activity.Value != null)
{
activity.Text = JsonConvert.SerializeObject(activity.Value);
}
}
var dialogResult = await dc.ContinueDialogAsync();
// Execute based on different situations within a Dialog. See BasicBot for examples:
// https://github.com/Microsoft/BotBuilder-Samples/blob/master/samples/csharp_dotnetcore/13.basic-bot/BasicBot.cs#L112
if (!dc.Context.Responded)
{
switch (dialogResult.Status)
{
case DialogTurnStatus.Empty:
case DialogTurnStatus.Waiting:
break;
case DialogTurnStatus.Complete:
await dc.EndDialogAsync();
break;
default:
await dc.CancelAllDialogsAsync();
break;
}
}
// Here's where we show welcome messages
if (activity.Type == ActivityTypes.ConversationUpdate)
{
if (activity.MembersAdded != null)
{
foreach (var member in activity.MembersAdded)
{
// This makes sure the new user isn't the bot. It's a little different from some of the samples
// because code has changed in the SDK and emulator
if (member.Name != "Bot" && member.Name != null)
{
await SendWelcomeMessageAsync(turnContext, cancellationToken);
await dc.BeginDialogAsync(ReservationDialog, cancellationToken);
}
}
}
}
// Be sure you're saving ConversationState at the end since DialogContext derives from it
await _conversationState.SaveChangesAsync(turnContext);
}