public async Task StartAsync(IDialogContext context)
        {
            var reply = context.MakeMessage();

            var text       = context.Activity.GetTextWithoutCommand(BotCommands.CandidateSummaryDialog);
            var candidates = await _candidateService.Search(text, 15, context.CancellationToken);

            if (candidates.Any())
            {
                reply.Attachments = new List <Attachment>();
                if (candidates.Count == 1)
                {
                    var candidate = candidates[0];
                    // Create file consent card
                    var consentContext = new FileConsentContext
                    {
                        CandidateId = candidate.CandidateId
                    };

                    var fileConsentCard = new FileConsentCard
                    {
                        Name           = SanitizeFileName($"{candidate.Name} Summary.txt"),
                        Description    = $"Here is summary for {candidate.Name}",
                        SizeInBytes    = Encoding.UTF8.GetBytes(candidate.Summary).Length,
                        AcceptContext  = consentContext,
                        DeclineContext = consentContext
                    };

                    reply.Attachments.Add(fileConsentCard.ToAttachment());
                }
                else
                {
                    var cardListItems = _mapper.Map <List <CardListItem> >(candidates,
                                                                           opt => opt.Items["botCommand"] = BotCommands.CandidateSummaryDialog);

                    var attachment = new Attachment
                    {
                        ContentType = ListCard.ContentType,
                        Content     = new ListCard
                        {
                            Title = "Please select candidate:",
                            Items = cardListItems
                        }
                    };

                    reply.Attachments.Add(attachment);
                }
            }
            else
            {
                reply.Text = "You don't have such candidates.";
            }

            await context.PostAsync(reply);

            context.Done(string.Empty);
        }
Exemple #2
0
        public async Task CardTests_FileConsentCardAsync()
        {
            FileConsentCard fileConsentCard = new FileConsentCard
            {
                Description = "File consent",
                SizeInBytes = 1024,
            };

            Attachment attachment = fileConsentCard.ToAttachment();

            Assert.AreEqual(FileConsentCard.ContentType, attachment.ContentType);
            await TestHelpers.TestAttachmentAsync(attachment).ConfigureAwait(false);
        }
        public void CardTests_FileConsentCard()
        {
            FileConsentCard fileConsentCard = new FileConsentCard
            {
                Description = "File consent",
                SizeInBytes = 1024
            };

            Attachment attachment = fileConsentCard.ToAttachment();

            Assert.AreEqual(FileConsentCard.ContentType, attachment.ContentType);
            this.TestCard(attachment);
        }
        public void CardTests_FileConsentCard()
        {
            FileConsentCard fileConsentCard = new FileConsentCard
            {
                Description    = "File consent",
                SizeInBytes    = 1024,
                Name           = "filename.txt",
                AcceptContext  = new object(),
                DeclineContext = new object(),
            };

            Attachment attachment = fileConsentCard.ToAttachment();

            Assert.AreEqual(FileConsentCard.ContentType, attachment.ContentType);
            Assert.AreEqual("filename.txt", attachment.Name);
            this.TestCard(attachment);
        }
        private static async Task HandleResumeCommand(IDialogContext context, string[] keywords)
        {
            if (keywords.Length > 0)
            {
                string name = string.Join(" ", keywords).ToLower();

                //
                //  Access the file from some storage location and capture its metadata
                //
                var fileID   = "abc";
                var fileSize = 1500;


                IMessageActivity reply = context.MakeMessage();
                reply.Attachments = new List <Attachment>();

                JObject acceptContext = new JObject();
                // Fill in any additional context to be sent back when the user accepts the file.
                acceptContext["fileId"] = fileID;
                acceptContext["name"]   = name;

                JObject declineContext = new JObject();
                // Fill in any additional context to be sent back when the user declines the file.

                FileConsentCard card = new FileConsentCard()
                {
                    Name           = $"{name} resume.txt",
                    AcceptContext  = acceptContext,
                    DeclineContext = declineContext,
                    SizeInBytes    = fileSize,
                    Description    = $"Here is the resume for {name}"
                };

                reply.Attachments.Add(card.ToAttachment());

                // A production bot would save the reply id so it can be updated later with file send status
                // https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-state?view=azure-bot-service-3.0
                //
                //var consentMessageReplyId = (reply as Activity).Id;
                //var consentMessageReplyConversationId = reply.Conversation.Id;


                await context.PostAsync(reply);
            }
        }
Exemple #6
0
        private async Task SendFileCardAsync(ITurnContext turnContext, string filename, long filesize)
        {
            var consentContext = new Dictionary <string, string>
            {
                { "filename", filename },
            };

            var fileCard = new FileConsentCard
            {
                Description    = "This is the file I want to send you",
                SizeInBytes    = filesize,
                AcceptContext  = consentContext,
                DeclineContext = consentContext,
            };

            Activity replyActivity = turnContext.Activity.CreateReply();

            replyActivity.Attachments = new List <Attachment>()
            {
                fileCard.ToAttachment(filename),
            };

            await turnContext.SendActivityAsync(replyActivity).ConfigureAwait(false);
        }
Exemple #7
0
        /// <summary>
        /// Send the OCR result to the user.
        /// </summary>
        /// <param name="context">Dialog context</param>
        /// <param name="operation">OCR task that yields an OCR result</param>
        /// <param name="filename">Suggested filename for the result</param>
        /// <returns>Tracking task</returns>
        private async Task SendOcrResultAsync(IDialogContext context, Task <OcrResult> operation, string filename = null)
        {
            try
            {
                var result = await operation;

                var text = this.GetRecognizedText(result);
                if (text.Length > 0)
                {
                    // Save the OCR result in conversation data
                    var resultData = new OcrResultData
                    {
                        ResultId = Guid.NewGuid().ToString(),
                        Text     = text,
                    };
                    context.ConversationData.SetValue(OcrResultKey, resultData);

                    // Create file consent card
                    var consentContext = new FileConsentContext
                    {
                        ResultId = resultData.ResultId,
                    };
                    var consentCard = new FileConsentCard
                    {
                        Name           = filename ?? "result.txt",
                        Description    = "Text recognized from the image",
                        SizeInBytes    = Encoding.UTF8.GetBytes(text).Length,
                        AcceptContext  = consentContext,
                        DeclineContext = consentContext,
                    };

                    // Get a human-readable name for the language
                    string languageName = result.Language;
                    try
                    {
                        var cultureInfo = CultureInfo.GetCultureInfo(result.Language);
                        languageName = cultureInfo.EnglishName;
                    }
                    catch (CultureNotFoundException)
                    {
                        // Ignore unknown language codes
                    }

                    // Send the response to the user, asking for permission to send the OCR result as a file
                    var reply = context.MakeMessage();
                    reply.Text        = string.Format("I found {0} text in that image.", languageName);
                    reply.Attachments = new List <Attachment> {
                        consentCard.ToAttachment()
                    };
                    await context.PostAsync(reply);
                }
                else
                {
                    await context.PostAsync("I didn't find any text in that picture.");
                }
            }
            catch (Exception ex)
            {
                await context.PostAsync(string.Format("There was a problem analyzing the image: {0}", ex.Message));
            }
        }