Dialogs/Conversation.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 Autofac;
36 using Microsoft.Bot.Connector;
37 using System;
38 using System.Globalization;
39 using System.Threading;
40 using System.Threading.Tasks;
41 
42 namespace Microsoft.Bot.Builder.Dialogs
43 {
47  public static partial class Conversation
48  {
49  public static readonly IContainer Container;
50 
51  static Conversation()
52  {
53  var builder = new ContainerBuilder();
54  builder.RegisterModule(new DialogModule_MakeRoot());
55  Container = builder.Build();
56  }
57 
76  public static async Task SendAsync(IMessageActivity toBot, Func<IDialog<object>> MakeRoot, CancellationToken token = default(CancellationToken))
77  {
78  using (var scope = DialogModule.BeginLifetimeScope(Container, toBot))
79  {
80  DialogModule_MakeRoot.Register(scope, MakeRoot);
81  await SendAsync(scope, toBot, token);
82  }
83  }
84 
93  public static async Task ResumeAsync<T>(ResumptionCookie resumptionCookie, T toBot, CancellationToken token = default(CancellationToken))
94  {
95  if (resumptionCookie.IsTrustedServiceUrl)
96  {
97  MicrosoftAppCredentials.TrustServiceUrl(resumptionCookie.Address.ServiceUrl);
98  }
99 
100  var continuationMessage = resumptionCookie.GetMessage();
101  using (var scope = DialogModule.BeginLifetimeScope(Container, continuationMessage))
102  {
103  Func<IDialog<object>> MakeRoot = () => { throw new InvalidOperationException(); };
104  DialogModule_MakeRoot.Register(scope, MakeRoot);
105 
106  await ResumeAsync(scope, continuationMessage, toBot, token);
107  }
108  }
109 
110  internal static async Task SendAsync(ILifetimeScope scope, IMessageActivity toBot, CancellationToken token = default(CancellationToken))
111  {
112  using (new LocalizedScope(toBot.Locale))
113  {
114  var task = scope.Resolve<IPostToBot>();
115  await task.PostAsync(toBot, token);
116  }
117  }
118 
119  internal static async Task ResumeAsync<T>(ILifetimeScope scope, IMessageActivity continuationMessage, T toBot, CancellationToken token = default(CancellationToken))
120  {
121  var client = scope.Resolve<IConnectorClient>();
122  var botData = scope.Resolve<IBotData>();
123  await botData.LoadAsync(token);
124 
125  using (new LocalizedScope(continuationMessage.Locale))
126  {
127  var task = scope.Resolve<IPostToBot>();
128  await task.PostAsync(toBot, token);
129  }
130  }
131  }
132 }
Root namespace for the Microsoft Bot Connector SDK.
Definition: ActionTypes.cs:7
Task LoadAsync(CancellationToken cancellationToken)
Loads the bot data from IBotDataStore<T>
Autofac module for Dialog components.
Definition: DialogModule.cs:54
The top level composition root for the SDK.
Methods to send a message from the user to the bot.
Definition: IDialogTask.cs:106
A IDialog<T> is a suspendable conversational process that produces a result of type T ...
Definition: IDialog.cs:47
static async Task SendAsync(IMessageActivity toBot, Func< IDialog< object >> MakeRoot, CancellationToken token=default(CancellationToken))
Process an incoming message within the conversation.
Namespace for internal Dialogs machinery that is not useful for most developers.
Someone has added a message to the conversation
static ILifetimeScope BeginLifetimeScope(ILifetimeScope scope, IMessageActivity message)
Definition: DialogModule.cs:61
string Locale
The language code of the Text field
Core namespace for Dialogs and associated infrastructure.
Definition: Address.cs:40
static void Register(ILifetimeScope scope, Func< IDialog< object >> MakeRoot)
Root namespace for the Microsoft Bot Builder SDK.
´╗┐The Bot Connector REST API allows your bot to send and receive messages to channels configured in t...