C# 从对话中回叫路易斯

C# 从对话中回叫路易斯,c#,bots,botframework,chatbot,azure-language-understanding,C#,Bots,Botframework,Chatbot,Azure Language Understanding,我正在使用Microsoft和C#开发聊天机器人。我的机器人基本上从LUIS那里得到了意图,并以此为基础,要么用静态字符串回答,要么转发到一个包含多个问题的新对话框。在新对话框中,用户发送的消息直接从代码中处理,无需通过LUIS 我的代码: main luisdialog.cs: [LuisIntent("Greetings")] public async Task Greetings(IDialogContext context, IAwaitable<IMessageActivity&

我正在使用Microsoft和C#开发聊天机器人。我的机器人基本上从LUIS那里得到了意图,并以此为基础,要么用静态字符串回答,要么转发到一个包含多个问题的新对话框。在新对话框中,用户发送的消息直接从代码中处理,无需通过LUIS

我的代码:

main luisdialog.cs:

[LuisIntent("Greetings")]
public async Task Greetings(IDialogContext context, IAwaitable<IMessageActivity> argument, LuisResult result)
{
    await context.PostAsync(@"Hello user!");
    context.Wait(MessageReceived);
}

[LuisIntent("NearbyRestaurants")]
public async Task NearbyRestaurants(IDialogContext context, IAwaitable<IMessageActivity> argument, LuisResult result)
{
    var msg = await argument;
    await context.Forward(new LocationDialog(), ResumeAfterLocationReceived, msg, CancellationToken.None);
}
public async Task StartAsync(IDialogContext context)
{
    context.Wait(MessageReceivedAsync);
}

public virtual async Task MessageReceivedAsync(IDialogCOntext context, IAwaitable<IMessageActivity> argument)
{
    var msg = await argument;
    var reply = context.MakeMessage();
    reply.Type = ActivityTypes.Message;
    reply.Text = "would you like to share your location?";
    reply.TextFormat = TextFormatTypes.Plain;
    reply.SuggestedActions = new SuggetedActions()
    {
        Actions = new List<CardAction>()
        {
            new CardAction(){ Title="Yes", Type=ActionTypes.ImBack, Value="yes"},
            new CardAction(){ Title="No", Type=ActionTypes.ImBack, Value="no"}
        }
    };
    await context.PostAsync(reply);
    context.Wait(ReplyReceivedAsync);
}

public virtual async Task ReplyReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
    var msg = await argument;
    if(msg.Text.Equals("yes"))
    {
        //forward to function for handling location
    }
    else if (msg.Text.Equals("no"))
    {
        context.Done("no location")
    }
    else
    {
        context.Done(msg.Text)
    }
}
[LuisIntent(“问候”)]
公共异步任务问候语(IDialogContext上下文、IAwaitable参数、LuisResult结果)
{
wait context.PostAsync(@“Hello user!”);
Wait(MessageReceived);
}
[LuisIntent(“NearbyRestarants”)]
公共异步任务NearbyRestaurants(IDialogContext上下文、IAwaitable参数、LuisResult结果)
{
var msg=等待参数;
wait context.Forward(new LocationDialog(),ResumeAfterLocationReceived,msg,CancellationToken.None);
}
LocationDialog.cs:

[LuisIntent("Greetings")]
public async Task Greetings(IDialogContext context, IAwaitable<IMessageActivity> argument, LuisResult result)
{
    await context.PostAsync(@"Hello user!");
    context.Wait(MessageReceived);
}

[LuisIntent("NearbyRestaurants")]
public async Task NearbyRestaurants(IDialogContext context, IAwaitable<IMessageActivity> argument, LuisResult result)
{
    var msg = await argument;
    await context.Forward(new LocationDialog(), ResumeAfterLocationReceived, msg, CancellationToken.None);
}
public async Task StartAsync(IDialogContext context)
{
    context.Wait(MessageReceivedAsync);
}

public virtual async Task MessageReceivedAsync(IDialogCOntext context, IAwaitable<IMessageActivity> argument)
{
    var msg = await argument;
    var reply = context.MakeMessage();
    reply.Type = ActivityTypes.Message;
    reply.Text = "would you like to share your location?";
    reply.TextFormat = TextFormatTypes.Plain;
    reply.SuggestedActions = new SuggetedActions()
    {
        Actions = new List<CardAction>()
        {
            new CardAction(){ Title="Yes", Type=ActionTypes.ImBack, Value="yes"},
            new CardAction(){ Title="No", Type=ActionTypes.ImBack, Value="no"}
        }
    };
    await context.PostAsync(reply);
    context.Wait(ReplyReceivedAsync);
}

