有人能告诉我如何在.Net服务器上实现机器人操作吗?是否有.net的模板或liblary?非常感谢。
发布于 2016-12-04 18:49:56
有一个用于处理会话、操作等的.NET库。
https://github.com/valencianok/Wit.ai.net
下面这个解决方案是基于Wit.ai.net nuget包的。
首先,您需要为合并、停止、操作设置一些处理程序,例如:
static object ActionHandler(string conversationId, object context, string action, Dictionary<string, List<Entity>> entities, double confidence)
{
Console.WriteLine("Action handler called");
//Invoke correct action based on action name passed in by action parameter
Actions[action].Invoke();
return context;
}
static object MergeHandler(string conversationId, object context, Dictionary<string, List<Entity>> entities, double confidence)
{
Console.WriteLine("Merge handler called");
return context;
}
static void SayHandler(string conversationId, object context, string msg, double confidence)
{
Console.WriteLine("Say handler called " + msg);
}
static object StopHandler(string conversationId, object context)
{
Console.WriteLine("Stop handler called");
return context;
}如果您想要侦听从Wit传入的操作,那么ActionHandler就是您要交互的那个。" action“参数将告诉您要运行哪个操作。例如,可以使用字典来设置这些操作,其中键是操作名称,值是操作。举个例子:
//Define actions to run based on Wit callback
static void Action1()
{
Console.WriteLine("Action1 is called");
}
static void Action2()
{
Console.WriteLine("Action2 is called");
}
//Define actions in dictionary, key will be the action recieved from Wit response, value will be an invokable action
static Dictionary<string, Action> Actions => new Dictionary<string, Action>
{
{ "action1", Action1 },
{ "action2", Action2 }
};注意:您需要在Wit.ai中有一个名为action1或action2的操作来完成这项工作。如果在Wit.ai.中有getNews操作,则将其更改为“Wit.ai.”。
然后创建一个会话并将处理程序传递给会话:
var conversation = new WitConversation<object>(WIT_ACCESS_TOKEN, "conversation-id-123", null, MergeHandler, SayHandler, ActionHandler, StopHandler);然后你就开始一段对话:
conversation.SendMessageAsync("Message to bot here").Result;如果需要向bot发送一些上下文密钥,则需要修改任何处理程序中的上下文参数并返回新上下文,例如:
static object ActionHandler(string conversationId, object context, string action, Dictionary<string, List<Entity>> entities, double confidence)
{
Console.WriteLine("Action handler called");
//Invoke correct action based on action name passed in by action parameter
Actions[action].Invoke();
return new { context_key_here = "context key value" };
}当然,这是一种简单的操作方法,我希望它能为你指明正确的方向。
您还可以为所有Wit操作验证一些接口,然后在ActionHandler中获取您想要的操作的实例。
我在下面的Gist中的控制台应用程序中实现了从Wit.ai api返回的操作。希望能帮上忙。
https://gist.github.com/SimonPirre/c2f571c4b47d0ac3defb1bc7292f456f
https://stackoverflow.com/questions/40733766
复制相似问题