diff --git a/src/samples/Teams/MessagingExtensionsSearch/Bots/TeamsMessagingExtensionsSearchBot.cs b/src/samples/Teams/MessagingExtensionsSearch/Bots/TeamsMessagingExtensionsSearchBot.cs index fb47768..da27b33 100644 --- a/src/samples/Teams/MessagingExtensionsSearch/Bots/TeamsMessagingExtensionsSearchBot.cs +++ b/src/samples/Teams/MessagingExtensionsSearch/Bots/TeamsMessagingExtensionsSearchBot.cs @@ -18,14 +18,9 @@ namespace MessagingExtensionsSearch.Bots { - public class TeamsMessagingExtensionsSearchBot : TeamsActivityHandler + public class TeamsMessagingExtensionsSearchBot(IConfiguration config) : TeamsActivityHandler { - public readonly string _baseUrl; - public TeamsMessagingExtensionsSearchBot(IConfiguration config) - { - _baseUrl = config["BaseUrl"]; - - } + public readonly string _baseUrl = config["BaseUrl"]; protected override async Task OnTeamsMessagingExtensionQueryAsync(ITurnContext turnContext, MessagingExtensionQuery query, CancellationToken cancellationToken) { @@ -33,7 +28,7 @@ protected override async Task OnTeamsMessagingExtens if (turnContext.Activity.Value != null) { - text = query?.Parameters?[0]?.Value as string ?? string.Empty; + text = query?.Parameters?[0]?.Value?.ToString() ?? string.Empty; switch (text) { @@ -57,17 +52,18 @@ protected override async Task OnTeamsMessagingExtens // The Preview is optional, if it includes a Tap, that will trigger the OnTeamsMessagingExtensionSelectItemAsync event back on this bot. var attachments = packages.Select(package => { - var cardValue = $"{{\"packageId\": \"{package.Item1}\", \"version\": \"{package.Item2}\", \"description\": \"{package.Item3}\", \"projectUrl\": \"{package.Item4}\", \"iconUrl\": \"{package.Item5}\"}}"; - var previewCard = new ThumbnailCard { Title = package.Item1, Tap = new CardAction { Type = "invoke", Value = cardValue } }; - if (!string.IsNullOrEmpty(package.Item5)) + var cardValue = $"{{\"packageId\": \"{package.Id}\", \"version\": \"{package.Version}\", \"description\": \"{PackageItem.NormalizeString(package.Description)}\", \"projectUrl\": \"{package.ProjectUrl}\", \"iconUrl\": \"{package.IconUrl}\"}}"; + + var previewCard = new ThumbnailCard { Title = package.Id, Tap = new CardAction { Type = "invoke", Value = cardValue } }; + if (!string.IsNullOrEmpty(package.IconUrl)) { - previewCard.Images = new List() { new CardImage(package.Item5, "Icon") }; + previewCard.Images = [new CardImage(package.IconUrl, "Icon")]; } var attachment = new MessagingExtensionAttachment { ContentType = HeroCard.ContentType, - Content = new HeroCard { Title = package.Item1 }, + Content = new HeroCard { Title = package.Id }, Preview = previewCard.ToAttachment() }; @@ -89,7 +85,6 @@ protected override async Task OnTeamsMessagingExtens protected override Task OnTeamsMessagingExtensionSelectItemAsync(ITurnContext turnContext, JsonElement 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 (packageId, version, description, projectUrl, iconUrl) = query.ToObject<(string, string, string, string, string)>(); string packageId = query.GetProperty("packageId").GetString(); string version = query.GetProperty("version").GetString(); string description = query.GetProperty("description").GetString(); @@ -103,16 +98,16 @@ protected override Task OnTeamsMessagingExtensionSel { Title = $"{packageId}, {version}", Subtitle = description, - Buttons = new List - { - new CardAction { Type = ActionTypes.OpenUrl, Title = "Nuget Package", Value = $"https://www.nuget.org/packages/{packageId}" }, + Buttons = + [ + new CardAction { Type = ActionTypes.OpenUrl, Title = "NuGet Package", Value = $"https://www.nuget.org/packages/{packageId}" }, new CardAction { Type = ActionTypes.OpenUrl, Title = "Project", Value = projectUrl }, - }, + ], }; if (!string.IsNullOrEmpty(iconUrl)) { - card.Images = new List() { new CardImage(iconUrl, "Icon") }; + card.Images = [new CardImage(iconUrl, "Icon")]; } var attachment = new MessagingExtensionAttachment @@ -133,11 +128,17 @@ protected override Task OnTeamsMessagingExtensionSel } // Generate a set of substrings to illustrate the idea of a set of results coming back from a query. - private async Task> FindPackages(string text) + private async Task> FindPackages(string text) { var obj = JsonObject.Parse(await (new HttpClient()).GetStringAsync($"https://azuresearch-usnc.nuget.org/query?q=id:{text}&prerelease=true")); var items = ProtocolJsonSerializer.ToObject>(obj["data"]); - return items.Select(item => (item["id"].ToString(), item["version"].ToString(), item["description"].ToString(), item["projectUrl"]?.ToString(), item["iconUrl"]?.ToString())); + return items.Select(item => new PackageItem() + { + Id = item["id"].ToString(), + Version = item["version"].ToString(), + Description = item["description"].ToString(), + ProjectUrl = item["projectUrl"]?.ToString(), + IconUrl = item["iconUrl"]?.ToString() }); } public MessagingExtensionResponse GetAdaptiveCard() @@ -249,4 +250,22 @@ public MessagingExtensionResponse GetResultGrid() }; } } + + class PackageItem + { + public string Id; + public string Version; + public string Description; + public string ProjectUrl; + public string IconUrl; + + public static string NormalizeString(string value) + { + return value + .Replace("\r\n", " ") + .Replace("\r", " ") + .Replace("\n", " ") + .Replace("\"", "\\\""); + } + } }