Skip to content

Commit

Permalink
[All] Expanded XmlEntity and XmlSegment (#689)
Browse files Browse the repository at this point in the history
Co-authored-by: 思思 <[email protected]>
  • Loading branch information
pk5ls20 and sisi0318 authored Nov 21, 2024
1 parent ad18e88 commit 606f376
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
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);
}
}

0 comments on commit 606f376

Please sign in to comment.