BotToolkit 1.0.0
dotnet add package BotToolkit --version 1.0.0
NuGet\Install-Package BotToolkit -Version 1.0.0
<PackageReference Include="BotToolkit" Version="1.0.0" />
paket add BotToolkit --version 1.0.0
#r "nuget: BotToolkit, 1.0.0"
// Install BotToolkit as a Cake Addin #addin nuget:?package=BotToolkit&version=1.0.0 // Install BotToolkit as a Cake Tool #tool nuget:?package=BotToolkit&version=1.0.0
BotToolkit
Toolkit that makes life easier when working with the Bot Framework in C#
Usage
The toolkit provides a BotService that simplifies the way you wire up a bot that presents the users with one or more menus.
In MessagesController.cs:
[BotAuthentication]
public class MessagesController : ApiController
{
private static BotService _BotService;
static MessagesController()
{
var bot = new Bot();
bot.RootMenu = new Menu
{
Prompt = "Hi there, this is the main menu",
MenuItems = new[]
{
new MenuItem { Label = "about", Action = new DialogMenuAction<object>(new AboutDialog(), _MenuFinished) },
new MenuItem { Label = "help", Action = new DialogMenuAction<object>(new HelpDialog(), _MenuFinished) }
}
};
_BotService = new BotService(bot);
}
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
await _BotService.RespondAsync(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
private static Task _MenuFinished(IDialogContext context, IAwaitable<object> result)
{
return Task.Delay(0);
}
}
This will render a menu when the bot receives a message.
Authentication
If your bot requires the user to login, you'll most likely want to make use of the excellent AuthBot project.
The BotService has an Authenticator property that you can use to integrate with AuthBot (or some other authentication mechanism of your choosing):
//In MessagesController.cs
_BotService = new BotService(bot);
_BotService.Authenticator = new AzureAuthenticator();
Here's a simple implementation of IAuthenticator for AuthBot:
[Serializable]
public class AzureAuthenticator : IAuthenticator
{
public async Task<AuthenticationStatus> CheckAuthAsync(IDialogContext context)
{
var hasToken = !string.IsNullOrEmpty(await context.GetAccessToken(AuthSettings.Scopes));
return hasToken ?
AuthenticationStatus.Authenticated :
AuthenticationStatus.NotAuthenticated;
}
public Task RequestAuth(IDialogContext context, IMessageActivity activity)
{
return context.Forward(new AzureAuthDialog(AuthSettings.Scopes), _AuthComplete, activity, CancellationToken.None);
}
private async Task _AuthComplete(IDialogContext context, IAwaitable<object> result)
{
var auth = await result;
context.Done<object>(null);
}
}
The BotService will then enforce authentication prior to displaying the menu.
Typing Indicator
If you want the bot to send the typing indicator automatically each time it receives a message, using BotService.RespondAsync
is all that's needed, it's enabled by default.
If you don't want this behaviour, simply set the SendTypingIndicator boolean to false on BotService.
Pickers
Some things need to be commonly asked for from the user so the toolkit includes some reusable pickers:
Use the DatePickerDialog to pick dates and optionally times. It returns a DateTime with the result.
var picker = new DatePickerDialog();
picker.Mode = DatePickerMode.DateAndTime;
context.Call(picker, DatePicked);
private async Task _DateReceived(IDialogContext context, IAwaitable<DateTime> result)
{
var date = await result;
//do something with the date
}
Helpers
Let's say you've stored an object into the IBotDataBag, ie: PrivateConversationData and you want to progressively update it.
This doesn't work:
var myObj = context.PrivateConversationData.Get<MyObject>("TheKey");
myObj.SomeProperty = "new value";
...as the object needs to be saved again to the IBotDataBag.
Instead, use the DataHelper like this to take care of the save once it leaves the using block:
using(var myObj = new DataHelper<MyObject>(context.PrivateConversationData, "TheKey"))
{
myObj.Value.SomeProperty = "new value";
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
- Autofac (>= 4.6.0)
- Microsoft.Bot.Builder (>= 3.9.1)
- Microsoft.Rest.ClientRuntime (>= 2.3.10)
- Newtonsoft.Json (>= 10.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 1,327 | 9/27/2017 |