feat: add image bed api
This commit is contained in:
10 files changed
+91
-31
No files matched your search
@@ -41,6 +41,7 @@ val gson: Gson = GsonBuilder()
|
|||||||
|
|
||||||
const val ROOT_PATH = "./data"
|
const val ROOT_PATH = "./data"
|
||||||
const val ASSETS_BASE_URL = "https://static.rtast.cn"
|
const val ASSETS_BASE_URL = "https://static.rtast.cn"
|
||||||
|
const val API_RTAST_URL = "https://api.rtast.cn"
|
||||||
|
|
||||||
val DEFAULT_CONFIG = Config(
|
val DEFAULT_CONFIG = Config(
|
||||||
ncmAPI = "https://ncm.rtast.cn",
|
ncmAPI = "https://ncm.rtast.cn",
|
||||||
@@ -67,7 +68,9 @@ val DEFAULT_CONFIG = Config(
|
|||||||
qqMusicApiUrl = "http://127.0.0.1:3200",
|
qqMusicApiUrl = "http://127.0.0.1:3200",
|
||||||
startUpNoticeUser = 114514L,
|
startUpNoticeUser = 114514L,
|
||||||
selfId = 114514L,
|
selfId = 114514L,
|
||||||
tianXingApiKey = "1145141919810"
|
tianXingApiKey = "1145141919810",
|
||||||
|
githubUser = "RTAkland",
|
||||||
|
githubImageRepo = "Static-Images"
|
||||||
)
|
)
|
||||||
|
|
||||||
val configManager = ConfigManager()
|
val configManager = ConfigManager()
|
||||||
|
|||||||
@@ -7,10 +7,13 @@
|
|||||||
|
|
||||||
package cn.rtast.fancybot.commands.misc
|
package cn.rtast.fancybot.commands.misc
|
||||||
|
|
||||||
|
import cn.rtast.fancybot.API_RTAST_URL
|
||||||
import cn.rtast.fancybot.annotations.CommandDescription
|
import cn.rtast.fancybot.annotations.CommandDescription
|
||||||
import cn.rtast.fancybot.entity.ShortLink
|
import cn.rtast.fancybot.entity.shortlink.ShortLinkPayload
|
||||||
|
import cn.rtast.fancybot.entity.shortlink.ShortLinkResponse
|
||||||
import cn.rtast.fancybot.util.Http
|
import cn.rtast.fancybot.util.Http
|
||||||
import cn.rtast.fancybot.util.str.encodeToBase64
|
import cn.rtast.fancybot.util.str.encodeToBase64
|
||||||
|
import cn.rtast.fancybot.util.str.toJson
|
||||||
import cn.rtast.rob.entity.GroupMessage
|
import cn.rtast.rob.entity.GroupMessage
|
||||||
import cn.rtast.rob.util.BaseCommand
|
import cn.rtast.rob.util.BaseCommand
|
||||||
import cn.rtast.rob.util.ob.OneBotListener
|
import cn.rtast.rob.util.ob.OneBotListener
|
||||||
@@ -19,7 +22,16 @@ import cn.rtast.rob.util.ob.OneBotListener
|
|||||||
class ShortLinkCommand : BaseCommand() {
|
class ShortLinkCommand : BaseCommand() {
|
||||||
override val commandNames = listOf("/s")
|
override val commandNames = listOf("/s")
|
||||||
|
|
||||||
private val shortLinkApi = "https://api.rtast.cn"
|
|
||||||
|
companion object {
|
||||||
|
fun String.makeShortLink(): String {
|
||||||
|
val shortLink = Http.post<ShortLinkResponse>(
|
||||||
|
"$API_RTAST_URL/short_link",
|
||||||
|
ShortLinkPayload(this).toJson()
|
||||||
|
)
|
||||||
|
return "$API_RTAST_URL/s?rnd_id=${shortLink.id}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
||||||
if (args.isEmpty()) {
|
if (args.isEmpty()) {
|
||||||
@@ -27,10 +39,7 @@ class ShortLinkCommand : BaseCommand() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
val target = args.first().encodeToBase64()
|
val target = args.first().encodeToBase64()
|
||||||
val shortLink = Http.post<ShortLink>(
|
val shortLink = target.makeShortLink()
|
||||||
"$shortLinkApi/short_link",
|
message.reply(shortLink)
|
||||||
params = mapOf("target" to target)
|
|
||||||
)
|
|
||||||
message.reply("$shortLinkApi/s?rnd_id=${shortLink.id}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,15 +113,25 @@ class AsciiArtCommand : BaseCommand() {
|
|||||||
val asciiFrames = mutableListOf<BufferedImage>()
|
val asciiFrames = mutableListOf<BufferedImage>()
|
||||||
val width = frames.first().width
|
val width = frames.first().width
|
||||||
val height = frames.first().height
|
val height = frames.first().height
|
||||||
frames.forEach { asciiFrames.add(it.convertToAscii().saveAsciiArtToImage(width, height)) }
|
frames.forEachIndexed { index, item ->
|
||||||
|
asciiFrames.add(item.convertToAscii().saveAsciiArtToImage(width, height))
|
||||||
|
logger.info("帧:${index}处理完成")
|
||||||
|
}
|
||||||
|
logger.info("合成GIF中...")
|
||||||
val gifBytes = decoder.makeGif(asciiFrames)
|
val gifBytes = decoder.makeGif(asciiFrames)
|
||||||
|
logger.info("合并完成")
|
||||||
val gifBase64 = gifBytes.encodeToBase64()
|
val gifBase64 = gifBytes.encodeToBase64()
|
||||||
logger.info("处理后的图片大小: ${gifBytes.size}字节, base64大小: ${gifBase64.length * 2}字节")
|
logger.info("处理后的图片大小: ${gifBytes.size}字节")
|
||||||
message.reply(MessageChain.Builder().addImage(gifBase64, true).build())
|
val msg = MessageChain.Builder()
|
||||||
|
.addImage(gifBase64, true)
|
||||||
|
message.reply(msg.build())
|
||||||
}
|
}
|
||||||
} catch (_: Exception) {
|
} catch (_: OutOfMemoryError) {
|
||||||
message.reply("处理GIF失败")
|
message.reply("GIF处理失败: 内存溢出")
|
||||||
logger.info("gif处理失败")
|
logger.info("GIF处理失败: 内存溢出")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
message.reply("处理GIF失败: ${e.message}")
|
||||||
|
logger.info("gif处理失败: ${e.message}")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
message.reply("回复错误已取消本次操作")
|
message.reply("回复错误已取消本次操作")
|
||||||
|
|||||||
@@ -36,4 +36,6 @@ data class Config(
|
|||||||
val startUpNoticeUser: Long,
|
val startUpNoticeUser: Long,
|
||||||
val selfId: Long,
|
val selfId: Long,
|
||||||
val tianXingApiKey: String,
|
val tianXingApiKey: String,
|
||||||
|
val githubUser: String,
|
||||||
|
val githubImageRepo: String,
|
||||||
)
|
)
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright © 2024 RTAkland
|
|
||||||
* Author: RTAkland
|
|
||||||
* Date: 2024/9/22
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
package cn.rtast.fancybot.entity
|
|
||||||
|
|
||||||
data class ShortLink(
|
|
||||||
val id: String
|
|
||||||
)
|
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
package cn.rtast.fancybot.items
|
package cn.rtast.fancybot.items
|
||||||
|
|
||||||
|
import cn.rtast.fancybot.API_RTAST_URL
|
||||||
import cn.rtast.fancybot.entity.setu.Setu
|
import cn.rtast.fancybot.entity.setu.Setu
|
||||||
import cn.rtast.fancybot.niuziManager
|
import cn.rtast.fancybot.niuziManager
|
||||||
import cn.rtast.fancybot.util.Http
|
import cn.rtast.fancybot.util.Http
|
||||||
@@ -27,7 +28,7 @@ class SetuItem : Item() {
|
|||||||
message: GroupMessage,
|
message: GroupMessage,
|
||||||
after: Double
|
after: Double
|
||||||
): MessageChain.Builder {
|
): MessageChain.Builder {
|
||||||
val response = Http.get("https://api.rtast.cn/api/setu")
|
val response = Http.get("$API_RTAST_URL/api/setu")
|
||||||
.fromArrayJson<List<Setu>>().first()
|
.fromArrayJson<List<Setu>>().first()
|
||||||
if (response.r18) {
|
if (response.r18) {
|
||||||
niuziManager.updateLength(message.sender.userId, itemPrice)
|
niuziManager.updateLength(message.sender.userId, itemPrice)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
package cn.rtast.fancybot.items
|
package cn.rtast.fancybot.items
|
||||||
|
|
||||||
|
import cn.rtast.fancybot.API_RTAST_URL
|
||||||
import cn.rtast.fancybot.ROOT_PATH
|
import cn.rtast.fancybot.ROOT_PATH
|
||||||
import cn.rtast.fancybot.configManager
|
import cn.rtast.fancybot.configManager
|
||||||
import cn.rtast.fancybot.entity.setu.Setu
|
import cn.rtast.fancybot.entity.setu.Setu
|
||||||
@@ -48,7 +49,7 @@ private fun getImages(r18: Boolean): NodeMessageChain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val response = Http.get("https://api.rtast.cn/api/setu?size=10").fromArrayJson<List<Setu>>()
|
val response = Http.get("$API_RTAST_URL/api/setu?size=10").fromArrayJson<List<Setu>>()
|
||||||
response.filter { !it.r18 }.map { it.urls.large }.forEach {
|
response.filter { !it.r18 }.map { it.urls.large }.forEach {
|
||||||
try {
|
try {
|
||||||
val imageBase64 = URI(it).toURL().readBytes().encodeToBase64()
|
val imageBase64 = URI(it).toURL().readBytes().encodeToBase64()
|
||||||
|
|||||||
@@ -10,14 +10,15 @@ package cn.rtast.fancybot.util
|
|||||||
import cn.rtast.fancybot.util.str.fromJson
|
import cn.rtast.fancybot.util.str.fromJson
|
||||||
import okhttp3.FormBody
|
import okhttp3.FormBody
|
||||||
import okhttp3.MediaType.Companion.toMediaType
|
import okhttp3.MediaType.Companion.toMediaType
|
||||||
|
import okhttp3.MultipartBody
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.RequestBody.Companion.toRequestBody
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
|
|
||||||
object Http {
|
object Http {
|
||||||
|
|
||||||
private val jsonMediaType = "application/json; charset=utf-8".toMediaType()
|
val jsonMediaType = "application/json; charset=utf-8".toMediaType()
|
||||||
private val jsonHeader = mapOf(
|
val jsonHeader = mapOf(
|
||||||
"Content-Type" to "application/json; charset=utf-8",
|
"Content-Type" to "application/json; charset=utf-8",
|
||||||
"Accept" to "application/json"
|
"Accept" to "application/json"
|
||||||
)
|
)
|
||||||
@@ -136,4 +137,45 @@ object Http {
|
|||||||
val headerRequest = addHeaders(request, headers)
|
val headerRequest = addHeaders(request, headers)
|
||||||
return this.executeRequest(headerRequest.build()).fromJson<T>()
|
return this.executeRequest(headerRequest.build()).fromJson<T>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JvmOverloads
|
||||||
|
inline fun <reified T> post(
|
||||||
|
url: String,
|
||||||
|
form: MultipartBody,
|
||||||
|
headers: Map<String, String>? = null
|
||||||
|
): T {
|
||||||
|
val request = Request.Builder()
|
||||||
|
.post(form)
|
||||||
|
.url(buildParams(url, headers))
|
||||||
|
val headerRequest = addHeaders(request, headers)
|
||||||
|
return this.executeRequest(headerRequest.build()).fromJson<T>()
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <reified T> put(
|
||||||
|
url: String,
|
||||||
|
jsonBody: String,
|
||||||
|
headers: Map<String, String>? = null,
|
||||||
|
params: Map<String, Any>? = null
|
||||||
|
): T {
|
||||||
|
val paramsUrl = buildParams(url, params)
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(paramsUrl)
|
||||||
|
.put(jsonBody.toRequestBody(jsonMediaType))
|
||||||
|
val headerRequest = addHeaders(request, headers)
|
||||||
|
return this.executeRequest(headerRequest.build()).fromJson<T>()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun put(
|
||||||
|
url: String,
|
||||||
|
jsonBody: String,
|
||||||
|
headers: Map<String, String>? = null,
|
||||||
|
params: Map<String, Any>? = null
|
||||||
|
): String {
|
||||||
|
val paramsUrl = buildParams(url, params)
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(paramsUrl)
|
||||||
|
.put(jsonBody.toRequestBody(jsonMediaType))
|
||||||
|
val headerRequest = addHeaders(request, headers)
|
||||||
|
return this.executeRequest(headerRequest.build())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -37,4 +37,6 @@ class ConfigManager : JsonFileHandler<Config>("config.json") {
|
|||||||
val startUpNoticeUser = config.startUpNoticeUser
|
val startUpNoticeUser = config.startUpNoticeUser
|
||||||
val selfId = config.selfId
|
val selfId = config.selfId
|
||||||
val tianXingApiKey = config.tianXingApiKey
|
val tianXingApiKey = config.tianXingApiKey
|
||||||
|
val githubUser = config.githubUser
|
||||||
|
val githubImageRepo = config.githubImageRepo
|
||||||
}
|
}
|
||||||
@@ -28,5 +28,7 @@
|
|||||||
"qqMusicApiUrl": "http://127.0.0.1:3200",
|
"qqMusicApiUrl": "http://127.0.0.1:3200",
|
||||||
"startUpNoticeUser": 114514,
|
"startUpNoticeUser": 114514,
|
||||||
"selfId": 114514,
|
"selfId": 114514,
|
||||||
"tianXingApiKey": "1145141919810"
|
"tianXingApiKey": "1145141919810",
|
||||||
|
"githubUser": "RTAkland",
|
||||||
|
"githubImageRepo": "Static-Images"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user