Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MessageExtension: Corrected package descriptions causing invalid json content #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@

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"];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this entry documented?
IE: what is "BaseURL" ?


protected override async Task<MessagingExtensionResponse> OnTeamsMessagingExtensionQueryAsync(ITurnContext<IInvokeActivity> turnContext, MessagingExtensionQuery query, CancellationToken cancellationToken)
{
var text = string.Empty;

if (turnContext.Activity.Value != null)
{
text = query?.Parameters?[0]?.Value as string ?? string.Empty;
text = query?.Parameters?[0]?.Value?.ToString() ?? string.Empty;

switch (text)
{
Expand All @@ -57,17 +52,18 @@ protected override async Task<MessagingExtensionResponse> 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<CardImage>() { 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()
};

Expand All @@ -89,7 +85,6 @@ protected override async Task<MessagingExtensionResponse> OnTeamsMessagingExtens
protected override Task<MessagingExtensionResponse> OnTeamsMessagingExtensionSelectItemAsync(ITurnContext<IInvokeActivity> 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();
Expand All @@ -103,16 +98,16 @@ protected override Task<MessagingExtensionResponse> OnTeamsMessagingExtensionSel
{
Title = $"{packageId}, {version}",
Subtitle = description,
Buttons = new List<CardAction>
{
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<CardImage>() { new CardImage(iconUrl, "Icon") };
card.Images = [new CardImage(iconUrl, "Icon")];
}

var attachment = new MessagingExtensionAttachment
Expand All @@ -133,11 +128,17 @@ protected override Task<MessagingExtensionResponse> OnTeamsMessagingExtensionSel
}

// Generate a set of substrings to illustrate the idea of a set of results coming back from a query.
private async Task<IEnumerable<(string, string, string, string, string)>> FindPackages(string text)
private async Task<IEnumerable<PackageItem>> 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<List<JsonObject>>(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()
Expand Down Expand Up @@ -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("\"", "\\\"");
}
}
}
Loading