public async Task StreamEventsForCommand(string token)
        {
            var caller  = Clients.Caller;
            var channel = ContentSubjectHelper.GetOrCreateChannel(token);

            channel.Subscribe(async content =>
            {
                await caller.SendAsync("outputReceived", content.Value);
            },
                              () =>
            {
                ContentSubjectHelper.DeleteExpired();
            });

#pragma warning disable CS4014,CS1998
            DotNetInteractiveProcessRunner.Instance.ExecuteEnvelope(token);
#pragma warning restore CS4014,CS1998

            await caller.SendAsync("outputEnded");
        }
Esempio n. 2
0
#pragma warning disable CS1998
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext,
                                                             CancellationToken cancellationToken)
        {
            var value       = turnContext.Activity;
            var attachments = turnContext.Activity.Attachments;

            if (turnContext.Activity.Value == null) // someone typed in something, it isn't a card
            {
                var content = turnContext.Activity.Text;
                var code    = CheckForCode(content);

                var conversationReference = turnContext.Activity.GetConversationReference();
                var mention = new Mention
                {
                    Mentioned = turnContext.Activity.From,
                    Text      = $"<at>{turnContext.Activity.From.Name}</at>",
                };

                if (!string.IsNullOrEmpty(code))
                {
                    if (DotNetInteractiveProcessRunner.Instance.CanExecuteCode)
                    {
                        var submissionToken = Guid.NewGuid().ToString("N");
                        var messageText     = string.Empty;
                        var user            = UserGame.GetOrCreateUser(mention, turnContext.Activity.From);
                        if (UserGame.CurrentChatUser?.Id != user.Id)
                        {
                            UserGame.CurrentChatUser = user;
                            messageText = $"Hey {mention.Text} It looks like you're typing some code. Let me run it for you! ๐Ÿ˜Š";
                        }
                        else
                        {
                            messageText = UserGame.GetMessageForUser(mention);
                        }

                        await turnContext.Adapter.ContinueConversationAsync(_botId, conversationReference, async (context, token) =>
                        {
                            var message = MessageFactory.Text(messageText);
                            if (messageText.Contains(mention.Text))
                            {
                                message.Entities.Add(mention);
                            }
                            await context.SendActivityAsync(message, token);
                        }, cancellationToken);

                        // build the envelope
                        var submitCode = new SubmitCode(code);
                        submitCode.SetToken(submissionToken);
                        var envelope = KernelCommandEnvelope.Create(submitCode);
                        var channel  = ContentSubjectHelper.GetOrCreateChannel(submissionToken);
                        EnvelopeHelper.StoreEnvelope(submissionToken, envelope);
                        var cardSent = false;
                        channel
                        .Timeout(DateTimeOffset.UtcNow.Add(TimeSpan.FromMinutes(1)))
                        .Buffer(TimeSpan.FromSeconds(1))
                        .Subscribe(
                            onNext: async formattedValues =>
                        {
                            turnContext.Adapter.ContinueConversationAsync(_botId, conversationReference,
                                                                          (context, token) =>
                            {
                                if (formattedValues.Count > 0)
                                {
                                    var hasHtml = formattedValues.Any(f => f.MimeType == HtmlFormatter.MimeType);

                                    if (hasHtml)
                                    {
                                        if (!cardSent)
                                        {
                                            cardSent = true;
                                            var card = new HeroCard
                                            {
                                                Title    = "Your output is too awesome ๐Ÿ˜Ž",
                                                Subtitle = "Use the viewer to see it.",
                                                Buttons  = new List <CardAction>
                                                {
                                                    new TaskModuleAction("Open Viewer",
                                                                         new { data = submissionToken })
                                                },
                                            }.ToAttachment();
                                            var message = MessageFactory.Attachment(card);
                                            context.SendActivityAsync(message, token).Wait();
                                        }
                                    }
                                    else
                                    {
                                        var content = string.Join("\n", formattedValues.Select(f => f.Value));
                                        var message = MessageFactory.Text($"```\n{content.HtmlEncode()}");
                                        context.SendActivityAsync(message, token).Wait();
                                    }
                                }

                                return(Task.CompletedTask);
                            }, cancellationToken).Wait();
                        }, onCompleted: async() =>
                        {
                            await turnContext.Adapter.ContinueConversationAsync(_botId, conversationReference, async(context, token) =>
                            {
                                await Task.Delay(1000);
                                var message = MessageFactory.Text($"{mention.Text} all done here ๐Ÿ‘");
                                message.Entities.Add(mention);
                                await context.SendActivityAsync(message, token);
                            }, cancellationToken);
                        },
                            onError: async error =>
                        {
                            await turnContext.Adapter.ContinueConversationAsync(_botId, conversationReference, async(context, token) =>
                            {
                                await Task.Delay(1000);
                                var message = MessageFactory.Text($@"{mention.Text} there were some issues ๐Ÿ‘Ž :\n {error.Message}");
                                message.Entities.Add(mention);
                                await context.SendActivityAsync(message, token);
                            }, cancellationToken);
                        });

                        user.IncrementCodeSubmissionCount();
                        await DotNetInteractiveProcessRunner.Instance.ExecuteEnvelope(submissionToken);
                    }
                    else
                    {
                        await turnContext.Adapter.ContinueConversationAsync(_botId, conversationReference, async (context, token) =>
                        {
                            var message = MessageFactory.Text($"Sorry {mention.Text} cannot execute your code now. ๐Ÿ˜“");
                            message.Entities.Add(mention);
                            await context.SendActivityAsync(message, token);
                        }, cancellationToken);
                    }
                }
                else if (string.IsNullOrWhiteSpace(DotNetInteractiveProcessRunner.Instance.SessionLanguage))
                {
                    var card   = CardUtilities.CreateAdaptiveCardAttachment(CardJsonFiles.SelectLanguage);
                    var attach = MessageFactory.Attachment(card);
                    await turnContext.SendActivityAsync(attach, cancellationToken);
                }
                else if (content.Contains("๐Ÿ‘Š"))
                {
                    var mentioned = turnContext.Activity.GetMentions()?.FirstOrDefault(m => m.Mentioned.Id.EndsWith(_botId));
                    if (mentioned != null)
                    {
                        await turnContext.Adapter.ContinueConversationAsync(_botId, conversationReference,
                                                                            async (context, token) =>
                        {
                            var message = MessageFactory.Text($"{mention.Text} back at you my friend! ๐Ÿ‘Š");
                            message.Entities.Add(mention);
                            await context.SendActivityAsync(message, token);
                        }, cancellationToken);
                    }
                }
            }
            else
            {
                var userAction = turnContext.Activity.Value;

                if (((JObject)userAction).Value <string>("userAction").Equals("SelectLanguage"))
                {
                    if (string.IsNullOrWhiteSpace(DotNetInteractiveProcessRunner.Instance.SessionLanguage))
                    {
                        var language = ((JObject)userAction).Value <string>("language");
                        DotNetInteractiveProcessRunner.Instance.SessionLanguage = language;
                        var languageLabel = ((JObject)userAction).Value <string>("languageLabel");
                        var message       = MessageFactory.Text($"All set. Let's write some {DotNetInteractiveProcessRunner.Instance.SessionLanguage} code together! ๐Ÿค˜๐Ÿป");
                        await turnContext.SendActivityAsync(message, cancellationToken);
                    }
                }
            }
        }