forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkillConversationIdFactory.cs
78 lines (66 loc) · 3.41 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Skills;
using Newtonsoft.Json.Linq;
namespace Microsoft.BotBuilderSamples.DialogRootBot
{
/// <summary>
/// A <see cref="SkillConversationIdFactory"/> that uses <see cref="IStorage"/> to store
/// and retrieve <see cref="SkillConversationReference"/> instances.
/// </summary>
public class SkillConversationIdFactory : SkillConversationIdFactoryBase
{
private readonly IStorage _storage;
public SkillConversationIdFactory(IStorage storage)
{
_storage = storage ?? throw new ArgumentNullException(nameof(storage));
}
public override async Task<string> CreateSkillConversationIdAsync(SkillConversationIdFactoryOptions options, CancellationToken cancellationToken)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
// Create the storage key based on the SkillConversationIdFactoryOptions.
var conversationReference = options.Activity.GetConversationReference();
var skillConversationId = $"{conversationReference.Conversation.Id}-{options.BotFrameworkSkill.Id}-{conversationReference.ChannelId}-skillconvo";
// Create the SkillConversationReference instance.
var skillConversationReference = new SkillConversationReference
{
ConversationReference = conversationReference,
OAuthScope = options.FromBotOAuthScope
};
// Store the SkillConversationReference using the skillConversationId as a key.
var skillConversationInfo = new Dictionary<string, object> { { skillConversationId, JObject.FromObject(skillConversationReference) } };
await _storage.WriteAsync(skillConversationInfo, cancellationToken).ConfigureAwait(false);
// Return the generated skillConversationId (that will be also used as the conversation ID to call the skill).
return skillConversationId;
}
public override async Task<SkillConversationReference> GetSkillConversationReferenceAsync(string skillConversationId, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(skillConversationId))
{
throw new ArgumentNullException(nameof(skillConversationId));
}
// Get the SkillConversationReference from storage for the given skillConversationId.
var skillConversationInfo = await _storage.ReadAsync(new[] { skillConversationId }, cancellationToken).ConfigureAwait(false);
if (skillConversationInfo.Any())
{
var conversationInfo = ((JObject)skillConversationInfo[skillConversationId]).ToObject<SkillConversationReference>();
return conversationInfo;
}
return null;
}
public override async Task DeleteConversationReferenceAsync(string skillConversationId, CancellationToken cancellationToken)
{
// Delete the SkillConversationReference from storage.
await _storage.DeleteAsync(new[] { skillConversationId }, cancellationToken).ConfigureAwait(false);
}
}
}