mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-01 12:00:34 +08:00
xml and Json messages support
This commit is contained in:
parent
21abfe4a64
commit
584b5e9822
@ -273,9 +273,9 @@ internal fun MessageChain.toRichTextElems(forGroup: Boolean): MutableList<ImMsgB
|
|||||||
}
|
}
|
||||||
this.forEach(::transformOneMessage)
|
this.forEach(::transformOneMessage)
|
||||||
|
|
||||||
// if(this.any<QuoteReply>()){
|
if (this.any<RichMessage>()) {
|
||||||
elements.add(ImMsgBody.Elem(generalFlags = ImMsgBody.GeneralFlags(pbReserve = "78 00 F8 01 00 C8 02 00".hexToBytes())))
|
elements.add(ImMsgBody.Elem(generalFlags = ImMsgBody.GeneralFlags(pbReserve = "08 09 78 00 C8 01 00 F0 01 00 F8 01 00 90 02 00 C8 02 00 98 03 00 A0 03 20 B0 03 00 C0 03 00 D0 03 00 E8 03 00 8A 04 02 08 03 90 04 80 80 80 10 B8 04 00 C0 04 00".hexToBytes())))
|
||||||
// }
|
} else elements.add(ImMsgBody.Elem(generalFlags = ImMsgBody.GeneralFlags(pbReserve = "78 00 F8 01 00 C8 02 00".hexToBytes())))
|
||||||
|
|
||||||
return elements
|
return elements
|
||||||
}
|
}
|
||||||
@ -343,6 +343,10 @@ internal class OnlineFriendImageImpl(
|
|||||||
internal fun MsgComm.Msg.toMessageChain(): MessageChain {
|
internal fun MsgComm.Msg.toMessageChain(): MessageChain {
|
||||||
val elements = this.msgBody.richText.elems
|
val elements = this.msgBody.richText.elems
|
||||||
|
|
||||||
|
if (this.msgHead.fromUin == 1040400290L){
|
||||||
|
println(this._miraiContentToString())
|
||||||
|
}
|
||||||
|
|
||||||
return buildMessageChain(elements.size + 1) {
|
return buildMessageChain(elements.size + 1) {
|
||||||
+MessageSourceFromMsg(delegate = this@toMessageChain)
|
+MessageSourceFromMsg(delegate = this@toMessageChain)
|
||||||
elements.joinToMessageChain(this)
|
elements.joinToMessageChain(this)
|
||||||
@ -405,27 +409,19 @@ internal fun List<ImMsgBody.Elem>.joinToMessageChain(message: MessageChainBuilde
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
it.richMsg != null -> {
|
it.richMsg != null -> {
|
||||||
|
println(this._miraiContentToString())
|
||||||
|
val content = MiraiPlatformUtils.unzip(it.richMsg.template1, 1).encodeToString()
|
||||||
when (it.richMsg.serviceId) {
|
when (it.richMsg.serviceId) {
|
||||||
60 -> message.add(
|
1 -> message.add(JsonMessage(content))
|
||||||
XMLMessage(
|
60 -> message.add(XmlMessage(content))
|
||||||
content = MiraiPlatformUtils.unzip(it.richMsg.template1, 1).encodeToString()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
else -> {
|
else -> {
|
||||||
@Suppress("DEPRECATION")
|
@Suppress("DEPRECATION")
|
||||||
MiraiLogger.debug {
|
MiraiLogger.debug {
|
||||||
"unknown richMsg.serviceId: ${it.richMsg.serviceId}, content=${it.richMsg.template1.contentToString()}, \ntryUnzip=${
|
"unknown richMsg.serviceId: ${it.richMsg.serviceId}, content=${it.richMsg.template1.contentToString()}, \ntryUnzip=${content}"
|
||||||
kotlin.runCatching {
|
|
||||||
MiraiPlatformUtils.unzip(it.richMsg.template1, 1).encodeToString()
|
|
||||||
}.getOrElse { "<failed>" }
|
|
||||||
}"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> {
|
|
||||||
println(it._miraiContentToString())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020 Mamoe Technologies and contributors.
|
||||||
|
*
|
||||||
|
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||||
|
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||||
|
*
|
||||||
|
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.mamoe.mirai.message.data
|
||||||
|
|
||||||
|
import net.mamoe.mirai.utils.MiraiExperimentalAPI
|
||||||
|
import net.mamoe.mirai.utils.SinceMirai
|
||||||
|
|
||||||
|
@SinceMirai("0.27.0")
|
||||||
|
@OptIn(MiraiExperimentalAPI::class)
|
||||||
|
class JsonMessage(override val content: String) : RichMessage {
|
||||||
|
companion object Key : Message.Key<JsonMessage>
|
||||||
|
|
||||||
|
override val serviceId: Int
|
||||||
|
get() = 1
|
||||||
|
|
||||||
|
override fun toString(): String = content
|
||||||
|
}
|
@ -61,7 +61,7 @@ interface MessageChain : Message, Iterable<SingleMessage> {
|
|||||||
fun <M : Message> getOrNull(key: Message.Key<M>): M? = firstOrNull(key)
|
fun <M : Message> getOrNull(key: Message.Key<M>): M? = firstOrNull(key)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 遍历每一个有内容的消息, 即 [At], [AtAll], [PlainText], [Image], [Face], [XMLMessage], [QuoteReply].
|
* 遍历每一个有内容的消息, 即 [At], [AtAll], [PlainText], [Image], [Face], [XmlMessage], [QuoteReply].
|
||||||
* 仅供 `Java` 使用
|
* 仅供 `Java` 使用
|
||||||
*/
|
*/
|
||||||
@Suppress("FunctionName", "INAPPLICABLE_JVM_NAME")
|
@Suppress("FunctionName", "INAPPLICABLE_JVM_NAME")
|
||||||
@ -73,7 +73,7 @@ interface MessageChain : Message, Iterable<SingleMessage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 遍历每一个消息, 即 [MessageSource] [At], [AtAll], [PlainText], [Image], [Face], [XMLMessage], [QuoteReply].
|
* 遍历每一个消息, 即 [MessageSource] [At], [AtAll], [PlainText], [Image], [Face], [XmlMessage], [QuoteReply].
|
||||||
* 仅供 `Java` 使用
|
* 仅供 `Java` 使用
|
||||||
*/
|
*/
|
||||||
@Suppress("FunctionName", "INAPPLICABLE_JVM_NAME")
|
@Suppress("FunctionName", "INAPPLICABLE_JVM_NAME")
|
||||||
@ -88,7 +88,7 @@ interface MessageChain : Message, Iterable<SingleMessage> {
|
|||||||
// region accessors
|
// region accessors
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 遍历每一个有内容的消息, 即 [At], [AtAll], [PlainText], [Image], [Face], [XMLMessage], [QuoteReply]
|
* 遍历每一个有内容的消息, 即 [At], [AtAll], [PlainText], [Image], [Face], [XmlMessage], [QuoteReply]
|
||||||
*/
|
*/
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
inline fun MessageChain.foreachContent(block: (Message) -> Unit) {
|
inline fun MessageChain.foreachContent(block: (Message) -> Unit) {
|
||||||
@ -130,6 +130,9 @@ fun <M : Message> MessageChain.firstOrNull(key: Message.Key<M>): M? = when (key)
|
|||||||
Face -> first<Face>()
|
Face -> first<Face>()
|
||||||
QuoteReply -> first<QuoteReply>()
|
QuoteReply -> first<QuoteReply>()
|
||||||
MessageSource -> first<MessageSource>()
|
MessageSource -> first<MessageSource>()
|
||||||
|
XmlMessage -> first<XmlMessage>()
|
||||||
|
JsonMessage -> first<JsonMessage>()
|
||||||
|
RichMessage -> first<RichMessage>()
|
||||||
else -> null
|
else -> null
|
||||||
} as M?
|
} as M?
|
||||||
|
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020 Mamoe Technologies and contributors.
|
||||||
|
*
|
||||||
|
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||||
|
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||||
|
*
|
||||||
|
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.mamoe.mirai.message.data
|
||||||
|
|
||||||
|
import net.mamoe.mirai.utils.MiraiExperimentalAPI
|
||||||
|
import net.mamoe.mirai.utils.SinceMirai
|
||||||
|
|
||||||
|
/**
|
||||||
|
* XML 消息等富文本消息
|
||||||
|
*
|
||||||
|
* @see XmlMessage
|
||||||
|
*/
|
||||||
|
@SinceMirai("0.27.0")
|
||||||
|
interface RichMessage : MessageContent {
|
||||||
|
companion object Key : Message.Key<RichMessage>
|
||||||
|
|
||||||
|
@MiraiExperimentalAPI
|
||||||
|
val serviceId: Int
|
||||||
|
|
||||||
|
@MiraiExperimentalAPI
|
||||||
|
val content: String
|
||||||
|
}
|
@ -15,6 +15,7 @@
|
|||||||
package net.mamoe.mirai.message.data
|
package net.mamoe.mirai.message.data
|
||||||
|
|
||||||
import net.mamoe.mirai.utils.MiraiExperimentalAPI
|
import net.mamoe.mirai.utils.MiraiExperimentalAPI
|
||||||
|
import net.mamoe.mirai.utils.SinceMirai
|
||||||
import kotlin.jvm.JvmMultifileClass
|
import kotlin.jvm.JvmMultifileClass
|
||||||
import kotlin.jvm.JvmName
|
import kotlin.jvm.JvmName
|
||||||
|
|
||||||
@ -23,20 +24,25 @@ import kotlin.jvm.JvmName
|
|||||||
*
|
*
|
||||||
* @see buildXMLMessage
|
* @see buildXMLMessage
|
||||||
*/
|
*/
|
||||||
@MiraiExperimentalAPI
|
@SinceMirai("0.27.0")
|
||||||
inline class XMLMessage(val stringValue: String) : Message, MessageContent {
|
@OptIn(MiraiExperimentalAPI::class)
|
||||||
override fun followedBy(tail: Message): Nothing = error("XMLMessage Message cannot be followed")
|
class XmlMessage constructor(override val content: String) : RichMessage {
|
||||||
override fun toString(): String = stringValue
|
companion object Key : Message.Key<XmlMessage>
|
||||||
|
|
||||||
|
override val serviceId: Int get() = 60
|
||||||
|
|
||||||
|
override fun toString(): String = content
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造一条 XML 消息
|
* 构造一条 XML 消息
|
||||||
*/
|
*/
|
||||||
@MiraiExperimentalAPI("还未支持")
|
@SinceMirai("0.27.0")
|
||||||
inline fun buildXMLMessage(block: @XMLDsl XMLMessageBuilder.() -> Unit): XMLMessage =
|
@MiraiExperimentalAPI
|
||||||
XMLMessage(XMLMessageBuilder().apply(block).text)
|
inline fun buildXMLMessage(block: @XMLDsl XMLMessageBuilder.() -> Unit): XmlMessage =
|
||||||
|
XmlMessage(XMLMessageBuilder().apply(block).text)
|
||||||
|
|
||||||
@Suppress("NOTHING_TO_INLINE")
|
@SinceMirai("0.27.0")
|
||||||
@XMLDsl
|
@XMLDsl
|
||||||
class ItemBuilder(
|
class ItemBuilder(
|
||||||
var bg: Int = 0,
|
var bg: Int = 0,
|
||||||
@ -46,21 +52,20 @@ class ItemBuilder(
|
|||||||
internal val builder: StringBuilder = StringBuilder()
|
internal val builder: StringBuilder = StringBuilder()
|
||||||
val text: String get() = "<item bg='$bg' layout='$layout'>$builder</item>"
|
val text: String get() = "<item bg='$bg' layout='$layout'>$builder</item>"
|
||||||
|
|
||||||
inline fun summary(text: String, color: String = "#FFFFFF") {
|
fun summary(text: String, color: String = "#000000") {
|
||||||
this.builder.append("<summary color='$color'>$text</summary>")
|
this.builder.append("<summary color='$color'>$text</summary>")
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun title(text: String, size: Int = 18, color: String = "#FFFFFF") {
|
fun title(text: String, size: Int = 25, color: String = "#000000") {
|
||||||
this.builder.append("<title size='$size' color='$color'>$text</title>")
|
this.builder.append("<title size='$size' color='$color'>$text</title>")
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun picture(coverUrl: String) {
|
fun picture(coverUrl: String) {
|
||||||
this.builder.append("<picture cover='$coverUrl'/>")
|
this.builder.append("<picture cover='$coverUrl'/>")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@XMLDsl
|
@XMLDsl
|
||||||
@Suppress("NOTHING_TO_INLINE")
|
|
||||||
class XMLMessageBuilder(
|
class XMLMessageBuilder(
|
||||||
var templateId: Int = 1,
|
var templateId: Int = 1,
|
||||||
var serviceId: Int = 1,
|
var serviceId: Int = 1,
|
||||||
@ -70,7 +75,7 @@ class XMLMessageBuilder(
|
|||||||
*/
|
*/
|
||||||
var actionData: String = "",
|
var actionData: String = "",
|
||||||
/**
|
/**
|
||||||
* 摘要
|
* 摘要, 在官方客户端内消息列表中显示
|
||||||
*/
|
*/
|
||||||
var brief: String = "",
|
var brief: String = "",
|
||||||
var flag: Int = 3,
|
var flag: Int = 3,
|
||||||
@ -89,11 +94,11 @@ class XMLMessageBuilder(
|
|||||||
"</msg>"
|
"</msg>"
|
||||||
|
|
||||||
@XMLDsl
|
@XMLDsl
|
||||||
inline fun item(block: @XMLDsl ItemBuilder.() -> Unit) {
|
fun item(block: @XMLDsl ItemBuilder.() -> Unit) {
|
||||||
builder.append(ItemBuilder().apply(block).text)
|
builder.append(ItemBuilder().apply(block).text)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun source(name: String, iconURL: String = "") {
|
fun source(name: String, iconURL: String = "") {
|
||||||
sourceName = name
|
sourceName = name
|
||||||
sourceIconURL = iconURL
|
sourceIconURL = iconURL
|
||||||
}
|
}
|
||||||
@ -101,4 +106,4 @@ class XMLMessageBuilder(
|
|||||||
|
|
||||||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.TYPE)
|
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.TYPE)
|
||||||
@DslMarker
|
@DslMarker
|
||||||
internal annotation class XMLDsl
|
annotation class XMLDsl
|
Loading…
Reference in New Issue
Block a user