feat: add tts command

This commit is contained in:
2024-10-07 16:07:07 +08:00
parent 35fd368cb6
commit 69743ca42b
4 files changed
+73 -2

No files matched your search

+2 -1
View File
@@ -118,5 +118,6 @@ val commands = listOf(
ShotSelfCommand(), TenSetuCommand(), ShotSelfCommand(), TenSetuCommand(),
ReactionCommand(), MCSkinCommand(), ReactionCommand(), MCSkinCommand(),
GithubLatestCommitCommand(), RandomMusicCommand(), GithubLatestCommitCommand(), RandomMusicCommand(),
ZDJDCommand(), ScanQRCodeCommand() ZDJDCommand(), ScanQRCodeCommand(),
TTSCommand()
) )
@@ -0,0 +1,42 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/10/7
*/
package cn.rtast.fancybot.commands.misc
import cn.rtast.fancybot.annotations.CommandDescription
import cn.rtast.fancybot.entity.tts.TTSResponse
import cn.rtast.fancybot.util.Http
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.ob.MessageChain
import cn.rtast.rob.util.ob.OneBotListener
import okhttp3.FormBody
@CommandDescription("将文字转换成语音(TTS)!")
class TTSCommand : BaseCommand() {
override val commandNames = listOf("/tts")
private val ttsApiUrl = "https://ttsmp3.com/makemp3_new.php"
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
if (args.isEmpty()) {
message.reply("发送`/tts <文字>`就可以生成语音啦~")
return
}
val content = args.joinToString(" ").trim()
val form = FormBody.Builder()
.add("msg", content)
.add("lang", "Zhiyu")
.add("source", "ttsmp3")
.build()
val response = Http.post<TTSResponse>(ttsApiUrl, form)
val msg = MessageChain.Builder()
.addRecord(response.url.replace("https://", "https://proxy.rtast.cn/https/"))
.build()
listener.sendGroupMessage(message.groupId, msg)
}
}
@@ -0,0 +1,15 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/10/7
*/
package cn.rtast.fancybot.entity.tts
import com.google.gson.annotations.SerializedName
data class TTSResponse(
@SerializedName("URL")
val url: String
)
+14 -1
View File
@@ -48,7 +48,7 @@ object Http {
return result.fromJson<T>() return result.fromJson<T>()
} }
private fun executeRequest(request: Request): String { fun executeRequest(request: Request): String {
return okHttpClient.newCall(request).execute().body.string() return okHttpClient.newCall(request).execute().body.string()
} }
@@ -123,4 +123,17 @@ object Http {
val headerRequest = addHeaders(request, headers) val headerRequest = addHeaders(request, headers)
return this.executeRequest(headerRequest.build()) return this.executeRequest(headerRequest.build())
} }
@JvmOverloads
inline fun <reified T> post(
url: String,
form: FormBody,
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>()
}
} }