forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkillConversationIdFactory.cs
41 lines (36 loc) · 1.8 KB
/
SkillConversationIdFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Skills;
using Microsoft.Bot.Schema;
using Newtonsoft.Json;
namespace Microsoft.BotBuilderSamples.SimpleRootBot
{
/// <summary>
/// A <see cref="SkillConversationIdFactory"/> that uses an in memory <see cref="ConcurrentDictionary{TKey,TValue}"/>
/// to store and retrieve <see cref="ConversationReference"/> instances.
/// </summary>
public class SkillConversationIdFactory : SkillConversationIdFactoryBase
{
private readonly ConcurrentDictionary<string, string> _conversationRefs = new ConcurrentDictionary<string, string>();
public override Task<string> CreateSkillConversationIdAsync(ConversationReference conversationReference, CancellationToken cancellationToken)
{
var crJson = JsonConvert.SerializeObject(conversationReference);
var key = $"{conversationReference.ChannelId}:{conversationReference.Conversation.Id}";
_conversationRefs.GetOrAdd(key, crJson);
return Task.FromResult(key);
}
public override Task<ConversationReference> GetConversationReferenceAsync(string skillConversationId, CancellationToken cancellationToken)
{
var conversationReference = JsonConvert.DeserializeObject<ConversationReference>(_conversationRefs[skillConversationId]);
return Task.FromResult(conversationReference);
}
public override Task DeleteConversationReferenceAsync(string skillConversationId, CancellationToken cancellationToken)
{
_conversationRefs.TryRemove(skillConversationId, out _);
return Task.CompletedTask;
}
}
}