从对话框中回调LUIS [英] Calling back LUIS from a dialog
问题描述
我正在使用Microsoft和C#开发聊天机器人.我的机器人基本上是从LUIS那里获得意图的,并基于此意图以静态字符串答复或转发到新的包含多个问题的对话框.在新对话框中,用户发送的消息是直接在代码中处理的,而无需通过LUIS.
I am developing a chatbot using Microsoft and C#. My bot basically gets the intent from LUIS and based on that either replies with a static String or forwards to a new Dialog of multiple questions. Inside the new dialog the messages sent by the user are directly handled from within the code without passing through LUIS.
我的代码:
MainLUISDialog.cs:
MainLUISDialog.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);
}
LocationDialog.cs:
LocationDialog.cs:
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)
}
}
MainLUISDialog.cs(ResumeAfterLocationReceived):
MainLUISDialog.cs (ResumeAfterLocationReceived):
public async Task ResumeAfterLocationReceived(IDialogContext context, IAwaitable<String> result)
{
if(result.Equals("no"))
{
await context.PostAsync(@"Sorry can't search");
context.Wait(MessageReceived);
}
else
{
//in this case i need to forward the message directly to LUIS to get the user's intent
}
}
当询问用户是否要共享其位置并且用户通过不同的消息(是/否)回答时,我需要将该消息直接转发回LUIS以获取用户的意图.我怎么做?我知道,如果我使用context.Wait(MessageReceived),这将使代码忘记用户发送的消息,并且用户将不得不再次键入该消息.
when the user is asked if he wants to share his location and the user answers by a message different that yes/no i need to forward that message directly back to LUIS to get the user's intent. How do I do that? I know that if I use context.Wait(MessageReceived) this will make the code forget the message sent by the user and the user will have to type it again.
推荐答案
我用以下方法在Ezequel Jadib的回答中以相同的方式解决了它: 如何将Prompt.Choice()的结果转发到当前对话框?
i solved it same way Ezequel Jadib answered in the below post with a few added lines: How to forward result of Prompt.Choice() to current dialog?
在我的MainLUISDialog.cs中(ResumeAfterLocationReceived):
in my MainLUISDialog.cs (ResumeAfterLocationReceived):
public async Task ResumeAfterLocationReceived(IDialogContext context, IAwaitable<String> result)
{
if(result.Equals("no"))
{
await context.PostAsync(@"Sorry can't search");
context.Wait(MessageReceived);
}
else
{
//added code here:
var userSearchString = result.Text;
Activity myActivity = new Activity();
myActivity.Text = userSearchString ;
await MessageReceived(context, Awaitable.FromItem(myActivity));
}
}
秘密在于获取用户输入的文本,创建一个新的活动,将该文本添加到活动中,然后将其传递给LUIS对话框的messageReceived任务.
the secret was to get the text that the user entered, create a new activity, add the text to the activity and then pass it to the LUIS dialog's messageReceived task.
这篇关于从对话框中回调LUIS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!