问题
I'm trying to integrate Luis.ai to C# bot framework. The code runs but when I send a message to the bot it shows this error:
"sorry my bot code is having an issue"
When it should reply depending on the entry using the intents, I only have 2 intents "None" and "perfil".
This is my log:
This is my class Perfil.cs:
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Luis.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
namespace SistemaExperto.Dialogs
{
[LuisModel(modelID: "e6168727-2f3e-438b-b46a-88449f4ab52f", subscriptionKey: "ed5f1bda20ac42649123b8969d30e1aa")]
[Serializable]
public class Perfil : LuisDialog<string>
{
[LuisIntent("None")]
public async Task None(IDialogContext context, LuisServiceResult result)
{
await context.PostAsync("I'm sorry I don't have that information");
await context.PostAsync("Try again");
}
[LuisIntent("perfil")]
public async Task perfil(IDialogContext context, LuisServiceResult result)
{
await context.PostAsync("My name is Alex");
}
}
}
This is my Controller MessageController.cs:
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
namespace SistemaExperto
{
[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new Dialogs.Perfil());
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
private Activity HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.DeleteUserData)
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
}
else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
// Handle add/remove from contact lists
// Activity.From + Activity.Action represent what happened
}
else if (message.Type == ActivityTypes.Typing)
{
// Handle knowing tha the user is typing
}
else if (message.Type == ActivityTypes.Ping)
{
}
return null;
}
}
}
回答1:
I test the code that you provided and replace with my LUIS app modelID&subscriptionKey, if it reach perfil intent, the code work as expected.
If the LuisDialog cannot resolve the method (intent) to execute based on the message received, I get the exception:
The given key was not present in the dictionary.
To solve the issue, I add [LuisIntent("")]
on top of the None
method.
[LuisModel(modelID: "{your_modelID}", subscriptionKey: "{your_ subscriptionKey}")]
[Serializable]
public class Perfil : LuisDialog<object>
{
[LuisIntent("")]
[LuisIntent("None")]
public async Task None(IDialogContext context, LuisResult result)
{
await context.PostAsync("I'm sorry I don't have that information");
await context.PostAsync("Try again");
}
[LuisIntent("perfil")]
public async Task perfil(IDialogContext context, LuisResult result)
{
await context.PostAsync("My name is Alex");
}
}
Test Result:
Reach perfil
intent:
Exception error:
来源:https://stackoverflow.com/questions/49971640/ingtegrating-luis-with-c-sharp-bot-framework-error