DeleteProfileScorable.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 
38 using Microsoft.Bot.Connector;
39 using System;
40 using System.Collections.Generic;
41 using System.Linq;
42 using System.Text;
43 using System.Text.RegularExpressions;
44 using System.Threading;
45 using System.Threading.Tasks;
46 
47 namespace Microsoft.Bot.Builder.Dialogs
48 {
49  public sealed class DeleteProfileScorable : ScorableBase<IActivity, string, double>
50  {
51  private readonly IDialogStack stack;
52  private readonly IBotData botData;
53  private readonly IBotToUser botToUser;
54  private readonly Regex regex;
55 
56  public DeleteProfileScorable(IDialogStack stack, IBotData botData, IBotToUser botToUser, Regex regex)
57  {
58  SetField.NotNull(out this.stack, nameof(stack), stack);
59  SetField.NotNull(out this.botData, nameof(botData), botData);
60  SetField.NotNull(out this.botToUser, nameof(botToUser), botToUser);
61  SetField.NotNull(out this.regex, nameof(regex), regex);
62  }
63 
64  protected override async Task<string> PrepareAsync(IActivity activity, CancellationToken token)
65  {
66  var message = activity as IMessageActivity;
67  if (message != null && message.Text != null)
68  {
69  var text = message.Text;
70  var match = regex.Match(text);
71  if (match.Success)
72  {
73  return match.Groups[0].Value;
74  }
75  }
76 
77  return null;
78  }
79 
80  protected override bool HasScore(IActivity item, string state)
81  {
82  return state != null;
83  }
84 
85  protected override double GetScore(IActivity item, string state)
86  {
87  return 1.0;
88  }
89 
90  protected override async Task PostAsync(IActivity item, string state, CancellationToken token)
91  {
92  this.stack.Reset();
93  botData.UserData.Clear();
94  botData.PrivateConversationData.Clear();
95  await botData.FlushAsync(token);
96  await botToUser.PostAsync(Resources.UserProfileDeleted);
97  }
98  protected override Task DoneAsync(IActivity item, string state, CancellationToken token)
99  {
100  return Task.CompletedTask;
101  }
102  }
103 }
override double GetScore(IActivity item, string state)
A strongly-typed resource class, for looking up localized strings, etc.
Namespace for the Microsoft Bot Connector SDK.
string Text
Content for the message
Allow for static type checking of opaque state for convenience of scorable implementations.
Definition: ScorableBase.cs:55
Namespace for internal scorable implementation that is not useful for most developers and may change ...
Namespace for scorable interfaces, classes and compositions.
Namespace for internal machinery that is not useful for most developers and may change in the future...
override async Task< string > PrepareAsync(IActivity activity, CancellationToken token)
DeleteProfileScorable(IDialogStack stack, IBotData botData, IBotToUser botToUser, Regex regex)
Namespace for resources.
Namespace for internal Dialogs machinery that is not useful for most developers and may change in the...
override async Task PostAsync(IActivity item, string state, CancellationToken token)
Shared properties for all activities
Definition: IActivity.cs:9
static string UserProfileDeleted
Looks up a localized string similar to User profile deleted!.
override Task DoneAsync(IActivity item, string state, CancellationToken token)
override bool HasScore(IActivity item, string state)
Namespace for the internal fibers machinery that is not useful for most developers and may change in ...
Core namespace for Dialogs and associated infrastructure.
Methods to send a message from the bot to the user.
Definition: BotToUser.cs:51
The stack of dialogs in the conversational process.
Definition: IDialogTask.cs:46
Root namespace for the Microsoft Bot Builder SDK.