-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageProxyService.cs
51 lines (46 loc) · 1.98 KB
/
MessageProxyService.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
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Configuration;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace SimpleProxyBot
{
public class MessageProxyService
{
private readonly ConcurrentDictionary<string, ConversationReference> _references = new ConcurrentDictionary<string, ConversationReference>();
private readonly IBotFrameworkHttpAdapter _adapter;
private readonly string _appId;
public MessageProxyService(IBotFrameworkHttpAdapter adapter, IConfiguration configuration)
{
_adapter = adapter;
_appId = configuration["MicrosoftAppId"];
}
public async Task ProxyToSharedConversation(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
bool send = true;
var reference = turnContext.Activity.GetConversationReference();
if (!_references.ContainsKey(reference.Conversation.Id))
{
await turnContext.SendActivityAsync(MessageFactory.Text("You have been added to the conversation."));
send = false;
}
_references.AddOrUpdate(reference.Conversation.Id, reference, (key, newValue) => reference);
if (send)
{
foreach (var refKeyVal in _references)
{
if (refKeyVal.Key != reference.Conversation.Id)
{
var continueReference = refKeyVal.Value;
await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, continueReference, async (context, cancelToken) =>
{
await context.SendActivityAsync(turnContext.Activity.Text);
}, cancellationToken);
}
}
}
}
}
}