Microsoft.Bot.Builder.Autofac/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 System;
35 using System.Threading;
36 using System.Threading.Tasks;
37 using Autofac;
40 using Microsoft.Bot.Connector;
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 
92  [Obsolete("Use the overload that uses ConversationReference instead of ResumptionCookie")]
93  public static async Task ResumeAsync(ResumptionCookie resumptionCookie, IActivity toBot, CancellationToken token = default(CancellationToken))
94  {
95  var conversationRef = resumptionCookie.ToConversationReference();
96  await ResumeAsync(conversationRef, toBot, token);
97  }
98 
106  public static async Task ResumeAsync(ConversationReference conversationReference, IActivity toBot, CancellationToken token = default(CancellationToken))
107  {
108  var continuationMessage = conversationReference.GetPostToBotMessage();
109  using (var scope = DialogModule.BeginLifetimeScope(Container, continuationMessage))
110  {
111  Func<IDialog<object>> MakeRoot = () => { throw new InvalidOperationException(); };
112  DialogModule_MakeRoot.Register(scope, MakeRoot);
113 
114  await SendAsync(scope, toBot, token);
115  }
116  }
117 
118  internal static async Task SendAsync(ILifetimeScope scope, IActivity toBot, CancellationToken token = default(CancellationToken))
119  {
120  var task = scope.Resolve<IPostToBot>();
121  await task.PostAsync(toBot, token);
122  }
123  }
124 }
Namespace for Microsoft Bot Builder SDK extensions to the Microsoft Bot Connector SDK...
Definition: KeyboardCard.cs:40
Namespace for the Microsoft Bot Connector SDK.
Autofac module for Dialog components.
Definition: DialogModule.cs:54
Activity GetPostToBotMessage()
Creates Activity from conversation reference as it is posted to bot.
Methods to send a message from the user to the bot.
Definition: PostToBot.cs:50
A IDialog<TResult> is a suspendable conversational process that produces a result of type TResult ...
Definition: IDialog.cs:47
Task PostAsync(IActivity activity, CancellationToken token)
Post an item (e.g. message or other external event) to the bot.
An object relating to a particular point in a conversation
static async Task SendAsync(IMessageActivity toBot, Func< IDialog< object >> MakeRoot, CancellationToken token=default(CancellationToken))
Process an incoming message within the conversation.
static async Task ResumeAsync(ConversationReference conversationReference, IActivity toBot, CancellationToken token=default(CancellationToken))
Resume a conversation and post the data to the dialog waiting.
Namespace for internal Dialogs machinery that is not useful for most developers and may change in the...
Shared properties for all activities
Definition: IActivity.cs:9
static async Task ResumeAsync(ResumptionCookie resumptionCookie, IActivity toBot, CancellationToken token=default(CancellationToken))
Resume a conversation and post the data to the dialog waiting.
static ILifetimeScope BeginLifetimeScope(ILifetimeScope scope, IMessageActivity message)
Definition: DialogModule.cs:62
Core namespace for Dialogs and associated infrastructure.
static void Register(ILifetimeScope scope, Func< IDialog< object >> MakeRoot)
Root namespace for the Microsoft Bot Builder SDK.