can someone show me how to implement a bot action in .Net server? there are any template or liblary for .net? Thanks a lot.
1 Answers
There is a .NET library for handling conversations, actions etc.
https://github.com/valencianok/Wit.ai.net
This solution below is based on Wit.ai.net nuget package.
First you need to setup some handlers for Merge, Stop, Action, Say:
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;
}
The ActionHandler is the one you are interrested in if you want to listen to actions passed in from Wit. The "action" parameter will tell you which action to run. These can for example be set up with a dictionary where key is action name and value is the action. An example:
//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 }
};
NOTE: You need to have an action in Wit.ai called action1 or action2 to make this work. Change it to for example "getNews" if you have a getNews action in Wit.ai.
You then create a conversation and pass the handlers to the conversation:
var conversation = new WitConversation<object>(WIT_ACCESS_TOKEN, "conversation-id-123", null, MergeHandler, SayHandler, ActionHandler, StopHandler);
Then you just start a conversation:
conversation.SendMessageAsync("Message to bot here").Result;
If you need to send some context key back to the bot, then you need to modify the context parameter in any handler and return the new context, for example:
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" };
}
This is of course one simple way of doing this operation and I hope it can point you in the right direction.
You can also cerate some interface for all Wit Actions and then get an instance of the action you want inside the ActionHandler.
I have made a simple implementation of actions returned from Wit.ai api in a console application in the Gist below. Hope it helps.
https://gist.github.com/SimonPirre/c2f571c4b47d0ac3defb1bc7292f456f

- 131
- 1
- 6