This is a simple Microsoft Bot Framework Direct Line implementation for Unity. The current implementation enables:
- Starting a new conversation with the bot
- Sending and receiving simple messages
-
First simply copy the content to the assets of your Unity app.
-
Initialize the BotDirectLineManager class with your secret key (this will bind the manager to your bot) and start listening to bot response events:
BotDirectLineManager.Initialize("INSERT YOUR BOT'S SECRET KEY HERE"); BotDirectLineManager.Instance.BotResponse += OnBotResponse;
-
Start a new conversation (
OnBotResponse
will be called when the new conversation is started):StartCoroutine(BotDirectLineManager.Instance.StartConversationCoroutine());
-
To send a message:
StartCoroutine(BotDirectLineManager.Instance.SendMessageCoroutine( _conversationState.ConversationId, "UnityUserId", "Hello bot!", "Unity User 1"));
Note that there are no return values, but your BotResponse
event handler will
be called for all results. Here's an example for the event handler
implementation:
private void OnBotResponse(object sender, Assets.BotDirectLine.BotResponseEventArgs e)
{
Debug.Log("OnBotResponse: " + e.ToString());
switch (e.EventType)
{
case EventTypes.ConversationStarted:
// Store the ID
_conversationState.ConversationId = e.ConversationId;
break;
case EventTypes.MessageSent:
if (!string.IsNullOrEmpty(_conversationState.ConversationId))
{
// Get the bot's response(s)
StartCoroutine(BotDirectLineManager.Instance.GetMessagesCoroutine(_conversationState.ConversationId));
}
break;
case EventTypes.MessageReceived:
// Handle the received message(s)
break;
case EventTypes.Error:
// Handle the error
break;
}
}
The implementation uses SimpleJSON for parsing JSON. The original version of the SimpleJSON project is licensed under MIT (see the license file) and was created by Markus Göbel.