我正在使用Microsoft和C#开发一个聊天机器人。我的机器人基本上从LUIS获取意图,并基于它回复静态字符串或转发到多个问题的新对话框。在新对话框中,用户发送的消息直接在代码中处理,而不通过LUIS。
我的代码:
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:
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):
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),这将使代码忘记用户发送的消息,用户将不得不再次输入。
答案 0 :(得分:0)
您可能需要考虑在此处更改逻辑。让你的按钮ImBack值类似于&#34;是的,使用我的位置&#34;或者&#34;不,不要使用我的位置&#34;或者一些变化。然后你就可以将整个字符串发送给Luis,就像你的问候语或附近的餐馆意图(或者你当前正在做的那样)作为一个位置意图。这样你就不必担心处理其他意图了。此外,你不会把这些词绑在一起&#34;是&#34;和&#34;不&#34;
答案 1 :(得分:0)
在我的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任务。