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

[All] Expanded XmlEntity and XmlSegment #689

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions Lagrange.Core/Message/Entity/XmlEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ namespace Lagrange.Core.Message.Entity;
public class XmlEntity : IMessageEntity
{
public string Xml { get; set; }

public int ServiceId { get; set; } = 35;

public XmlEntity() => Xml = "";

public XmlEntity(string xml) => Xml = xml;

public XmlEntity(string xml, int serviceId) => (Xml, ServiceId) = (xml, serviceId);

IEnumerable<Elem> IMessageEntity.PackElement()
{
Expand All @@ -22,12 +26,12 @@ IEnumerable<Elem> IMessageEntity.PackElement()
{
RichMsg = new RichMsg
{
ServiceId = 35,
ServiceId = ServiceId,
Template1 = ZCompression.ZCompress(Xml, new byte[] { 0x01 }),
}
}
};
}
}

IMessageEntity? IMessageEntity.UnpackElement(Elem elems)
{
Expand Down
13 changes: 13 additions & 0 deletions Lagrange.Core/Message/MessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ public MessageBuilder Xml(string xml)

return this;
}

/// <summary>
/// Add a xml entity (with custom serviceId) to the message chain (card message)
/// </summary>
/// <param name="xml">The xml to be sent</param>
/// <param name="serviceId">The service id of the xml</param>
public MessageBuilder Xml(string xml, int serviceId)
{
var xmlEntity = new XmlEntity(xml, serviceId);
_chain.Add(xmlEntity);

return this;
}

/// <summary>
/// Add a image entity to the message chain
Expand Down
31 changes: 31 additions & 0 deletions Lagrange.OneBot/Message/Entity/XmlSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Text.Json.Serialization;
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;

namespace Lagrange.OneBot.Message.Entity;

[Serializable]
public partial class XmlSegment(string xml, int serviceid)
{
public XmlSegment() : this("", 35) { }

[JsonPropertyName("data")] [CQProperty] public string Xml { get; set; } = xml;

[JsonPropertyName("service_id")] [CQProperty] public int ServiceId { get; set; } = serviceid;
}

[SegmentSubscriber(typeof(XmlEntity), "xml")]
public partial class XmlSegment : SegmentBase
{
public override void Build(MessageBuilder builder, SegmentBase segment)
{
if (segment is XmlSegment xml) builder.Xml(xml.Xml, xml.ServiceId);
}

public override SegmentBase FromEntity(MessageChain chain, IMessageEntity entity)
{
if (entity is not XmlEntity xmlEntity) throw new ArgumentException("Invalid entity type.");

return new XmlSegment(xmlEntity.Xml, xmlEntity.ServiceId);
}
}
Loading