Ejemplo n.º 1
0
        public static async Task ReplyWithChart(IDialogContext context, string workbookId, string worksheetId, WorkbookChart chart)
        {
            try
            {
                // Create and save user nonce
                var userNonce = (Guid.NewGuid()).ToString();
                context.ConversationData.SetValue <ChartAttachment>(
                    userNonce,
                    new ChartAttachment()
                {
                    WorkbookId  = workbookId,
                    WorksheetId = worksheetId,
                    ChartId     = chart.Id
                });
                await context.FlushAsync(context.CancellationToken);

                // Reply with chart URL attached
                var reply = context.MakeMessage();
                reply.Recipient.Id = (reply.Recipient.Id != null) ? reply.Recipient.Id : (string)(HttpContext.Current.Items["UserId"]);

                var image = new Microsoft.Bot.Connector.Attachment()
                {
                    ContentType = "image/png",
                    ContentUrl  = $"{RequestHelper.RequestUri.Scheme}://{RequestHelper.RequestUri.Authority}/api/{reply.ChannelId}/{reply.Conversation.Id}/{reply.Recipient.Id}/{userNonce}/image"
                };

                reply.Attachments.Add(image);
                await context.PostAsync(reply);
            }
            catch (Exception ex)
            {
                await context.PostAsync($"Sorry, something went wrong getting the **{chart.Name}** chart ({ex.Message})");
            }
        }
Ejemplo n.º 2
0
 public async Task Reminder(IDialogContext context, LuisResult result)
 {
     context.Reset();
     context.ConversationData.Clear();
     context.UserData.Clear();
     context.PrivateConversationData.Clear();
     await context.FlushAsync(CancellationToken.None);
 }
Ejemplo n.º 3
0
        private async Task SaveAnswer(IDialogContext context, IMessageActivity message)
        {
            var question = context.PrivateConversationData.GetValue <string>(Constants.QuestionToAnswerKey);

            var typing = context.MakeMessage();

            typing.Type = ActivityTypes.Typing;
            await context.PostAsync(typing);

            await this.updateKnowledgeBaseService.AddAnswer(question, message.Text);

            context.PrivateConversationData.RemoveValue(Constants.IsAnsweringKey);
            context.PrivateConversationData.RemoveValue(Constants.QuestionToAnswerKey);

            if (ConversationHelper.IsSlackChannel(message))
            {
                var responseUrl = context.PrivateConversationData.GetValue <string>(Constants.SlackResponseUrlKey);

                context.PrivateConversationData.RemoveValue(Constants.SlackResponseUrlKey);

                var interactiveMessage = new SlackInteractiveMessage
                {
                    ResponseType   = "in_channel",
                    DeleteOriginal = true,
                    Text           = $"Wow! I just learnt something new. Thanks {message.From.Name}!",
                    Attachments    =
                    {
                        new SlackAttachment
                        {
                            Color = "#36a64f",
                            Title = $"{question}",
                            Text  = message.Text
                        }
                    }
                };

                await SendSlackInteractiveMessage(responseUrl, interactiveMessage);
            }
            else
            {
                var reply = context.MakeMessage();
                reply.Text        = $"Wow! I just learnt something new. Thanks {message.From.Name}!";
                reply.Attachments = new List <Attachment>
                {
                    new HeroCard
                    {
                        Title = question,
                        Text  = message.Text,
                    }.ToAttachment()
                };

                await context.PostAsync(reply);
            }

            await context.FlushAsync(CancellationToken.None);

            context.Wait(this.MessageReceivedAsync);
        }
Ejemplo n.º 4
0
        public async Task ClearData(IDialogContext context, LuisResult result)
        {
            context.ConversationData.Clear();
            context.UserData.Clear();
            await context.FlushAsync(CancellationToken.None);

            await context.PostAsync($"OK. I've deleted all data I had of you. It's true.");

            context.Wait(MessageReceived);
        }
Ejemplo n.º 5
0
        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result)
        {
            var activity = await result as Activity;
            var answer   = await _qnaHandler.Handle(activity.Text);

            await context.PostAsync(answer.Answer);

            await context.FlushAsync(System.Threading.CancellationToken.None);

            context.Wait(MessageReceivedAsync);
        }
