0
votes

I am unable to send an Adaptivecard using the "OnTeamsMessagingExtensionSelectItemAsync" method in MS teams Messageextension. I can able to send the Thumbnail/Harocard, Please let me know if "OnTeamsMessagingExtensionSelectItemAsync" supports returning an AdaptiveCard.

private readonly AdaptiveCardCreator adaptiveCardCreator;
public AdaptiveCard CreateAdaptiveCard(
        string title,
        string imageUrl,
        string summary,
        string author,
        string buttonTitle,
        string buttonUrl)
    {
        var version = new AdaptiveSchemaVersion(1, 0);
        AdaptiveCard card = new AdaptiveCard(version);

        card.Body.Add(new AdaptiveTextBlock()
        {
            Text = title,
            Size = AdaptiveTextSize.Default,
            Weight = AdaptiveTextWeight.Bolder,
            Wrap = true,
        });
        card.Speak = title;

        if (!string.IsNullOrWhiteSpace(imageUrl))
        {
            card.Body.Add(new AdaptiveImage()
            {
                Url = new Uri(imageUrl, UriKind.RelativeOrAbsolute),
                Spacing = AdaptiveSpacing.Default,
                Size = AdaptiveImageSize.Stretch,
                AltText = string.Empty,
            });
        }

        if (!string.IsNullOrWhiteSpace(summary))
        {
            card.Body.Add(new AdaptiveTextBlock()
            {
                Text = summary,
                Wrap = true,
            });
        }

        if (!string.IsNullOrWhiteSpace(author))
        {
            card.Body.Add(new AdaptiveTextBlock()
            {
                Text = author,
                Size = AdaptiveTextSize.Small,
                Weight = AdaptiveTextWeight.Lighter,
                Wrap = true,
            });
        }

        if (!string.IsNullOrWhiteSpace(buttonTitle)
            && !string.IsNullOrWhiteSpace(buttonUrl))
        {
            card.Actions.Add(new AdaptiveOpenUrlAction()
            {
                Title = buttonTitle,
                Url = new Uri(buttonUrl, UriKind.RelativeOrAbsolute),
            });
        }

        return card;
    } protected override async Task<MessagingExtensionResponse> OnTeamsMessagingExtensionSelectItemAsync(ITurnContext<IInvokeActivity> turnContext, JObject query, CancellationToken cancellationToken)
    { var adaptiveCard = this.adaptiveCardCreator.CreateAdaptiveCard(
            title,
            imageLink,
            summary,
            author,
            buttonTitle,
            buttonLink
            );

        var attachment = new MessagingExtensionAttachment
        {
            ContentType = AdaptiveCard.ContentType,
            Content = adaptiveCard,
        };
return Task.FromResult(new MessagingExtensionResponse
        {
            ComposeExtension = new MessagingExtensionResult
            {
                Type = "result",
                AttachmentLayout = "list",
                Attachments = new List<MessagingExtensionAttachment> { attachment }
            }
        });

}

Sample Code: https://github.com/microsoft/BotBuilder-Samples/blob/master/samples/csharp_dotnetcore/50.teams-messaging-extensions-search/Bots/TeamsMessagingExtensionsSearchBot.cs

1

1 Answers

0
votes

You can use adaptive card by sending it as an attachment to thumbnail.

var adaptcard = new AdaptiveCard(new AdaptiveSchemaVersion(1, 2));
 var titleContainer = new AdaptiveContainer();
        titleContainer.Items.Add(new AdaptiveTextBlock
        {
            Text = $"{packageId}, {version}",
            Size = AdaptiveTextSize.Large,
            Weight = AdaptiveTextWeight.Bolder,
            Wrap = true
        });
        titleContainer.Items.Add(new AdaptiveImage { Url = new System.Uri(iconUrl) });
        adaptcard.Body.Add(titleContainer);
        adaptcard.Actions.Add(
            new AdaptiveOpenUrlAction { Title = "Nuget Package", Url = new System.Uri($"https://www.nuget.org/packages/{packageId}") }
        );
        var _previewCard = new ThumbnailCard { Title = $"{packageId}, {version}" };
        if (!string.IsNullOrEmpty(iconUrl))
        {
            _previewCard.Images = new List<CardImage>() { new CardImage(iconUrl, "Icon") };
        }
        var attachment = new MessagingExtensionAttachment
        {
            ContentType = AdaptiveCard.ContentType,
            Content = adaptcard,
            Preview = _previewCard.ToAttachment()
        };
        return Task.FromResult(new MessagingExtensionResponse
        {
            ComposeExtension = new MessagingExtensionResult
            {
                Type = "result",
                AttachmentLayout = "list",
                Attachments = new List<MessagingExtensionAttachment> { attachment }
            }
        });