DialogContext.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.Generic;
36 using System.Threading;
37 using System.Threading.Tasks;
38 
40 using Microsoft.Bot.Connector;
41 
42 namespace Microsoft.Bot.Builder.Dialogs.Internals
43 {
44  public sealed class DialogContext : IDialogContext
45  {
46  private readonly IBotToUser botToUser;
47  private readonly IBotData botData;
48  private readonly IDialogStack stack;
49  private readonly CancellationToken token;
50  private readonly IActivity activity;
51 
52  public DialogContext(IBotToUser botToUser, IBotData botData, IDialogStack stack, IActivity activity, CancellationToken token)
53  {
54  SetField.NotNull(out this.botToUser, nameof(botToUser), botToUser);
55  SetField.NotNull(out this.botData, nameof(botData), botData);
56  SetField.NotNull(out this.stack, nameof(stack), stack);
57  SetField.NotNull(out this.activity, nameof(activity), activity);
58  this.token = token;
59  }
60 
62  {
63  get
64  {
65  return this.botData.ConversationData;
66  }
67  }
68 
70  {
71  get
72  {
73  return this.botData.PrivateConversationData;
74  }
75  }
76 
78  {
79  get
80  {
81  return this.botData.UserData;
82  }
83  }
84 
85  async Task IBotToUser.PostAsync(IMessageActivity message, CancellationToken cancellationToken)
86  {
87  await this.botToUser.PostAsync(message, cancellationToken);
88  }
89 
91  {
92  return this.botToUser.MakeMessage();
93  }
94 
95  IReadOnlyList<Delegate> IDialogStack.Frames
96  {
97  get
98  {
99  return this.stack.Frames;
100  }
101  }
102 
103  void IDialogStack.Call<R>(IDialog<R> child, ResumeAfter<R> resume)
104  {
105  this.stack.Call<R>(child, resume);
106  }
107 
108  void IDialogStack.Post<E>(E @event, ResumeAfter<E> resume)
109  {
110  this.stack.Post<E>(@event, resume);
111  }
112 
113  async Task IDialogStack.Forward<R, T>(IDialog<R> child, ResumeAfter<R> resume, T item, CancellationToken token)
114  {
115  await this.stack.Forward<R, T>(child, resume, item, token);
116  }
117 
118  void IDialogStack.Done<R>(R value)
119  {
120  this.stack.Done<R>(value);
121  }
122 
123  void IDialogStack.Fail(Exception error)
124  {
125  this.stack.Fail(error);
126  }
127 
128  void IDialogStack.Wait<R>(ResumeAfter<R> resume)
129  {
130  this.stack.Wait(resume);
131  }
132 
133  void IDialogStack.Reset()
134  {
135  this.stack.Reset();
136  }
137 
138  async Task IBotData.LoadAsync(CancellationToken cancellationToken)
139  {
140  await this.botData.LoadAsync(cancellationToken);
141  }
142 
143  async Task IBotData.FlushAsync(CancellationToken cancellationToken)
144  {
145  await this.botData.FlushAsync(cancellationToken);
146  }
147 
148  CancellationToken IBotContext.CancellationToken => this.token;
149 
150  IActivity IBotContext.Activity => this.activity;
151  }
152 }
The context for the execution of a dialog&#39;s conversational process.
Namespace for the Microsoft Bot Connector SDK.
Task LoadAsync(CancellationToken cancellationToken)
Loads the bot data from IBotDataStore<T>
void Fail(Exception error)
Fail the current dialog and return an exception to the parent dialog.
IReadOnlyList< Delegate > Frames
The dialog frames active on the stack.
Definition: IDialogTask.cs:51
Task FlushAsync(CancellationToken cancellationToken)
Flushes the bot data to IBotDataStore<T>
IBotDataBag UserData
Private bot data associated with a user (across all channels and conversations).
Definition: IBotData.cs:47
Namespace for internal machinery that is not useful for most developers and may change in the future...
A property bag of bot data.
Definition: IBotDataBag.cs:41
IBotDataBag ConversationData
Private bot data associated with a conversation.
Definition: IBotData.cs:52
Task PostAsync(IMessageActivity message, CancellationToken cancellationToken=default(CancellationToken))
Post a message to be sent to the user.
Shared properties for all activities
Definition: IActivity.cs:9
DialogContext(IBotToUser botToUser, IBotData botData, IDialogStack stack, IActivity activity, CancellationToken token)
IActivity Activity
The activity posted to bot.
IMessageActivity MakeMessage()
Make a message.
Namespace for the internal fibers machinery that is not useful for most developers and may change in ...
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.
IBotDataBag PrivateConversationData
Private bot data associated with a user in a conversation.
Definition: IBotData.cs:57