ScorableDialogs.cs
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license.
4 //
5 // Microsoft Bot Framework: http://botframework.com
6 //
7 // Bot Builder SDK Github:
8 // https://github.com/Microsoft/BotBuilder
9 //
10 // Copyright (c) Microsoft Corporation
11 // All rights reserved.
12 //
13 // MIT License:
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 
34 using System;
35 using System.Collections;
36 using System.Collections.Generic;
37 using System.Globalization;
38 using System.Linq;
39 using System.Text;
40 using System.Threading;
41 using System.Threading.Tasks;
42 
45 using Microsoft.Bot.Connector;
46 
47 namespace Microsoft.Bot.Builder.Dialogs.Internals
48 {
49  public sealed class ScoringDialogTask<Score> : IPostToBot
50  {
51  private readonly IPostToBot inner;
52  private readonly IScorable<IActivity, Score> scorable;
54  {
55  SetField.NotNull(out this.inner, nameof(inner), inner);
56  SetField.NotNull(out this.scorable, nameof(scorable), scorable);
57  }
58 
59  async Task IPostToBot.PostAsync<T>(T item, CancellationToken token)
60  {
61  var activity = item as IActivity;
62  if (activity != null)
63  {
64  if (await this.scorable.TryPostAsync(activity, token))
65  {
66  return;
67  }
68  }
69 
70  await this.inner.PostAsync<T>(item, token);
71  }
72  }
73 
74 
75  public partial class Extensions
76  {
77  public static IDialog<T> WithScorable<T, Item, Score>(this IDialog<T> antecedent, IScorable<Item, Score> scorable)
78  {
79  return new WithScorableDialog<T, Item, Score>(antecedent, scorable);
80  }
81 
82  [Serializable]
83  private sealed class WithScorableDialog<T, Item, Score> : DelegatingScorable<Item, Score>, IDialog<T>
84  {
85  public readonly IDialog<T> Antecedent;
86  public WithScorableDialog(IDialog<T> antecedent, IScorable<Item, Score> scorable)
87  : base(scorable)
88  {
89  SetField.NotNull(out this.Antecedent, nameof(antecedent), antecedent);
90  }
91  async Task IDialog<T>.StartAsync(IDialogContext context)
92  {
93  context.Call<T>(this.Antecedent, ResumeAsync);
94  }
95  private async Task ResumeAsync(IDialogContext context, IAwaitable<T> result)
96  {
97  context.Done(await result);
98  }
99  }
100  }
101 }
The context for the execution of a dialog&#39;s conversational process.
Root namespace for the Microsoft Bot Connector SDK.
Definition: ActionTypes.cs:7
Task StartAsync(IDialogContext context)
The start of the code that represents the conversational dialog.
Namespace for internal machinery that is not useful for most developers.
Methods to send a message from the user to the bot.
Definition: IDialogTask.cs:106
ScoringDialogTask(IPostToBot inner, IScorable< IActivity, Score > scorable)
basic shared properties for all activities
Definition: IActivity.cs:9
Namespace for the internal fibers machinery that is not useful for most developers.
Definition: Awaitable.cs:36
Root namespace for the Microsoft Bot Builder SDK.