ResumptionContext.cs
1 using System;
2 using System.IO;
3 using System.IO.Compression;
4 using System.Runtime.Serialization.Formatters.Binary;
5 using System.Threading;
6 using System.Threading.Tasks;
7 
10 using Microsoft.Bot.Connector;
11 
12 namespace Microsoft.Bot.Builder.ConnectorEx
13 {
17  public sealed class ResumptionData
18  {
22  public string Locale { set; get; }
23 
27  public bool IsTrustedServiceUrl { set; get; }
28  }
29 
33  public sealed class ResumptionContext
34  {
35 
39  public const string RESUMPTION_CONTEXT_KEY = "ResumptionContext";
40 
44  private readonly Lazy<IBotDataBag> botDataBag;
45 
46  public ResumptionContext(Func<IBotDataBag> makeBotDataBag)
47  {
48  SetField.CheckNull(nameof(makeBotDataBag), makeBotDataBag);
49  this.botDataBag = new Lazy<IBotDataBag>(() => makeBotDataBag());
50  }
51 
56  public async Task<ResumptionData> LoadDataAsync(CancellationToken token)
57  {
58  ResumptionData data;
59  botDataBag.Value.TryGetValue(ResumptionContext.RESUMPTION_CONTEXT_KEY, out data);
60  return data;
61  }
62 
68  public async Task SaveDataAsync(ResumptionData data, CancellationToken token)
69  {
70  var clonedData = new ResumptionData
71  {
72  Locale = data.Locale,
73  IsTrustedServiceUrl = data.IsTrustedServiceUrl
74  };
75 
76  botDataBag.Value.SetValue(ResumptionContext.RESUMPTION_CONTEXT_KEY, clonedData);
77  }
78  }
79 
83  public sealed class ConversationReferenceHelpers
84  {
85 
91  public static ConversationReference GZipDeserialize(string str)
92  {
93  byte[] bytes = Convert.FromBase64String(str);
94 
95  using (var stream = new MemoryStream(bytes))
96  using (var gz = new GZipStream(stream, CompressionMode.Decompress))
97  {
98  return (ConversationReference)(new BinaryFormatter().Deserialize(gz));
99  }
100  }
101  }
102 
103  public static partial class Extensions
104  {
111  {
112  return new ConversationReference
113  {
114  Bot = new ChannelAccount { Id = address.BotId },
115  ChannelId = address.ChannelId,
116  User = new ChannelAccount { Id = address.UserId },
117  Conversation = new ConversationAccount { Id = address.ConversationId },
118  ServiceUrl = address.ServiceUrl
119  };
120  }
121 
122 #pragma warning disable CS0618 //disable obsolete warning for this helper.
123  public static ConversationReference ToConversationReference(this ResumptionCookie resumptionCookie)
129  {
130  return new ConversationReference
131  {
132  Bot = new ChannelAccount { Id = resumptionCookie.Address.BotId, Name = resumptionCookie.UserName },
133  ChannelId = resumptionCookie.Address.ChannelId,
134  User = new ChannelAccount { Id = resumptionCookie.Address.UserId, Name = resumptionCookie.UserName },
135  Conversation = new ConversationAccount { Id = resumptionCookie.Address.ConversationId, IsGroup = resumptionCookie.IsGroup },
136  ServiceUrl = resumptionCookie.Address.ServiceUrl
137  };
138  }
139 #pragma warning restore CS0618
140 
147  {
148  return new ConversationReference
149  {
150  Bot = new ChannelAccount { Id = activity.Recipient.Id, Name = activity.Recipient.Name },
151  ChannelId = activity.ChannelId,
152  User = new ChannelAccount { Id = activity.From.Id, Name = activity.From.Name },
153  Conversation = new ConversationAccount { Id = activity.Conversation.Id, IsGroup = activity.Conversation.IsGroup, Name = activity.Conversation.Name },
154  ServiceUrl = activity.ServiceUrl
155  };
156  }
157 
163  public static string GZipSerialize(this ConversationReference conversationReference)
164  {
165  using (var cmpStream = new MemoryStream())
166  using (var stream = new GZipStream(cmpStream, CompressionMode.Compress))
167  {
168  new BinaryFormatter().Serialize(stream, conversationReference);
169  stream.Close();
170  return Convert.ToBase64String(cmpStream.ToArray());
171  }
172  }
173  }
174 }
string Name
Display friendly name
bool IsGroup
Is this a reference to a group
static ConversationReference ToConversationReference(this IActivity activity)
Creates a ConversationReference from IActivity.
static ConversationReference GZipDeserialize(string str)
Deserializes the GZip serialized ConversationReference using Extensions.GZipSerialize(ConversationRef...
Namespace for the Microsoft Bot Connector SDK.
bool IsTrustedServiceUrl
The flag indicating if the ServiceUrl is trusted.
ConversationAccount Conversation
Address for the conversation that this activity is associated with
Definition: IActivity.cs:49
static string GZipSerialize(this ConversationReference conversationReference)
Binary serializes ConversationReference using GZipStream.
static ConversationReference ToConversationReference(this IAddress address)
Creates a ConversationReference from IAddress.
string ServiceUrl
ServiceUrl
Definition: IActivity.cs:24
Channel account information for a conversation
string ChannelId
Channel this activity is associated with
Definition: IActivity.cs:39
string Id
Channel id for the user or bot on this channel (Example: joe@smith.com, or or 123456) ...
ChannelAccount Recipient
Address that received the message
Definition: IActivity.cs:54
The data persisted for ConversationReference that will be consumed for Conversation.ResumeAsync.
const string RESUMPTION_CONTEXT_KEY
The key for ResumptionData in botDataBag.
Namespace for internal machinery that is not useful for most developers and may change in the future...
The key that minimally and completely identifies a bot&#39;s conversation with a user on a channel...
Definition: Address.cs:45
The resumption context that is responsible for loading/persisting the ResumptionData.
An object relating to a particular point in a conversation
async Task SaveDataAsync(ResumptionData data, CancellationToken token)
Save the data in botDataBag.
Shared properties for all activities
Definition: IActivity.cs:9
ChannelAccount From
Sender address data
Definition: IActivity.cs:44
string Id
Channel id for the user or bot on this channel (Example: joe@smith.com, or or 123456) ...
Namespace for the internal fibers machinery that is not useful for most developers and may change in ...
Core namespace for Dialogs and associated infrastructure.
Root namespace for the Microsoft Bot Builder SDK.
async Task< ResumptionData > LoadDataAsync(CancellationToken token)
Load ResumptionData from botDataBag.
Channel account information needed to route a message
ResumptionContext(Func< IBotDataBag > makeBotDataBag)