Ejemplo n.º 6
0
        public async Task None(IDialogContext context, LuisResult result)
        {
            var factory = new DialogFactory();
            AzureBotLuisDialog <string> dialog = await factory.Create(result.Query);

            if (dialog != null)
            {
                var message = context.MakeMessage();
                message.Text = userToBot;

                var accessToken = await context.GetAccessToken(resourceId.Value);

                if (string.IsNullOrEmpty(accessToken))
                {
                    return;
                }
                await context.Forward(dialog, this.ResumeAfterForward, message, CancellationToken.None);
            }
            else
            {
                if (new[] { "cancel", "reset", "restart", "kill", "undo", "start over" }.Any(c => result.Query.Contains(c)))
                {
                    context.Reset();
                    context.ConversationData.Clear();
                    context.UserData.Clear();
                    context.PrivateConversationData.Clear();
                    await context.FlushAsync(CancellationToken.None);

                    await context.PostAsync($"Alright, I've reset everything, let's try again.");

                    await this.Help(context, new LuisResult());
                }
                else
                {
                    string message = $"Sorry, I did not understand '{result.Query}'. Type 'help' if you need assistance.";
                    await context.PostAsync(message);

                    context.Wait(MessageReceived);
                }
            }
        }
Ejemplo n.º 7
0
        public async Task InterviewDone(IDialogContext context, IAwaitable <bool> confirmation)
        {
            if (await confirmation)
            {
                //TODO: find way to properly start new conversation. this will do it in weird way
                await context.PostAsync("Seems we are done");

                await context.PostAsync("Enjoy the rest of your day");

                await context.FlushAsync(context.CancellationToken);

                context.Wait(MessageReceivedAsync);
            }
            else
            {
                await context.PostAsync("O, let me restart the interview");

                //TODO: find a better way to do this, don't want to call startasync ourselves
                await StartAsync(context);
            }
        }
Ejemplo n.º 8
0
        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> result)
        {
            var message = await result;

            context.PrivateConversationData.TryGetValue <bool>(Constants.IsAnsweringKey, out bool isAnswering);

            if (isAnswering)
            {
                await this.SaveAnswer(context, message);
            }
            else if ((ConversationHelper.IsSlackChannel(message) && (message.ChannelData == null || message.ChannelData.Payload == null)) || !ConversationHelper.IsUserGoingToAnswerQuestion(message))
            {
                var text = message.Conversation.IsGroup.GetValueOrDefault()
                ? $"Oops. I dont know how to answer that. Anyone willing to help?\n\n**{message.Text}**"
                : $"Oops. I dont know how to answer that. Do you know the answer to **{message.Text}**?";

                // in reality we can just check if user is going to answer a question and remove the Slack specific check
                var askForHelpReply = context.MakeMessage();

                askForHelpReply.Text        = text;
                askForHelpReply.Attachments = new List <Attachment>
                {
                    new HeroCard
                    {
                        Buttons = new List <CardAction>
                        {
                            // sending the question as part of the button. Not required for Slack but a quick workaround for other channels.
                            new CardAction(ActionTypes.PostBack, "Answer", value: $"{Constants.WikiQuestionKey}{message.Text}")
                        }
                    }.ToAttachment()
                };

                await context.PostAsync(askForHelpReply);

                context.Wait(this.MessageReceivedAsync);
            }
            else
            {
                string user = message.From.Name;

                // the value of the button should contain the question
                string question = message.Text.Substring(Constants.WikiQuestionKey.Length);

                context.PrivateConversationData.SetValue(Constants.IsAnsweringKey, true);
                context.PrivateConversationData.SetValue(Constants.QuestionToAnswerKey, question);

                if (ConversationHelper.IsSlackChannel(message))
                {
                    string responseUrl = message.ChannelData.Payload.response_url;

                    context.PrivateConversationData.SetValue(Constants.SlackResponseUrlKey, responseUrl);
                    await context.FlushAsync(CancellationToken.None);

                    // just showing how to access the data from the Slack payload
                    user = message.ChannelData.Payload.user.name;

                    // question = message.ChannelData.Payload.original_message.text.ToString().Replace("*", string.Empty).Replace("\n", string.Empty);
                    var interactiveMessage = new SlackInteractiveMessage
                    {
                        ResponseType    = "in_channel",
                        ReplaceOriginal = true,
                        Text            = message.Conversation.IsGroup.GetValueOrDefault()
                            ? "Oops. I dont know how to answer that. Anyone willing to help?"
                            : "Oops. I dont know how to answer that. Do you know the answer?",
                        Attachments =
                        {
                            new SlackAttachment
                            {
                                Color = "#36a64f",
                                Title = question,
                                Text  = $"@{user} is answering"
                            }
                        }
                    };

                    await SendSlackInteractiveMessage(responseUrl, interactiveMessage);
                }
                else
                {
                    await context.FlushAsync(CancellationToken.None);

                    await context.PostAsync($"@{user} is answering");
                }

                context.Wait(this.MessageReceivedAsync);
            }
        }
 public Task FlushAsync(CancellationToken cancellationToken)
 {
     return(context.FlushAsync(cancellationToken));
 }
Ejemplo n.º 10
0
 public Task FlushAsync(CancellationToken cancellationToken)
 {
     return(_original.FlushAsync(cancellationToken));
 }