1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
using ArcaneLibs;
using LibMatrix.EventTypes.Spec;
namespace LibMatrix.Helpers;
public class MessageBuilder(string msgType = "m.text", string format = "org.matrix.custom.html") {
private RoomMessageEventContent Content { get; set; } = new() {
MessageType = msgType,
Format = format
};
public RoomMessageEventContent Build() => Content;
public MessageBuilder WithBody(string body) {
Content.Body += body;
Content.FormattedBody += body;
return this;
}
public MessageBuilder WithHtmlTag(string tag, string body, Dictionary<string, string>? attributes = null) {
Content.Body += body;
Content.FormattedBody += $"<{tag}";
if (attributes != null)
foreach (var (key, value) in attributes)
Content.FormattedBody += $" {key}=\"{value}\"";
Content.FormattedBody += $">{body}</{tag}>";
return this;
}
public MessageBuilder WithHtmlTag(string tag, Action<MessageBuilder> bodyBuilder, Dictionary<string, string>? attributes = null) {
Content.FormattedBody += $"<{tag}";
if (attributes != null)
foreach (var (key, value) in attributes)
Content.FormattedBody += $" {key}=\"{value}\"";
Content.FormattedBody += ">";
bodyBuilder(this);
Content.FormattedBody += $"</{tag}>";
return this;
}
public MessageBuilder WithColoredBody(string color, string body) {
Content.Body += body;
Content.FormattedBody += $"<font color=\"{color}\">{body}</font>";
return this;
}
public MessageBuilder WithColoredBody(string color, Action<MessageBuilder> bodyBuilder) {
Content.FormattedBody += $"<font color=\"{color}\">";
bodyBuilder(this);
Content.FormattedBody += "</font>";
return this;
}
public MessageBuilder WithCustomEmoji(string mxcUri, string name) {
Content.Body += $"{{{name}}}";
Content.FormattedBody += $"<img data-mx-emoticon height=\"32\" src=\"{mxcUri}\" alt=\"{name}\" title=\"{name}\" />";
return this;
}
public MessageBuilder WithRainbowString(string text, byte skip = 1, int offset = 0, double lengthFactor = 255.0, bool useLength = true) {
if (useLength) {
lengthFactor = text.Length;
}
// HslaColorInterpolator interpolator = new((0, 255, 128, 255), (255, 255, 128, 255));
// // RainbowEnumerator enumerator = new(skip, offset, lengthFactor);
// for (int i = 0; i < text.Length; i++) {
// // var (r, g, b) = enumerator.Next();
// // var (r,g,b,a) = interpolator.Interpolate((i+offset) * skip, lengthFactor).ToRgba();
// // Console.WriteLine($"RBA: {r} {g} {b} {a}");
// // Content.FormattedBody += $"<font color=\"#{r:X2}{g:X2}{b:X2}\">{text[i]}</font>";
// }
return this;
}
public MessageBuilder WithCodeBlock(string code, string language = "plaintext") {
Content.Body += code;
Content.FormattedBody += $"<pre><code class=\"language-{language}\">{code}</code></pre>";
return this;
}
public MessageBuilder WithCollapsibleSection(string title, string body) {
Content.Body += body;
Content.FormattedBody += $"<details><summary>{title}</summary>{body}</details>";
return this;
}
public MessageBuilder WithCollapsibleSection(string title, Action<MessageBuilder> bodyBuilder) {
Content.FormattedBody += $"<details><summary>{title}</summary>";
bodyBuilder(this);
Content.FormattedBody += "</details>";
return this;
}
}
|