Skip to content

Commit

Permalink
feat: 支持使用base64向群和c2c(不可用)上传媒体(图片)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Oct 31, 2024
1 parent 4885463 commit 757103d
Show file tree
Hide file tree
Showing 19 changed files with 717 additions and 30 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ apiValidation {
"love.forte.simbot.annotations.InternalSimbotAPI",
"love.forte.simbot.qguild.QGInternalApi",
"love.forte.simbot.component.qguild.ExperimentalQGApi",
"love.forte.simbot.qguild.ExperimentalQGMediaApi"
),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public final class love/forte/simbot/qguild/ErrInfo$Companion {
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public abstract interface annotation class love/forte/simbot/qguild/ExperimentalQGMediaApi : java/lang/annotation/Annotation {
}

public abstract interface annotation class love/forte/simbot/qguild/Generated : java/lang/annotation/Annotation {
}

Expand Down Expand Up @@ -1032,10 +1035,11 @@ public final class love/forte/simbot/qguild/api/files/UploadGroupFilesApi : love
public static final field FILE_TYPE_IMAGE I
public static final field FILE_TYPE_VIDEO I
public static final field Factory Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi$Factory;
public synthetic fun <init> (Ljava/lang/String;Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi$Body;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Ljava/lang/String;Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi$BodyValue;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public static final fun create (Ljava/lang/String;ILjava/lang/String;)Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi;
public static final fun create (Ljava/lang/String;ILjava/lang/String;Z)Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi;
public static final fun create (Ljava/lang/String;Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi$Body;)Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi;
public static final fun create (Ljava/lang/String;Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi$BodyValue;)Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi;
public fun getResultDeserializationStrategy ()Lkotlinx/serialization/DeserializationStrategy;
}

Expand Down Expand Up @@ -1070,10 +1074,38 @@ public final class love/forte/simbot/qguild/api/files/UploadGroupFilesApi$Body$C
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class love/forte/simbot/qguild/api/files/UploadGroupFilesApi$BodyValue {
public static final field Companion Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi$BodyValue$Companion;
public fun <init> ()V
public final fun getFileType ()Ljava/lang/Integer;
public final fun getSrvSendMsg ()Ljava/lang/Boolean;
public final fun getUrl ()Ljava/lang/String;
public final fun setFileType (Ljava/lang/Integer;)V
public final fun setSrvSendMsg (Ljava/lang/Boolean;)V
public final fun setUrl (Ljava/lang/String;)V
public fun toString ()Ljava/lang/String;
}

public synthetic class love/forte/simbot/qguild/api/files/UploadGroupFilesApi$BodyValue$$serializer : kotlinx/serialization/internal/GeneratedSerializer {
public static final field INSTANCE Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi$BodyValue$$serializer;
public final fun childSerializers ()[Lkotlinx/serialization/KSerializer;
public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object;
public final fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi$BodyValue;
public final fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor;
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
public final fun serialize (Lkotlinx/serialization/encoding/Encoder;Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi$BodyValue;)V
public fun typeParametersSerializers ()[Lkotlinx/serialization/KSerializer;
}

public final class love/forte/simbot/qguild/api/files/UploadGroupFilesApi$BodyValue$Companion {
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class love/forte/simbot/qguild/api/files/UploadGroupFilesApi$Factory : love/forte/simbot/qguild/api/SimplePostApiDescription {
public final fun create (Ljava/lang/String;ILjava/lang/String;)Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi;
public final fun create (Ljava/lang/String;ILjava/lang/String;Z)Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi;
public final fun create (Ljava/lang/String;Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi$Body;)Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi;
public final fun create (Ljava/lang/String;Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi$BodyValue;)Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi;
public static synthetic fun create$default (Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi$Factory;Ljava/lang/String;ILjava/lang/String;ZILjava/lang/Object;)Llove/forte/simbot/qguild/api/files/UploadGroupFilesApi;
}

Expand All @@ -1083,10 +1115,11 @@ public final class love/forte/simbot/qguild/api/files/UploadUserFilesApi : love/
public static final field FILE_TYPE_IMAGE I
public static final field FILE_TYPE_VIDEO I
public static final field Factory Llove/forte/simbot/qguild/api/files/UploadUserFilesApi$Factory;
public synthetic fun <init> (Ljava/lang/String;Llove/forte/simbot/qguild/api/files/UploadUserFilesApi$Body;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Ljava/lang/String;Llove/forte/simbot/qguild/api/files/UploadUserFilesApi$BodyValue;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public static final fun create (Ljava/lang/String;ILjava/lang/String;)Llove/forte/simbot/qguild/api/files/UploadUserFilesApi;
public static final fun create (Ljava/lang/String;ILjava/lang/String;Z)Llove/forte/simbot/qguild/api/files/UploadUserFilesApi;
public static final fun create (Ljava/lang/String;Llove/forte/simbot/qguild/api/files/UploadUserFilesApi$Body;)Llove/forte/simbot/qguild/api/files/UploadUserFilesApi;
public static final fun create (Ljava/lang/String;Llove/forte/simbot/qguild/api/files/UploadUserFilesApi$BodyValue;)Llove/forte/simbot/qguild/api/files/UploadUserFilesApi;
public fun getResultDeserializationStrategy ()Lkotlinx/serialization/DeserializationStrategy;
}

Expand Down Expand Up @@ -1121,10 +1154,38 @@ public final class love/forte/simbot/qguild/api/files/UploadUserFilesApi$Body$Co
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class love/forte/simbot/qguild/api/files/UploadUserFilesApi$BodyValue {
public static final field Companion Llove/forte/simbot/qguild/api/files/UploadUserFilesApi$BodyValue$Companion;
public fun <init> ()V
public final fun getFileType ()Ljava/lang/Integer;
public final fun getSrvSendMsg ()Ljava/lang/Boolean;
public final fun getUrl ()Ljava/lang/String;
public final fun setFileType (Ljava/lang/Integer;)V
public final fun setSrvSendMsg (Ljava/lang/Boolean;)V
public final fun setUrl (Ljava/lang/String;)V
public fun toString ()Ljava/lang/String;
}

public synthetic class love/forte/simbot/qguild/api/files/UploadUserFilesApi$BodyValue$$serializer : kotlinx/serialization/internal/GeneratedSerializer {
public static final field INSTANCE Llove/forte/simbot/qguild/api/files/UploadUserFilesApi$BodyValue$$serializer;
public final fun childSerializers ()[Lkotlinx/serialization/KSerializer;
public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object;
public final fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Llove/forte/simbot/qguild/api/files/UploadUserFilesApi$BodyValue;
public final fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor;
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
public final fun serialize (Lkotlinx/serialization/encoding/Encoder;Llove/forte/simbot/qguild/api/files/UploadUserFilesApi$BodyValue;)V
public fun typeParametersSerializers ()[Lkotlinx/serialization/KSerializer;
}

public final class love/forte/simbot/qguild/api/files/UploadUserFilesApi$BodyValue$Companion {
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class love/forte/simbot/qguild/api/files/UploadUserFilesApi$Factory : love/forte/simbot/qguild/api/SimplePostApiDescription {
public final fun create (Ljava/lang/String;ILjava/lang/String;)Llove/forte/simbot/qguild/api/files/UploadUserFilesApi;
public final fun create (Ljava/lang/String;ILjava/lang/String;Z)Llove/forte/simbot/qguild/api/files/UploadUserFilesApi;
public final fun create (Ljava/lang/String;Llove/forte/simbot/qguild/api/files/UploadUserFilesApi$Body;)Llove/forte/simbot/qguild/api/files/UploadUserFilesApi;
public final fun create (Ljava/lang/String;Llove/forte/simbot/qguild/api/files/UploadUserFilesApi$BodyValue;)Llove/forte/simbot/qguild/api/files/UploadUserFilesApi;
public static synthetic fun create$default (Llove/forte/simbot/qguild/api/files/UploadUserFilesApi$Factory;Ljava/lang/String;ILjava/lang/String;ZILjava/lang/Object;)Llove/forte/simbot/qguild/api/files/UploadUserFilesApi;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
package love.forte.simbot.qguild



/**
* 一个仅服务于Java的API。对于Kotlin来讲通常有更优选择。
*/
@Retention(AnnotationRetention.BINARY)
@MustBeDocumented
@RequiresOptIn("API marked for Java use, not recommended for Kotlin.", level = RequiresOptIn.Level.WARNING)
public annotation class QGApi4J

/**
* 一个仅服务于JS的API。对于Kotlin来讲通常有更优选择。
*/
Expand All @@ -50,3 +50,16 @@ public annotation class QGInternalApi
@Retention(AnnotationRetention.SOURCE)
@MustBeDocumented
public annotation class Generated

/**
* 一个**实验性**的与媒体资源相关的API,可能在未来发生变更或被移除。
*
* @since 4.1.1
*/
@Retention(AnnotationRetention.BINARY)
@MustBeDocumented
@RequiresOptIn(
"一个实验性的与媒体资源相关的API,可能在未来发生变更或被移除。",
level = RequiresOptIn.Level.WARNING
)
public annotation class ExperimentalQGMediaApi
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ package love.forte.simbot.qguild.api.files
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import love.forte.simbot.qguild.ExperimentalQGMediaApi
import love.forte.simbot.qguild.api.PostQQGuildApi
import love.forte.simbot.qguild.api.SimplePostApiDescription
import love.forte.simbot.qguild.model.MessageMedia
import kotlin.io.encoding.Base64
import kotlin.io.encoding.ExperimentalEncodingApi
import kotlin.jvm.JvmOverloads
import kotlin.jvm.JvmStatic

Expand All @@ -34,7 +38,7 @@ import kotlin.jvm.JvmStatic
*/
public class UploadGroupFilesApi private constructor(
openid: String,
private val _body: Body
private val _body: BodyValue
) : PostQQGuildApi<MessageMedia>() {
public companion object Factory : SimplePostApiDescription(
"/v2/groups/{group_openid}/files"
Expand All @@ -50,9 +54,28 @@ public class UploadGroupFilesApi private constructor(
* @param openid 群聊的 openid
*/
@JvmStatic
@Suppress("DEPRECATION")
@Deprecated("Use create(openid: String, body: BodyValue)")
public fun create(openid: String, body: Body): UploadGroupFilesApi =
UploadGroupFilesApi(
openid,
BodyValue().also {
it.fileType = body.fileType
it.url = body.url
it.srvSendMsg = body.srvSendMsg
})


/**
* Create [UploadGroupFilesApi].
*
* @param openid 群聊的 openid
*/
@JvmStatic
public fun create(openid: String, body: BodyValue): UploadGroupFilesApi =
UploadGroupFilesApi(openid, body)


/**
* Create [UploadGroupFilesApi].
*
Expand All @@ -73,21 +96,59 @@ public class UploadGroupFilesApi private constructor(
): UploadGroupFilesApi =
create(
openid,
Body(
fileType = fileType,
url = url,
srvSendMsg = srvSendMsg
)
BodyValue().also {
it.fileType = fileType
it.url = url
it.srvSendMsg = srvSendMsg
}
)

/**
* Create [UploadGroupFilesApi].
*
* @param openid 群聊的 openid
* @param fileType 媒体类型:1 图片,2 视频,3 语音,4 文件(暂不开放)
* 资源格式要求:
* 图片:png/jpg,视频:mp4,语音:silk
* @param fileData 上传文件的数据
* @param srvSendMsg 设置 true 会直接发送消息到目标端,且会占用主动消息频次
*
* @since 4.1.1
*/
@ExperimentalQGMediaApi
@JvmStatic
@JvmOverloads
public fun create(
openid: String,
fileType: Int,
fileData: ByteArray,
srvSendMsg: Boolean = false
): UploadGroupFilesApi =
create(
openid,
BodyValue().also {
it.fileType = fileType
it.fileDataBytes = fileData
it.srvSendMsg = srvSendMsg
}
)
}

override val resultDeserializationStrategy: DeserializationStrategy<MessageMedia>
get() = MessageMedia.serializer()

override val path: Array<String> = arrayOf("v2", "groups", openid, "files")

override fun createBody(): Any = _body
@OptIn(ExperimentalQGMediaApi::class)
private val fileDataBytes = _body.fileDataBytes

@OptIn(ExperimentalEncodingApi::class)
override fun createBody(): Any {
if (fileDataBytes != null) {
_body.fileDataBase64Hex = Base64.encode(fileDataBytes)
}
return _body
}

/**
* @property fileType 媒体类型:1 图片,2 视频,3 语音,4 文件(暂不开放)
Expand All @@ -97,11 +158,62 @@ public class UploadGroupFilesApi private constructor(
* @property srvSendMsg 设置 true 会直接发送消息到目标端,且会占用主动消息频次
*/
@Serializable
@Deprecated("Use `BodyValue`")
public data class Body(
@SerialName("file_type")
val fileType: Int,
val url: String,
@SerialName("srv_send_msg")
val srvSendMsg: Boolean,
)


/**
* The body for [UploadGroupFilesApi].
*
* @since 4.1.1
*/
@Serializable
public class BodyValue {
/**
* 媒体类型:1 图片,2 视频,3 语音,4 文件(暂不开放)
* 资源格式要求:
* 图片:png/jpg,视频:mp4,语音:silk
*
* Required.
*/
@SerialName("file_type")
public var fileType: Int? = null

/**
* 需要发送媒体资源的url
*/
public var url: String? = null

/**
* 设置 `true` 会直接发送消息到目标端,且会占用主动消息频次
*/
@SerialName("srv_send_msg")
public var srvSendMsg: Boolean? = null

/**
* 上传文件的数据,如果不为 `null` 则使用 `file_data` 而不是 [url]。
*/
@Transient
@ExperimentalQGMediaApi
public var fileDataBytes: ByteArray? = null

@SerialName("file_data")
internal var fileDataBase64Hex: String? = null

@OptIn(ExperimentalQGMediaApi::class)
override fun toString(): String = buildString {
append("BodyValue(fileType=").append(fileType)
append(", url=").append(url)
append(", srvSendMsg=").append(srvSendMsg)
append(", fileDataBytes=")
fileDataBytes?.joinTo(this, prefix = "[", postfix = "]", limit = 6)
append(')')
}
}
}
Loading

0 comments on commit 757103d

Please sign in to comment.