示例#1
0
        protected override Task <MessagingExtensionActionResponse> OnTeamsMessagingExtensionFetchTaskAsync(ITurnContext <IInvokeActivity> turnContext, MessagingExtensionAction action, CancellationToken cancellationToken)
        {
            switch (action.CommandId)
            {
            // These commandIds are defined in the Teams App Manifest.
            case TeamsCommands.TakeQuickNote:
                var quickNoteCard = NoteCardFactory.GetAdaptiveCard("NewNoteTemplate.json", new Note());

                return(CreateActionResponse("Create quick note", quickNoteCard));

            case TeamsCommands.NoteFromMessage:
                var converter = new Converter();
                var newNote   = new Note
                {
                    Title    = FixString(new string(HtmlUtilities.ConvertToPlainText(action.MessagePayload.Body.Content).Take(42).ToArray())),
                    NoteBody = FixString(converter.Convert(action.MessagePayload.Body.Content)),
                };
                var newNoteCard = NoteCardFactory.GetAdaptiveCard("NewNoteTemplate.json", newNote);

                return(CreateActionResponse("Create note from message", newNoteCard));

            default:
                throw new NotImplementedException($"Invalid Fetch CommandId: {action.CommandId}");
            }
        }
        protected override async Task <MessagingExtensionResponse> OnTeamsMessagingExtensionSelectItemAsync(ITurnContext <IInvokeActivity> turnContext, JObject query, CancellationToken cancellationToken)
        {
            // The Preview card's Tap should have a Value property assigned, this will be returned to the bot in this event.
            var selectedNote = query.ToObject <Note>();

            if (selectedNote == null)
            {
                throw new Exception("Unable to create a note from the selected item.");
            }

            var card = NoteCardFactory.GetAdaptiveCard("NoteTemplate.json", selectedNote);

            var fromMessage = (AdaptiveTextBlock)card.Body.Find(ae => ae.Id == "FromMessage");

            fromMessage.Text = $"{turnContext.Activity.From.Name} shared a note with you.";

            if (string.IsNullOrWhiteSpace(selectedNote.MessageLinkUrl))
            {
                // strip the action
                card.Actions.RemoveAt(0);
            }

            var imageElement = card.Body.FindAll(element => element.Id == "IconUrl").FirstOrDefault();

            var attachment = new Attachment
            {
                Content     = card,
                ContentType = AdaptiveCard.ContentType,
            };

            var messageActivity = MessageFactory.Attachment(attachment).ApplyConversationReference(turnContext.Activity.GetConversationReference());
            await turnContext.SendActivityAsync(messageActivity, cancellationToken);

            // TODO maybe do this? https://stackoverflow.com/questions/57116935/how-to-use-adaptive-cards-on-teams-messaging-extension
            return(new MessagingExtensionResponse());

            // We take every row of the results and wrap them in cards wrapped in in MessagingExtensionAttachment objects.
            // The Preview is optional, if it includes a Tap, that will trigger the OnTeamsMessagingExtensionSelectItemAsync event back on this bot.
            //var card = new ThumbnailCard
            //{
            //    //Title = selectedNote.Title,
            //    Images = new List<CardImage> { new CardImage(GetNoteIconUrl(selectedNote), "Icon") },
            //    //Subtitle = $"On {selectedNote.MessageActionsPayload.CreatedDateTime}, created by {selectedNote.MessageActionsPayload.From.Application.DisplayName}",
            //    Text = selectedNote.NoteBody,
            //    Buttons = string.IsNullOrWhiteSpace(selectedNote.MessageLinkUrl)
            //        ? null
            //        : new List<CardAction>
            //        {
            //            new CardAction
            //            {
            //                Type = ActionTypes.OpenUrl,
            //                Title = "Go to conversation",
            //                Value = selectedNote.MessageLinkUrl
            //            },
            //        }
            //};

            //var attachment = new MessagingExtensionAttachment
            //{
            //    ContentType = ThumbnailCard.ContentType,
            //    Content = card,
            //};

            //return new MessagingExtensionResponse
            //{
            //    ComposeExtension = new MessagingExtensionResult
            //    {
            //        Type = "result",
            //        AttachmentLayout = "list",
            //        Attachments = new List<MessagingExtensionAttachment> { attachment }
            //    }
            //};
        }