diff --git a/Lagrange.Core/Message/Entity/XmlEntity.cs b/Lagrange.Core/Message/Entity/XmlEntity.cs index e015e42c8..98bbf5ff9 100644 --- a/Lagrange.Core/Message/Entity/XmlEntity.cs +++ b/Lagrange.Core/Message/Entity/XmlEntity.cs @@ -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 IMessageEntity.PackElement() { @@ -22,12 +26,12 @@ IEnumerable IMessageEntity.PackElement() { RichMsg = new RichMsg { - ServiceId = 35, + ServiceId = ServiceId, Template1 = ZCompression.ZCompress(Xml, new byte[] { 0x01 }), } } }; - } + } IMessageEntity? IMessageEntity.UnpackElement(Elem elems) { diff --git a/Lagrange.Core/Message/MessageBuilder.cs b/Lagrange.Core/Message/MessageBuilder.cs index 5bf5fc34d..3c83cb618 100644 --- a/Lagrange.Core/Message/MessageBuilder.cs +++ b/Lagrange.Core/Message/MessageBuilder.cs @@ -166,6 +166,19 @@ public MessageBuilder Xml(string xml) return this; } + + /// + /// Add a xml entity (with custom serviceId) to the message chain (card message) + /// + /// The xml to be sent + /// The service id of the xml + public MessageBuilder Xml(string xml, int serviceId) + { + var xmlEntity = new XmlEntity(xml, serviceId); + _chain.Add(xmlEntity); + + return this; + } /// /// Add a image entity to the message chain diff --git a/Lagrange.OneBot/Message/Entity/XmlSegment.cs b/Lagrange.OneBot/Message/Entity/XmlSegment.cs new file mode 100644 index 000000000..20f9de6f4 --- /dev/null +++ b/Lagrange.OneBot/Message/Entity/XmlSegment.cs @@ -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); + } +} \ No newline at end of file