Skip to content

Super simple example demonstrating how little code is needed to proxy a conversation between users on different channels

License

Notifications You must be signed in to change notification settings

EricDahlvang/SuperSimpleProxyBot

Repository files navigation

SimpleProxyBot

This sample crudely demonstrates how to forward messages from every conversation to all conversations.

    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);
                    }
                }
            }
        }
    }

About

Super simple example demonstrating how little code is needed to proxy a conversation between users on different channels

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published