forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SkillConversationIdFactory.cs
45 lines (40 loc) · 2.14 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
42
43
44
45
// 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(SkillConversationIdFactoryOptions options, CancellationToken cancellationToken)
{
var skillConversationReference = new SkillConversationReference
{
ConversationReference = options.Activity.GetConversationReference(),
OAuthScope = options.FromBotOAuthScope
};
var key = $"{options.FromBotId}-{options.BotFrameworkSkill.AppId}-{skillConversationReference.ConversationReference.Conversation.Id}-{skillConversationReference.ConversationReference.ChannelId}-skillconvo";
_conversationRefs.GetOrAdd(key, JsonConvert.SerializeObject(skillConversationReference));
return Task.FromResult(key);
}
public override Task<SkillConversationReference> GetSkillConversationReferenceAsync(string skillConversationId, CancellationToken cancellationToken)
{
var conversationReference = JsonConvert.DeserializeObject<SkillConversationReference>(_conversationRefs[skillConversationId]);
return Task.FromResult(conversationReference);
}
public override Task DeleteConversationReferenceAsync(string skillConversationId, CancellationToken cancellationToken)
{
_conversationRefs.TryRemove(skillConversationId, out _);
return Task.CompletedTask;
}
}
}