public virtual async Task ReplyReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
    var msg = await argument;
    if(msg.Text.Equals("yes"))
    {
        //forward to function for handling location
    }
    else if (msg.Text.Equals("no"))
    {
        context.Done("no location")
    }
    else
    {
        context.Done(msg.Text)
    }
}
公共异步任务StartAsync(IDialogContext上下文)
{
Wait(MessageReceivedAsync);
}
公共虚拟异步任务消息ReceivedAsync(IDialogCOntext上下文,IAwaitable参数)
{
var msg=等待参数;
var reply=context.MakeMessage();
reply.Type=ActivityTypes.Message;
reply.Text=“您想分享您的位置吗?”;
reply.TextFormat=textformatypes.Plain;
reply.SuggestedActions=新建suggettedactions()
{
操作=新列表()
{
new CardAction(){Title=“Yes”,Type=ActionTypes.ImBack,Value=“Yes”},
new CardAction(){Title=“No”,Type=ActionTypes.ImBack,Value=“No”}
}
};
等待上下文。PostAsync(回复);
Wait(ReplyReceivedAsync);
}
公共虚拟异步任务ReplyReceivedAsync(IDialogContext上下文,IAwaitable参数)
{
var msg=等待参数;
如果(msg.Text.Equals(“是”))
{
//转发到处理位置的函数
}
else if(msg.Text.Equals(“no”))
{
上下文。完成(“无位置”)
}
其他的
{
context.Done(msg.Text)
}
}
Main LuisDialog.cs(ResumeAfterLocationReceived):

公共异步任务ResumeAfterLocationReceived(IDialogContext上下文,IAwaitable结果) { 如果(结果等于(“否”)) { 等待上下文。PostAsync(@“抱歉无法搜索”); Wait(MessageReceived); } 其他的 { //在这种情况下,我需要将消息直接转发给LUIS,以获得用户的意图 } }
当用户被问到是否要共享他的位置时,用户通过不同的消息回答是/否,我需要将该消息直接转发给LUIS,以获取用户的意图。我该怎么做?我知道,如果我使用上下文。等待(MeasGraceCeIVED),这将使代码忘记用户发送的消息,用户将不得不再次键入。

< P>您可能想考虑在这里更改逻辑。让您的按钮嵌入值可以是“是,使用我的位置”或“否,不要使用我的位置”之类的内容,或类似的变体。然后,您可以将整个字符串发送给Luis,就像您的问候语或附近的餐厅意图一样(或不像您当前所做的那样),作为位置意图。这样,您就不必关心处理其他意图。同样,你也不会把“是”和“否”这两个词绑在一起。

我在下面的帖子中用同样的方式解决了这个问题,并添加了几行:

在my Main LuisDialog.cs(ResumeAfterLocationReceived)中:

公共异步任务ResumeAfterLocationReceived(IDialogContext上下文,IAwaitable结果) { 如果(结果等于(“否”)) { 等待上下文。PostAsync(@“抱歉无法搜索”); Wait(MessageReceived); } 其他的 { //在此处添加代码: var userSearchString=result.Text; 活动myActivity=新活动(); myActivity.Text=userSearchString; Wait MessageReceived(上下文,waitiable.FromItem(myActivity)); } }
秘密是获取用户输入的文本,创建一个新活动,将文本添加到活动中,然后将其传递到LUIS对话框的messageReceived任务。

为了澄清,您想将“是”一词发送给LUIS?@JasonSowers否,如果是/否,我不需要返回LUIS,如果用户回答的不是“是/否”,例如“你好”,我需要将此消息发送回LUIS。谢谢:)谢谢,我看错了:)看看我刚才加的副本。在您的情况下,因为您使用的是
LuisDialog
而不是
MessageReceivedAsync
您只需要使用
MessageReceived
@EzequielJadib谢谢:)您链接的帖子解决了我的问题,我将在这里发布我案例的完整答案。这不是我的具体情况,在我的例子中,我有多个对话框,每个对话框可能包含多达30个问题,我不能每次都返回LUIS的主对话框,每当用户输入一个不是选项之一的文本时,我都需要这样做。不管怎样,我在以西结的评论中找到了我的答案,我会把它贴在下面。谢谢:)在我的回答中添加了更多关于活动的细节