如何在Microsoft Bot框架中创建新的IDialogContext实例?

时间:2017-04-11 09:43:10

标签: c# frameworks bots botframework luis

我需要重置或终止(使用context.Done)当前的IDialogContext实例并创建一个新实例并在另一个函数中使用它。如何创建这样的实例,IDialogContext是一个接口。有可能吗?

这是我尝试调用函数“selfredirect”的函数

private async Task ApprovalConfirm(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
xxxticket xxxticketobject = new xxxticket();
if (message.Text.Contains("yes"))
{
    xxxticketobject = context.ConversationData.Get<xxxticket>("xxxcard");
    if (xxxticketobject.action == "Approve")
        await context.PostAsync("Accepted");
    else
        await context.PostAsync("Rejected");
    context.ConversationData.RemoveValue("xxxfinalcard");
    context.Reset();// *This is what I want to do...clear context and then call the following function*
    await SelfRedirect(context, context.UserData.Get<string>("CurrentQuery"), "Approval");
}
else if (message.Text == "no")
{
    await context.PostAsync("Thank you");
    context.Done(context);
}
else
    await SelfRedirect(context, context.UserData.Get<string>("CurrentQuery"), null);

}

自我重定位功能

  public static async Task SelfRedirect(IDialogContext context, string msg, string intent=null)
    {
        try
        {
            DialogLuis LUISDialog = new DialogLuis();
            IMessageActivity message = context.MakeMessage();
            message.Text = msg;
            var tasks = LUISDialog.services.Select(s => s.QueryAsync(message.Text, context.CancellationToken)).ToArray();
            var results = await Task.WhenAll(tasks);
            var winners = from result in results.Select((value, index) => new { value, index })
                          let resultWinner = LUISDialog.BestIntentFrom(result.value)
                          where resultWinner != null
                          select new LuisServiceResult(result.value, resultWinner, LUISDialog.services[result.index]);
            if (intent != null)
            {
                winners = from result in results.Select((value, index) => new { value, index })
                          let resultWinner = new IntentRecommendation(intent)
                          where resultWinner != null
                          select new LuisServiceResult(result.value, resultWinner, LUISDialog.services[result.index]);
            }
            var winner = LUISDialog.BestResultFrom(winners);
            if (winner == null)
            {
                throw new InvalidOperationException("No winning intent selected from Luis results.");
            }
            if (winner.Result.Dialog?.Status == DialogResponse.DialogStatus.Question)
            {
                var childDialog = await LUISDialog.MakeLuisActionDialog(winner.LuisService,
                                                             winner.Result.Dialog.ContextId,
                                                             winner.Result.Dialog.Prompt);
                context.Call(childDialog, LUISDialog.LuisActionDialogFinished);
            }
            else
            {
                IAwaitable<IMessageActivity> item = null;
                await LUISDialog.DispatchToIntentHandler(context, item, winner.BestIntent, winner.Result);
            }
        }
        catch (Exception ex) { CommonMethods.LogTime("", ex, context); }
    }

但由于堆栈为空,我无法在重置后使用重定向功能。请帮忙

让我详细说明为什么我必须通过这个小函数使用selfredirect函数而不是context.wait(MessageReceived)

private async Task ApprovalConfirm(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        var message = await result;
        if (message.Text == "no")
        {
            await context.PostAsync("Thank you");
            context.Done(context);
        }
        else if (message.Text == "yes")
        {
            await context.PostAsync("yes");
            context.Done(context);
        }
        else
        {
            context.Wait(MessageReceived);
        }
    }

我的用例就像,如果用户输入yes,那么bot应该返回yes,如果用户输入no,那么bot应该返回谢谢。如果用户输入“Hi”,机器人应该通过Luis找到Hi的答案,这将触及一个新的Greeting intent并返回Hi用户。

如果我使用context.Wait(MessageReceived),那么机器人只会等待来自用户的下一个查询

对话框的结构!

  [LuisIntent("Approval")]
    public async Task Approval(IDialogContext context, LuisResult result)
    {
        context.ConversationData.SetValue("currentIntent", "Approval");
        .
        .
        .
        PromptDialog.Choice<string>(context, ApprovalList, ...);
    }


private async Task ApprovalList(IDialogContext context, IAwaitable<string> result)
    {
        try
        {
            var request_type = await result;
            .
            .
            context.Wait(ApprovalResponse);
            }
        }
        catch (Exception ex)
        {
            CommonMethods.LogTime("ApprovalList", ex, context);
        }
    }
 private async Task ApprovalResponse(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        var message = await result;
       .
       .
       .
            context.Wait(AfterApprovalComment);
        }
        catch (Exception ex)
        {
            CommonMethods.LogTime("ApprovalResponse", ex, context);
            await context.PostAsync("out of approvals...");
            context.Done(context);
        }
    }


private async Task ApprovalConfirm(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        var message = await result;
       .
       .
       .
    await SelfRedirect(context, context.UserData.Get<string>("CurrentQuery"), "Approval");
    }

1 个答案:

答案 0 :(得分:0)

根据我们在评论中的讨论,了解更多您的要求,似乎您不需要重置堆栈。

相反,您需要做的是利用您继承的MessageReceived中的LuisDialog方法。通过这样做,您可以直接调用该方法并强制它转到LUIS以解析新意图,而无需用户的新输入。

调用该方法与调用任何其他方法完全相同。唯一需要注意的是,由于MessageReceived方法需要IAwaitable<IMessageActivity>,因此您必须创建该方法。好消息就是使用Awaitable.FromItem

一样简单