private async Task SendFileCardAsync(ITurnContext turnContext, string filename, long filesize, CancellationToken cancellationToken)
        {
            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,
            };

            var asAttachment = new Attachment
            {
                Content     = fileCard,
                ContentType = FileConsentCard.ContentType,
                Name        = filename,
            };

            var replyActivity = turnContext.Activity.CreateReply();

            replyActivity.Attachments = new List <Attachment>()
            {
                asAttachment
            };
            await turnContext.SendActivityAsync(replyActivity, cancellationToken);
        }
Example #2
0
        public void FileConsentCardInitsWithNoArgs()
        {
            var fileConsentCard = new FileConsentCard();

            Assert.NotNull(fileConsentCard);
            Assert.IsType <FileConsentCard>(fileConsentCard);
        }
Example #3
0
        // Send consent card to user.
        private static Activity SendFileCardAsync(ITurnContext turnContext, string filename, long filesize)
        {
            var consentContext = new Dictionary <string, string>
            {
                { "filename", filename },
            };

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

            var asAttachment = new Microsoft.Bot.Schema.Attachment
            {
                Content     = fileCard,
                ContentType = FileConsentCard.ContentType,
                Name        = filename,
            };

            var replyActivity = turnContext.Activity.CreateReply();

            replyActivity.Attachments = new List <Microsoft.Bot.Schema.Attachment>()
            {
                asAttachment
            };
            return(replyActivity);
        }
 /// <summary>
 /// Creates a new attachment from <see cref="FileConsentCard"/>.
 /// </summary>
 /// <param name="card"> The instance of <see cref="FileConsentCard"/>.</param>
 /// <returns> The generated attachment.</returns>
 public static Attachment ToAttachment(this FileConsentCard card)
 {
     return(new Attachment
     {
         Content = card,
         ContentType = FileConsentCard.ContentType
     });
 }
        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);
        }
Example #6
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);
        }
Example #8
0
        public void FileConsentCardInits()
        {
            var  description    = "A text document about butterflies";
            long sizeInBytes    = 4000;
            var  acceptContext  = "free flow schema sent back in Value field of Activity with file upload consent";
            var  declineContext = "free flow schema sent back in Value field of Activity with decline of file upload";

            var fileConsentCard = new FileConsentCard(description, sizeInBytes, acceptContext, declineContext);

            Assert.NotNull(fileConsentCard);
            Assert.IsType <FileConsentCard>(fileConsentCard);
            Assert.Equal(description, fileConsentCard.Description);
            Assert.Equal(sizeInBytes, fileConsentCard.SizeInBytes);
            Assert.Equal(acceptContext, fileConsentCard.AcceptContext);
            Assert.Equal(declineContext, fileConsentCard.DeclineContext);
        }
        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);
            }
        }
Example #11
0
        private Attachment MakeTeamsFileConsentCardAttachment(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,
            };

            var asAttachment = new Attachment
            {
                Content     = fileCard,
                ContentType = FileConsentCard.ContentType,
                Name        = filename,
            };

            return(asAttachment);
        }
Example #12
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);
        }
Example #13
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));
            }
        }