Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
CSharp/Microsoft.Bot.Schema.Teams/MessageActionsPayloadBodyExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// <copyright file="MessageActionsPayloadBodyExtensions.cs" company="Microsoft"> | ||
// Licensed under the MIT License. | ||
// </copyright> | ||
|
||
namespace Microsoft.Bot.Schema.Teams | ||
{ | ||
using System.Collections.Generic; | ||
using HtmlAgilityPack; | ||
|
||
/// <summary> | ||
/// MessageActionsPayloadBody extensions. | ||
/// </summary> | ||
|
||
public static class MessageActionsPayloadBodyExtensions | ||
{ | ||
private static readonly HashSet<string> textRestrictedHtmlTags = new HashSet<string> { "at", "attachment" }; | ||
|
||
/// <summary> | ||
/// Strip HTML tags from MessageActionsPayloadBody content. | ||
/// </summary> | ||
/// <param name="body">The MessageActionsPayloadBody.</param> | ||
/// <returns>Plain text content.</returns> | ||
public static string GetPlainTextContent(this MessageActionsPayloadBody body) | ||
{ | ||
var doc = new HtmlDocument(); | ||
doc.LoadHtml(body.content); | ||
return StripHtmlTags(doc.DocumentNode, textRestrictedHtmlTags); | ||
} | ||
|
||
private static string StripHtmlTags(HtmlNode node, ISet<string> tags) | ||
{ | ||
string result = ""; | ||
if (tags.Contains(node.Name)) | ||
{ | ||
result += node.OuterHtml; | ||
} | ||
else | ||
{ | ||
foreach (HtmlNode childNode in node.ChildNodes) | ||
{ | ||
if (childNode.NodeType == HtmlNodeType.Text) | ||
{ | ||
result += childNode.InnerText; | ||
} | ||
else | ||
{ | ||
result += StripHtmlTags(childNode, tags); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
} |