Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
49fb298
Release 07-07-2017 (#176)
RobStand Jul 7, 2017
0973c43
Sync with live for 7-7-17 release
RobStand Jul 7, 2017
12646e6
ISO 639-2 recommends use of the language codes in lower case (#175)
johnd-ms Jul 8, 2017
926f3c4
Update bot-builder-dotnet-quickstart.md
brandonh-msft Jul 6, 2017
d0cd730
Removing bad metadata (#183)
Jul 14, 2017
ac1c2eb
Release 07-14-2017 (#184)
Jul 14, 2017
010b980
Conversation designer (#178)
v-ducvo Jul 14, 2017
1e3591f
Conversation Designer going live (#188)
Jul 14, 2017
574e013
requires (#186)
johnd-ms Jul 14, 2017
1ce3e76
Merge branch 'master' into live
RobStand Jul 14, 2017
6f6c2c2
Dialog next steps (#177)
v-ducvo Jul 20, 2017
9743606
Release 7-20-2017
RobStand Jul 20, 2017
3361cd6
Sync for release
RobStand Jul 20, 2017
8d30028
Subscribe to bot events, always show bot online (#189)
johnd-ms Jul 20, 2017
ec98458
Changing author alias (#193)
RobStand Jul 20, 2017
b4ec305
links to best current samples, User Story 17067 (#185)
johnd-ms Jul 20, 2017
6c0f28c
Second effort for PR181 (#187)
johnd-ms Jul 20, 2017
9370c54
Groupme screencaps (#191)
johnd-ms Jul 20, 2017
6de5e70
Updates to Slack, GroupMe, sample links, next steps (#194)
Jul 20, 2017
2c3143e
Fixed broken anchor links from table to section in topic. (#195)
v-ducvo Jul 21, 2017
36215da
Adding NoIndex, NoFollow to metadata (#196)
RobStand Jul 24, 2017
2ba2d36
Merge branch 'live' into master
RobStand Jul 24, 2017
5d966b7
Release 7-24-2017 (#197)
RobStand Jul 24, 2017
df7db59
Merge branch 'live'
RobStand Jul 24, 2017
27620f9
Removing broken video
RobStand Jul 26, 2017
88128b2
Update bot-builder-nodejs-cortana-skill.md
Jul 28, 2017
b7400f1
Update bot-builder-nodejs-cortana-skill.md (#198)
Jul 28, 2017
79f5dd8
added dotnet scorable dialogs (#180)
matthewshim-ms Jul 28, 2017
07aafb8
Merge branch 'live' into master
Jul 28, 2017
bf01cb8
Update filenames.md
RobStand Jul 28, 2017
9f42eca
Added entries about the documentation (#206)
RobStand Jul 31, 2017
23fa10d
Slack review (#204)
johnd-ms Aug 1, 2017
229e7ef
Scorable review (#203)
Aug 1, 2017
34ad2cf
Grammar correction. Whose, not Who's (#33)
Jul 24, 2017
3a6f112
Updated to correct version of IISExpress (#35)
deankroker Jul 31, 2017
ab17c93
Update resources-tools-downloads.md (#31)
mschray Aug 1, 2017
2fc82cc
Changed AttachmentLayouts to AttachmentLayoutTypes (#38)
peterkaplan Aug 1, 2017
be39fd3
Release 08-01-2017 (#207)
RobStand Aug 1, 2017
b979a3b
Updating next step
RobStand Aug 1, 2017
0b6fc95
Fixing illegal bookmark usage
RobStand Aug 1, 2017
53a0d6f
Fixing build warning in dotnet reference
RobStand Aug 1, 2017
c1fc1a8
Fixing build warning in dotnet reference
RobStand Aug 1, 2017
346bb26
Staging for fix from nuget release
RobStand Aug 1, 2017
a87b3e1
Cropped out wechat icon. wechat is not supported (#209)
RobStand Aug 2, 2017
f65fd93
Merge branch 'master' into live
RobStand Aug 2, 2017
8334a16
three downloads for one template (#212)
johnd-ms Aug 2, 2017
886333d
Initialize open publishing repository: https://github.com/MicrosoftDo…
RobStand Aug 2, 2017
e65b0ca
Initialize open publishing repository: https://github.com/MicrosoftDo…
RobStand Aug 2, 2017
46db002
Fix for bug# 17564. Add gitter.im, specifically the BotBuilder ROOM (…
johnd-ms Aug 3, 2017
22b7e60
Merge branch 'live' into master
RobStand Aug 3, 2017
998faaa
Release 08-03-2017 (#217)
RobStand Aug 3, 2017
b9e944b
Sync live with master for release
RobStand Aug 3, 2017
0748ceb
17561 deploy from local git (#215)
johnd-ms Aug 4, 2017
b2005b8
add ms.date metadata (#219)
Aug 4, 2017
44e9b64
Fixing search scope
RobStand Aug 5, 2017
20d2a36
Release 08-04-2017 (#221)
RobStand Aug 5, 2017
1a9969c
Merge branch 'live' into master
RobStand Aug 5, 2017
83cbd9c
# 17767: shorten titles to 80 chars for SEO (#220)
Aug 5, 2017
722d5a5
Community content (#223)
RobStand Aug 7, 2017
159b9b1
Configure PDF builds.
Aug 7, 2017
e74c9e5
Update .openpublishing.publish.config.json
Aug 7, 2017
a3b598a
Merge pull request #225 from MicrosoftDocs/master
Aug 7, 2017
91332fb
Merge branch 'live' into live-sync-work
RobStand Aug 7, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added dotnet scorable dialogs (#180)
* added dotnet scorable dialogs

* edited links, fixed typos to scorables article

* added date/title/header, grammar change

* Updated bot-builder-dotnet-scorable-dialogs.md
  • Loading branch information
matthewshim-ms authored and Kamran Iqbal committed Jul 28, 2017
commit 79f5dd875c5362cf34a6d2ab9f62406744b6671e
1 change: 1 addition & 0 deletions articles/TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#### [Dialogs overview](~/dotnet/bot-builder-dotnet-dialogs.md)
#### [Manage conversation flow](~/dotnet/bot-builder-dotnet-manage-conversation-flow.md)
#### [Enable language understanding](~/dotnet/bot-builder-dotnet-luis-dialogs.md)
#### [Global message handlers](~/dotnet/bot-builder-dotnet-scorable-dialogs.md)
### FormFlow
#### [Basic features of FormFlow](~/dotnet/bot-builder-dotnet-formflow.md)
#### [Advanced features of FormFlow](~/dotnet/bot-builder-dotnet-formflow-advanced.md)
Expand Down
148 changes: 148 additions & 0 deletions articles/dotnet/bot-builder-dotnet-scorable-dialogs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---
title: Global message handlers using Scorables
description: Create more flexible dialogs using scorables within the Bot Builder SDK for .NET.
author: matthewshim-ms
ms.author: v-shimma
manager: rstand
ms.topic: article
ms.prod: bot-framework
ms.date: 07/12/2017
---
# Global message handlers using Scorables

Within the Bot Framework .NET SDK, `Scorables` allow you to create dialogs as flexible components which act as global message handlers, in lieu of the traditional dialog model.

Scorable dialogs within a bot monitor all incoming messages, and determine wether or not that message is actionable in some way. Messages that are scorable are assigned a score between 0 - 1 (one being the highest) by each scorable dialog in the bot. The scorable dialog which determines the highest score then handles the response to the user.

When a user triggers a scorable dialog, that dialogue is then added to the top of the [dialog stack](../bot-design-conversation-flow.md#dialog-stack). After the scorable dialog is resolved, the conversation will continue from where it left off. This allows you to create more flexible conversations by allowing your users to 'interrupt' the normal hierarchal dialog structure.

>[!NOTE]
> Scorables is one of the ways that the .NET SDK uses [AutoFac](https://autofac.org/). AutoFac is used in the .NET BotBuilder SDK
> for [inversion of control and dependency injection](https://martinfowler.com/articles/injection.html). You can learn more about
> Autofac in this [quick start guide](http://autofac.readthedocs.io/en/latest/getting-started/index.html).

## Creating a Scorable dialog

First, you can define a new [dialog](bot-builder-dotnet-dialogs.md). If you've created a bot before, the following example will look very familiar, it is a typical dialog which uses the `IDialog` interface.

```cs
public class SampleDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync("This is a Sample Dialog which is Scorable. Reply with anything to return to the prior prior dialog.");

context.Wait(this.MessageReceived);
}

private async Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;

if ((message.Text != null) && (message.Text.Trim().Length > 0))
{
context.Done<object>(null);
}
else
{
context.Fail(new Exception("Message was not a string or was an empty string."));
}
}
}
```
To make a dialog scorable, you create a class which provides implementation of the abstract `ScorableBase` class. The `ScorableBase` class implements the `IScorable` interface that's defined in the Scorables class in the Bot Builder SDK for .NET.

```cs
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Builder.Internals.Fibers;
using Microsoft.Bot.Builder.Scorables.Internals;

public class SampleScorable : ScorableBase<IActivity, string, double>
{
private readonly IDialogTask task;

public SampleScorable(IDialogTask task)
{
SetField.NotNull(out this.task, nameof(task), task);
}
}
```

The **ScorableBase** abstract class uses the **IScorable** interface which requires that this class must include the following methods:

```cs
public interface IScorable<in Item, out Score>
{
Task<object> PrepareAsync(Item item, CancellationToken token);

bool HasScore(Item item, object state);

Score GetScore(Item item, object state);

Task PostAsync(Item item, object state, CancellationToken token);

Task DoneAsync(Item item, object state, CancellationToken token);
}
```
* **PrepareAsync**: This first method in the scorable instance accepts incoming message activity, analyzes and sets the dialog's `state`, which is passed to all the other methods in the interface.

* **HasScore**: This boolean method checks the state property to determine if the Scorable Dialog should provide a score for the message. If it returns false, the message will be ignored by the Scorable dialog.

* **GetScore**: Will only trigger if `HasScore` returns true, provision the logic here to determine the score for a message between 0 - 1.0

* **PostAsync**: In this method you define core actions to be performed for the scorable class. All scorable dialogs will monitor all incoming messages, and assign scores to valid messages based on the scorables' `GetScore` method. The scorable class which determines the highest score (between 0 - 1.0) will then trigger that scorable's `PostAsync` method.

* **DoneAsync**: Here you should dispose of any scoped resources as it is called when the scoring process has completed.

## Create a module to register the IScorable service

Define a `Module` which will register the `SampleScorable` as a component which provisions the `IScorable` service.

```cs
public class GlobalMessageHandlersBotModule : Module
{
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);

builder
.Register(c => new SampleScorable(c.Resolve<IDialogTask>()))
.As<IScorable<IActivity, double>>()
.InstancePerLifetimeScope();
}
}
```
## Register the module with

Lastly, you need to apply the `SampleScorable` to the bot's Conversation Container. This will register the scorable service within the bot builder framework's message handling pipeline.

Simply update the `Conversation.Container` as shown below within the bot app's initialization in **Global.asax.cs** as shown:

```cs
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
this.RegisterBotModules();
GlobalConfiguration.Configure(WebApiConfig.Register);
}

private void RegisterBotModules()
{
var builder = new ContainerBuilder();
builder.RegisterModule(new ReflectionSurrogateModule());

//Register the module within the Conversation container
builder.RegisterModule<GlobalMessageHandlersBotModule>();

builder.Update(Conversation.Container);
}
}
```

## Additional resources
* [Global Message Handlers sample](https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/core-GlobalMessageHandlers)
* [Simple Scorable Bot sample](https://github.com/Microsoft/BotFramework-Samples/tree/master/CSharp/ScorableBotSample)
* [Dialogs overview](bot-builder-dotnet-dialogs.md)
* [AutoFac](https://autofac.org/)