You can create a WhosOn Custom Bot that runs on your WhosOn Server. This is useful if you want to access on-premises resources, or customise your interactions with the NLU services.
This guide creates a simple bot that reverses the input text that you say to it.
IBot
interface. The instances of the class relate to a single Bot inside the WhosOn Bot service, but can have many conversations ongoing at the same time. You can use whatever namespace you wish here.using Parkersoft.WhosOn.BotConnector;
using Parkersoft.WhosOn.BotConnector.Message;
namespace WhosOnBot
{
public class CustomBot : IBot
{
}
}
UniqueName
property as a simple property. This is where the bot's username is stored if you need to use it.public string UniqueName {get; set;}
OnBotMessage
delegate. This is the delegate that the consumer of your class will register against to receive messages from your bot code. The consumer of your class is the WhosOn Bot Service.public Delegates.BotMessage OnBotMessage {get; set;}
CreateConversation
function. This function is where you would put any code that is executed when your conversation starts. For our example, we don't need anything here, so we just return true.public bool CreateConversation(string conversationId, IDictionary<string, string> properties)
{
return true;
}
EndConversation
function. This function is where you would put any clean up code for a particular conversation after the visitor closes the session, or the Bot hands the conversation off to another user. For our example we don't need this to do anything.public bool EndConversation(string conversationId)
{
return true;
}
AddProperties
function. This function is called whenever any properties are changed. This is usually called when you are collecting dynamic survey fields from customers. Again, we don't need this for our example.public void AddProperties(string conversationId, IDictionary<string, string> properties)
{
return;
}
SendMessage
receives a line of text, and can then respond to the customer. Note that you don't have to respond, but you should return true unless you want the WhosOn Bot Service to attempt a default fallback response. Here we will call the SendBotMessageAsync
helper function (which in turn calls the OnBotMessage) with our reversal message. We use the text message type to send a message back to the end user.public bool SendMessage(string conversationId, string message)
{
var charArr = message.ToCharArray();
System.Array.Reverse(charArr);
this.SendBotMessageAsync(conversationId, new BotTextMessage() { Text = new string(charArr) });
return true;
}
IBotCreator
interface.using Parkersoft.WhosOn.BotConnector;
namespace WhosOnBot
{
public class CustomBotCreator : IBotCreator
{
BotType
property. This is the name that needs to be put in the settings portal to load your type of bot into the bot service. It doesn't need to match your class name.public string BotType => "CustomBot";
CreateBot
has a single parameter for the BotSettings
and returns an instance of a class that implements IBot
.public Delegates.CreateBot DoCreateBot
{
get
{
return ((x) =>
{
return new CustomBot();
});
}
}
BotSettings is a key table that contains any startup properties for the bot creation.
Now we have created our bot, compile it. The DLL needs to follow the naming pattern Parkersoft.WhosOn.BotConnector.{botname}.dll. The name doesn't need to match your bot type. Take the DLL that is created, and drop it into the Bot Services "Connectors" folder.
In the settings portal, you can now setup your bot. Login is an Admin user, go to your users -> bots area, and create a new bot.
In the Bot Platform, select "WhosOn Custom Bot" from the drop down.
In the Assistant ID field, enter the Bot Type that you set in the Bot Creator.
Save your bot.
Restart your WhosOn Bot Service so that the connectors are reloaded.
Test it out.
Sample source code is available on github.
https://github.com/Parker-Software/WhosOn-Custom-Bot
If your Bot Connector has dependencies as part of the build, you should put these into a subfolder with the same name as your bot assembly name.
If your assembly name is "CustomBot" then you put the CustomBot.dll into the "Connectors" folder, and make a subfolder here named "CustomBot". All the dependencies go into the "CustomBot" subfolder.
You can enter a JSON object in the custom bot options. This should be key value pairs like:
{
"url": "https://www.test.com/service",
"api-key": "1231412331231231241412313"
}
You can extract these from the BotSettings
object during bot creation.