feat: add wiki command
This commit is contained in:
5 files changed
+72
-1
No files matched your search
@@ -20,6 +20,7 @@ import cn.rtast.fancybot.commands.lookup.NslookupCommand
|
||||
import cn.rtast.fancybot.commands.lookup.PixivCommand
|
||||
import cn.rtast.fancybot.commands.lookup.QRCodeCommand
|
||||
import cn.rtast.fancybot.commands.lookup.WeatherCommand
|
||||
import cn.rtast.fancybot.commands.lookup.WikipediaCommand
|
||||
import cn.rtast.fancybot.commands.parse.BVParseCommand
|
||||
import cn.rtast.fancybot.commands.parse.GitHubParseCommand
|
||||
import cn.rtast.fancybot.commands.parse.ImageURLCommand
|
||||
@@ -167,7 +168,7 @@ val commands = listOf(
|
||||
GithubUserCommand(), AboutCommand(),
|
||||
MusicPlayUrlCommand(), DomainPriceCommand(),
|
||||
SendMailCommand(), ZiBiCommand(),
|
||||
UnsetZiBiCommand()
|
||||
UnsetZiBiCommand(), WikipediaCommand()
|
||||
)
|
||||
|
||||
fun initFilesDir() {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/9/19
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.commands.lookup
|
||||
|
||||
import cn.rtast.fancybot.entity.wiki.WikipediaResponse
|
||||
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 org.jsoup.Jsoup
|
||||
|
||||
class WikipediaCommand : BaseCommand() {
|
||||
override val commandNames = listOf("/wiki", "/wk")
|
||||
|
||||
private val wikipediaAPI = "https://proxy.rtast.cn/https/zh.wikipedia.org/w/api.php"
|
||||
|
||||
private fun String.extractPlainTextFromHtml(): String {
|
||||
val doc = Jsoup.parse(this)
|
||||
return doc.text()
|
||||
}
|
||||
|
||||
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
|
||||
val title = args.first()
|
||||
val response = Http.get<WikipediaResponse>(
|
||||
wikipediaAPI,
|
||||
mapOf(
|
||||
"action" to "query",
|
||||
"list" to "search",
|
||||
"srsearch" to title,
|
||||
"format" to "json"
|
||||
)
|
||||
).query.search.first()
|
||||
val msg = MessageChain.Builder()
|
||||
.addText("标题: ${response.title} 内容片段如下")
|
||||
.addNewLine()
|
||||
.addText(response.snippet.extractPlainTextFromHtml())
|
||||
.build()
|
||||
message.reply(msg)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright © 2024 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2024/9/19
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.fancybot.entity.wiki
|
||||
|
||||
data class WikipediaResponse(
|
||||
val query: Query
|
||||
) {
|
||||
data class Query(
|
||||
val search: List<Search>
|
||||
)
|
||||
|
||||
data class Search(
|
||||
val title: String,
|
||||
val snippet: String,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user