Compare commits

..

No commits in common. "main" and "v0.4.0" have entirely different histories.
main ... v0.4.0

43 changed files with 425 additions and 1941 deletions

View File

@ -1,2 +0,0 @@
**/
!scalabot-app/build/install/

View File

@ -1,33 +0,0 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "gradle"
directory: "/"
schedule:
interval: "daily"
# - package-ecosystem: "gradle"
# directory: "/scalabot-app"
# schedule:
# interval: "weekly"
# - package-ecosystem: "gradle"
# directory: "/scalabot-meta"
# schedule:
# interval: "weekly"
# - package-ecosystem: "gradle"
# directory: "/scalabot-extension"
# schedule:
# interval: "weekly"
# - package-ecosystem: "gradle"
# directory: "/scalabot-ext-example"
# schedule:
# interval: "weekly"

View File

@ -1,33 +0,0 @@
name: Binary compatibility verification (for API)
on:
push:
paths:
- 'scalabot-meta/**'
- 'scalabot-extension/**'
pull_request:
paths:
- 'scalabot-meta/**'
- 'scalabot-extension/**'
permissions:
contents: read
jobs:
apiCompatibilityCheck:
timeout-minutes: 8
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'adopt-hotspot'
- name: Set up Gradle
uses: gradle/actions/setup-gradle@v4
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build and run binary compatibility verification
run: ./gradlew apiCheck

View File

@ -18,15 +18,17 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v3
- name: Set up JDK 17 - name: Set up JDK 11
uses: actions/setup-java@v4 uses: actions/setup-java@v3
with: with:
java-version: '17' java-version: '11'
distribution: 'adopt-hotspot' distribution: 'adopt-hotspot'
- name: Set up Gradle cache: 'gradle'
uses: gradle/actions/setup-gradle@v4
- name: Grant execute permission for gradlew - name: Grant execute permission for gradlew
run: chmod +x gradlew run: chmod +x gradlew
- name: Build and test - name: Build and test
run: ./gradlew clean test uses: gradle/gradle-build-action@v2.2.1
with:
gradle-version: 'wrapper'
arguments: test

View File

@ -1,62 +0,0 @@
name: Create release draft
on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+*'
# 该 Action 有以下步骤:
# 1. 拉取并构建代码, 然后生成 Application 发行包;
# 2. 创建 Release, 并标记为 Draft(草稿);
# 3. 上传 Application 发行包;
permissions:
contents: write
jobs:
create-release:
timeout-minutes: 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
# 创建更新日志.
- name: 'Get Previous tag'
id: previous-tag
uses: younited/get-previous-tag-action@v1.1.0
with:
match: "v*.*.*"
- name: Set up Python 3
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install Commitizen
run: pip install -U commitizen
- name: Create Change log
run: cz ch --start-rev ${{ steps.previous-tag.outputs.previous-tag }} --file-name ${{ github.workspace }}/CURRENT_CHANGELOG.md
# 开始构建项目.
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'adopt-hotspot'
- name: Set up Gradle
uses: gradle/actions/setup-gradle@v4
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build and test
run: ./gradlew clean test assembleDist
# 创建新的发行版本
- name: Create Release
uses: softprops/action-gh-release@v2
with:
draft: true
body_path: ${{ github.workspace }}/CURRENT_CHANGELOG.md
files: |
*/build/distributions/*
*/build/libs/*

View File

@ -0,0 +1,10 @@
name: "Validate Gradle Wrapper"
on: [push, pull_request]
jobs:
validation:
name: "Validation"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: gradle/wrapper-validation-action@v1

View File

@ -1,46 +0,0 @@
name: Publish artifacts
on:
release:
types:
- published
env:
IMAGE_NAME: lamgc/scalabot
jobs:
publish-container-image:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'adopt-hotspot'
- name: Set up Gradle
uses: gradle/actions/setup-gradle@v4
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build and test
run: ./gradlew clean test installDist
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker BuildX
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push container image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ env.IMAGE_NAME }}:latest, ${{ env.IMAGE_NAME }}:${{ github.ref_name }}
platforms: linux/amd64,linux/arm64
cache-from: type=gha
cache-to: type=gha,mode=max

View File

@ -1,46 +0,0 @@
name: Build development version container image
on:
push:
branches:
- "main"
permissions:
contents: read
jobs:
build:
timeout-minutes: 15
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'adopt-hotspot'
- name: Set up Gradle
uses: gradle/actions/setup-gradle@v4
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build project and install Distribution package
run: ./gradlew clean test installDist
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker BuildX
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push container image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: lamgc/scalabot:dev
platforms: linux/amd64,linux/arm64
cache-from: type=gha
cache-to: type=gha,mode=max

View File

@ -1,8 +0,0 @@
FROM eclipse-temurin:21-jdk-alpine
ENV BOT_DATA_PATH /scalabot/data/
WORKDIR /scalabot/run/
CMD ["/scalabot/app/bin/scalabot-app"]
COPY scalabot-app/build/install/scalabot-app/ /scalabot/app/

View File

@ -1,7 +1,6 @@
plugins { plugins {
kotlin("jvm") version "2.1.0" apply false kotlin("jvm") version "1.6.10" apply false
id("org.jetbrains.kotlinx.kover") version "0.8.3" apply false id("org.jetbrains.kotlinx.kover") version "0.5.1" apply false
id("org.jetbrains.kotlinx.binary-compatibility-validator") version "0.16.3" apply false
} }
allprojects { allprojects {
@ -13,5 +12,5 @@ allprojects {
} }
group = "net.lamgc" group = "net.lamgc"
version = "0.8.0-1" version = "0.4.0"
} }

Binary file not shown.

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

10
gradlew vendored
View File

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
# #
# Copyright © 2015-2021 the original authors. # Copyright ? 2015-2021 the original authors.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -32,10 +32,10 @@
# Busybox and similar reduced shells will NOT work, because this script # Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features: # requires all of these POSIX shell features:
# * functions; # * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», # * expansions ?$var?, ?${var}?, ?${var:-default}?, ?${var+SET}?,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»; # ?${var#prefix}?, ?${var%suffix}?, and ?$( cmd )?;
# * compound commands having a testable exit status, especially «case»; # * compound commands having a testable exit status, especially ?case?;
# * various built-in commands including «command», «set», and «ulimit». # * various built-in commands including ?command?, ?set?, and ?ulimit?.
# #
# Important for patching: # Important for patching:
# #

View File

@ -1,4 +1,3 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins { plugins {
@ -11,9 +10,9 @@ dependencies {
implementation(project(":scalabot-meta")) implementation(project(":scalabot-meta"))
implementation(project(":scalabot-extension")) implementation(project(":scalabot-extension"))
implementation("org.slf4j:slf4j-api:2.0.11") implementation("org.slf4j:slf4j-api:1.7.36")
implementation("io.github.microutils:kotlin-logging:3.0.5") implementation("io.github.microutils:kotlin-logging:2.1.23")
implementation("ch.qos.logback:logback-classic:1.5.12") implementation("ch.qos.logback:logback-classic:1.2.11")
val aetherVersion = "1.1.0" val aetherVersion = "1.1.0"
implementation("org.eclipse.aether:aether-api:$aetherVersion") implementation("org.eclipse.aether:aether-api:$aetherVersion")
@ -23,35 +22,31 @@ dependencies {
implementation("org.eclipse.aether:aether-transport-http:$aetherVersion") implementation("org.eclipse.aether:aether-transport-http:$aetherVersion")
implementation("org.eclipse.aether:aether-connector-basic:$aetherVersion") implementation("org.eclipse.aether:aether-connector-basic:$aetherVersion")
implementation("org.apache.maven:maven-aether-provider:3.3.9") implementation("org.apache.maven:maven-aether-provider:3.3.9")
implementation("org.codehaus.plexus:plexus-utils:3.5.1")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.2")
implementation("org.jetbrains.kotlin:kotlin-reflect:2.1.0") implementation("org.jetbrains.kotlin:kotlin-reflect:1.6.20")
implementation("com.google.code.gson:gson:2.11.0") implementation("com.google.code.gson:gson:2.9.0")
implementation("org.jdom:jdom2:2.0.6.1") implementation("org.jdom:jdom2:2.0.6.1")
implementation("org.telegram:telegrambots-abilities:8.0.0") implementation("org.telegram:telegrambots-abilities:6.0.1")
implementation("org.telegram:telegrambots-longpolling:8.0.0") implementation("org.telegram:telegrambots:6.0.1")
implementation("org.telegram:telegrambots-client:8.0.0")
implementation("io.prometheus:simpleclient:0.16.0") implementation("io.prometheus:simpleclient:0.15.0")
implementation("io.prometheus:simpleclient_httpserver:0.16.0") implementation("io.prometheus:simpleclient_httpserver:0.15.0")
testImplementation(kotlin("test")) testImplementation(kotlin("test"))
testImplementation("io.mockk:mockk:1.13.13") testImplementation("io.mockk:mockk:1.12.4")
testImplementation("com.github.stefanbirkner:system-lambda:1.2.1") testImplementation("com.github.stefanbirkner:system-lambda:1.2.1")
} }
tasks.test { tasks.test {
useJUnitPlatform() useJUnitPlatform()
jvmArgs("--add-opens", "java.base/java.util=ALL-UNNAMED", "--add-opens", "java.base/java.lang=ALL-UNNAMED")
} }
tasks.withType<KotlinCompile> { tasks.withType<KotlinCompile> {
compilerOptions { kotlinOptions.jvmTarget = "11"
jvmTarget = JvmTarget.JVM_17
}
} }
application { application {
@ -61,8 +56,3 @@ application {
tasks.jar.configure { tasks.jar.configure {
exclude("**/logback-test.xml") exclude("**/logback-test.xml")
} }
tasks.withType<AbstractArchiveTask>().configureEach {
isPreserveFileTimestamps = false
isReproducibleFileOrder = true
}

View File

@ -3,7 +3,7 @@ package net.lamgc.scalabot
import ch.qos.logback.core.PropertyDefinerBase import ch.qos.logback.core.PropertyDefinerBase
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import com.google.gson.JsonArray import com.google.gson.reflect.TypeToken
import mu.KotlinLogging import mu.KotlinLogging
import net.lamgc.scalabot.config.* import net.lamgc.scalabot.config.*
import net.lamgc.scalabot.config.serializer.* import net.lamgc.scalabot.config.serializer.*
@ -12,24 +12,23 @@ import org.eclipse.aether.repository.Authentication
import org.eclipse.aether.repository.Proxy import org.eclipse.aether.repository.Proxy
import org.eclipse.aether.repository.RemoteRepository import org.eclipse.aether.repository.RemoteRepository
import org.eclipse.aether.repository.RepositoryPolicy import org.eclipse.aether.repository.RepositoryPolicy
import org.slf4j.event.Level import org.telegram.telegrambots.bots.DefaultBotOptions
import java.io.File import java.io.File
import java.net.URL import java.net.URL
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
import java.util.function.Supplier import kotlin.system.exitProcess
import kotlin.reflect.KProperty
private val log = KotlinLogging.logger { } private val log = KotlinLogging.logger { }
internal fun ProxyType.toJavaProxyType(): java.net.Proxy.Type? { internal fun ProxyType.toTelegramBotsType(): DefaultBotOptions.ProxyType {
return when (this) { return when (this) {
ProxyType.NO_PROXY -> null ProxyType.NO_PROXY -> DefaultBotOptions.ProxyType.NO_PROXY
ProxyType.HTTP -> java.net.Proxy.Type.HTTP ProxyType.HTTP -> DefaultBotOptions.ProxyType.HTTP
ProxyType.HTTPS -> java.net.Proxy.Type.HTTP ProxyType.HTTPS -> DefaultBotOptions.ProxyType.HTTP
ProxyType.SOCKS4 -> java.net.Proxy.Type.SOCKS ProxyType.SOCKS4 -> DefaultBotOptions.ProxyType.SOCKS4
ProxyType.SOCKS5 -> java.net.Proxy.Type.SOCKS ProxyType.SOCKS5 -> DefaultBotOptions.ProxyType.SOCKS5
} }
} }
@ -42,32 +41,13 @@ internal fun ProxyConfig.toAetherProxy(): Proxy? {
return Proxy(typeStr, host, port) return Proxy(typeStr, host, port)
} }
internal fun MavenRepositoryConfig.toRemoteRepository(proxyConfig: ProxyConfig? = null): RemoteRepository { internal fun MavenRepositoryConfig.toRemoteRepository(proxyConfig: ProxyConfig): RemoteRepository {
val repositoryId = if (id == null) { val builder =
val generatedRepoId = createDefaultRepositoryId() RemoteRepository.Builder(id ?: createDefaultRepositoryId(), checkRepositoryLayout(layout), url.toString())
log.debug { "仓库 Url `$url` 未设置仓库 Id, 已分配缺省 Id: $generatedRepoId" }
generatedRepoId
} else if ("local".contentEquals(id, ignoreCase = true)) {
val generatedRepoId = createDefaultRepositoryId()
log.debug { "仓库 Url `$url` 不允许使用 `local` 作为仓库 Id, 已分配缺省 Id: $generatedRepoId" }
generatedRepoId
} else {
id
}
val builder = RemoteRepository.Builder(repositoryId, checkRepositoryLayout(layout), url.toString())
if (proxy != null) { if (proxy != null) {
val selfProxy = proxy!! builder.setProxy(proxy)
builder.setProxy(selfProxy) } else if (proxyConfig.type == ProxyType.HTTP) {
log.debug { "仓库 $repositoryId 已使用独立的代理配置: ${selfProxy.type}://${selfProxy.host}:${selfProxy.port}" }
} else if (proxyConfig != null) {
if (proxyConfig.type in (ProxyType.HTTP..ProxyType.HTTPS)) {
builder.setProxy(proxyConfig.toAetherProxy()) builder.setProxy(proxyConfig.toAetherProxy())
log.debug { "仓库 $repositoryId 已使用 全局/Bot 代理配置: $proxyConfig" }
} else {
log.debug { "仓库 $repositoryId 不支持 全局/Bot 的代理配置: `$proxyConfig` (仅支持 HTTP 和 HTTPS)" }
}
} else {
log.debug { "仓库 $repositoryId 不使用代理." }
} }
builder.setReleasePolicy( builder.setReleasePolicy(
@ -108,9 +88,9 @@ private fun createDefaultRepositoryId(): String {
* 必须提供 `pathSupplier` `fileSupplier` 其中一个, 才能正常提供路径. * 必须提供 `pathSupplier` `fileSupplier` 其中一个, 才能正常提供路径.
*/ */
internal enum class AppPaths( internal enum class AppPaths(
private val pathSupplier: PathSupplier, private val pathSupplier: () -> String = { fileSupplier.invoke().canonicalPath },
private val initializer: AppPaths.() -> Unit = AppPaths::defaultInitializer, private val initializer: AppPaths.() -> Unit = AppPaths::defaultInitializer,
private val fileSupplier: FileSupplier, private val fileSupplier: () -> File = { File(pathSupplier()) }
) { ) {
/** /**
* 数据根目录. * 数据根目录.
@ -119,7 +99,7 @@ internal enum class AppPaths(
* *
* 提示: 结尾不带 `/`. * 提示: 结尾不带 `/`.
*/ */
DATA_ROOT(fileSupplier = FileSupplier { DATA_ROOT(fileSupplier = {
File( File(
System.getProperty(PathConst.PROP_DATA_PATH) ?: System.getenv(PathConst.ENV_DATA_PATH) System.getProperty(PathConst.PROP_DATA_PATH) ?: System.getenv(PathConst.ENV_DATA_PATH)
?: System.getProperty("user.dir") ?: "." ?: System.getProperty("user.dir") ?: "."
@ -131,10 +111,10 @@ internal enum class AppPaths(
} }
}), }),
CONFIG_APPLICATION(PathSupplier { "$DATA_ROOT/config.json" }, { DEFAULT_CONFIG_APPLICATION({ "$DATA_ROOT/config.json" }, {
if (!file.exists()) { if (!file.exists()) {
file.bufferedWriter(StandardCharsets.UTF_8).use { file.bufferedWriter(StandardCharsets.UTF_8).use {
GsonConst.appConfigGson.toJson( GsonConst.botConfigGson.toJson(
AppConfig( AppConfig(
mavenRepositories = listOf( mavenRepositories = listOf(
MavenRepositoryConfig( MavenRepositoryConfig(
@ -147,7 +127,7 @@ internal enum class AppPaths(
} }
} }
}), }),
CONFIG_BOT(PathSupplier { "$DATA_ROOT/bot.json" }, { DEFAULT_CONFIG_BOT({ "$DATA_ROOT/bot.json" }, {
if (!file.exists()) { if (!file.exists()) {
file.bufferedWriter(StandardCharsets.UTF_8).use { file.bufferedWriter(StandardCharsets.UTF_8).use {
GsonConst.botConfigGson.toJson( GsonConst.botConfigGson.toJson(
@ -173,25 +153,10 @@ internal enum class AppPaths(
TEMP({ "$DATA_ROOT/tmp/" }) TEMP({ "$DATA_ROOT/tmp/" })
; ;
constructor(pathSupplier: PathSupplier, initializer: AppPaths.() -> Unit = AppPaths::defaultInitializer) : this( val file: File
fileSupplier = FileSupplier { File(pathSupplier.path).canonicalFile }, get() = fileSupplier.invoke()
pathSupplier = pathSupplier, val path: String
initializer = initializer get() = pathSupplier.invoke()
)
constructor(fileSupplier: FileSupplier, initializer: AppPaths.() -> Unit = AppPaths::defaultInitializer) : this(
fileSupplier = fileSupplier,
pathSupplier = PathSupplier { fileSupplier.file.canonicalPath },
initializer = initializer
)
constructor(pathSupplier: () -> String) : this(
fileSupplier = FileSupplier { File(pathSupplier.invoke()).canonicalFile },
pathSupplier = PathSupplier { pathSupplier.invoke() }
)
val file: File by fileSupplier
val path: String by pathSupplier
private val initialized = AtomicBoolean(false) private val initialized = AtomicBoolean(false)
@ -203,44 +168,15 @@ internal enum class AppPaths(
} }
} }
/**
* 一个内部方法, 用于将 [initialized] 状态重置.
*
* 如果不重置该状态, 将使得单元测试无法让 AppPath 重新初始化文件.
*
* 警告: 该方法不应该被非测试代码调用.
*/
@Suppress("unused")
private fun reset() {
log.warn {
"初始化状态已重置: `${this.name}`, 如果在非测试环境中重置状态, 请报告该问题."
}
initialized.set(false)
}
override fun toString(): String { override fun toString(): String {
return path return path
} }
object PathConst { private object PathConst {
const val PROP_DATA_PATH = "bot.path.data" const val PROP_DATA_PATH = "bot.path.data"
const val ENV_DATA_PATH = "BOT_DATA_PATH" const val ENV_DATA_PATH = "BOT_DATA_PATH"
} }
private class FileSupplier(private val supplier: Supplier<File>) {
operator fun getValue(appPaths: AppPaths, property: KProperty<*>): File = supplier.get()
val file: File
get() = supplier.get()
}
private class PathSupplier(private val supplier: Supplier<String>) {
operator fun getValue(appPaths: AppPaths, property: KProperty<*>): String = supplier.get()
val path: String
get() = supplier.get()
}
} }
/** /**
@ -252,37 +188,8 @@ internal class LogDirectorySupplier : PropertyDefinerBase() {
} }
} }
internal class LogLevelSupplier : PropertyDefinerBase() {
override fun getPropertyValue(): String {
val property = System.getProperty("scalabot.log.level", System.getenv("BOT_LOG_LEVEL"))
val level = if (property != null) {
try {
Level.valueOf(property.uppercase())
} catch (e: IllegalArgumentException) {
addWarn("Invalid log level: `$property`, the log will be output using the Info log level.")
Level.INFO
}
} else {
Level.INFO
}
return level.name
}
}
internal class NetworkVerboseLogSupplier : PropertyDefinerBase() {
override fun getPropertyValue(): String {
val propertyValue = System.getProperty("scalabot.log.network.verbose", "false")
return if (propertyValue.toBoolean()) {
"DEBUG"
} else {
"INFO"
}
}
}
internal object Const { internal object Const {
val config = loadAppConfig() val config = loadAppConfig()
const val METRICS_NAMESPACE = "scalabot"
} }
private fun AppPaths.defaultInitializer() { private fun AppPaths.defaultInitializer() {
@ -300,28 +207,22 @@ private fun AppPaths.defaultInitializer() {
} }
} }
/** internal fun initialFiles() {
* 执行 AppPaths 所有项目的初始化, 并检查是否停止运行, 让用户编辑配置. val configFilesNotInitialized = !AppPaths.DEFAULT_CONFIG_APPLICATION.file.exists()
* && !AppPaths.DEFAULT_CONFIG_BOT.file.exists()
* @return 如果需要让用户编辑配置, 则返回 `true`.
*/
internal fun initialFiles(): Boolean {
val configFilesNotInitialized = !AppPaths.CONFIG_APPLICATION.file.exists()
&& !AppPaths.CONFIG_BOT.file.exists()
for (path in AppPaths.entries) { for (path in AppPaths.values()) {
path.initial() path.initial()
} }
if (configFilesNotInitialized) { if (configFilesNotInitialized) {
log.warn { "配置文件已初始化, 请根据需要修改配置文件后重新启动本程序." } log.warn { "配置文件已初始化, 请根据需要修改配置文件后重新启动本程序." }
return true exitProcess(1)
} }
return false
} }
internal object GsonConst { private object GsonConst {
private val baseGson: Gson = GsonBuilder() val baseGson: Gson = GsonBuilder()
.setPrettyPrinting() .setPrettyPrinting()
.serializeNulls() .serializeNulls()
.create() .create()
@ -337,11 +238,10 @@ internal object GsonConst {
.registerTypeAdapter(ProxyType::class.java, ProxyTypeSerializer) .registerTypeAdapter(ProxyType::class.java, ProxyTypeSerializer)
.registerTypeAdapter(BotConfig::class.java, BotConfigSerializer) .registerTypeAdapter(BotConfig::class.java, BotConfigSerializer)
.registerTypeAdapter(Artifact::class.java, ArtifactSerializer) .registerTypeAdapter(Artifact::class.java, ArtifactSerializer)
.registerTypeAdapter(BotAccount::class.java, BotAccountSerializer)
.create() .create()
} }
internal fun loadAppConfig(configFile: File = AppPaths.CONFIG_APPLICATION.file): AppConfig { internal fun loadAppConfig(configFile: File = AppPaths.DEFAULT_CONFIG_APPLICATION.file): AppConfig {
try { try {
configFile.bufferedReader(StandardCharsets.UTF_8).use { configFile.bufferedReader(StandardCharsets.UTF_8).use {
return GsonConst.appConfigGson.fromJson(it, AppConfig::class.java)!! return GsonConst.appConfigGson.fromJson(it, AppConfig::class.java)!!
@ -352,10 +252,10 @@ internal fun loadAppConfig(configFile: File = AppPaths.CONFIG_APPLICATION.file):
} }
} }
internal fun loadBotConfigJson(botConfigFile: File = AppPaths.CONFIG_BOT.file): JsonArray? { internal fun loadBotConfig(botConfigFile: File = AppPaths.DEFAULT_CONFIG_BOT.file): Set<BotConfig>? {
try { try {
botConfigFile.bufferedReader(StandardCharsets.UTF_8).use { botConfigFile.bufferedReader(StandardCharsets.UTF_8).use {
return GsonConst.botConfigGson.fromJson(it, JsonArray::class.java)!! return GsonConst.botConfigGson.fromJson(it, object : TypeToken<Set<BotConfig>>() {}.type)!!
} }
} catch (e: Exception) { } catch (e: Exception) {
log.error(e) { "读取 Bot 配置文件 (bot.json) 时发生错误, 请检查配置格式是否正确." } log.error(e) { "读取 Bot 配置文件 (bot.json) 时发生错误, 请检查配置格式是否正确." }

View File

@ -1,22 +1,20 @@
package net.lamgc.scalabot package net.lamgc.scalabot
import com.google.gson.JsonParseException
import io.prometheus.client.exporter.HTTPServer import io.prometheus.client.exporter.HTTPServer
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import mu.KotlinLogging import mu.KotlinLogging
import net.lamgc.scalabot.config.* import net.lamgc.scalabot.config.AppConfig
import net.lamgc.scalabot.config.BotConfig
import net.lamgc.scalabot.config.MetricsConfig
import net.lamgc.scalabot.config.ProxyType
import net.lamgc.scalabot.util.registerShutdownHook import net.lamgc.scalabot.util.registerShutdownHook
import okhttp3.OkHttpClient
import org.eclipse.aether.repository.LocalRepository import org.eclipse.aether.repository.LocalRepository
import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient import org.telegram.telegrambots.bots.DefaultBotOptions
import org.telegram.telegrambots.longpolling.BotSession import org.telegram.telegrambots.meta.TelegramBotsApi
import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication import org.telegram.telegrambots.meta.generics.BotSession
import org.telegram.telegrambots.meta.api.methods.GetMe import org.telegram.telegrambots.updatesreceivers.DefaultBotSession
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.net.InetSocketAddress
import java.net.Proxy
import java.nio.file.attribute.PosixFilePermission import java.nio.file.attribute.PosixFilePermission
import java.nio.file.attribute.PosixFilePermissions import java.nio.file.attribute.PosixFilePermissions
import kotlin.io.path.createDirectories import kotlin.io.path.createDirectories
@ -30,15 +28,12 @@ private val log = KotlinLogging.logger { }
fun main(args: Array<String>): Unit = runBlocking { fun main(args: Array<String>): Unit = runBlocking {
log.info { "ScalaBot 正在启动中..." } log.info { "ScalaBot 正在启动中..." }
log.info { "数据目录: ${AppPaths.DATA_ROOT}" } log.info { "数据目录: ${AppPaths.DATA_ROOT}" }
log.debug { "Kotlin: ${KotlinVersion.CURRENT}, JVM: ${Runtime.version()}" }
log.debug { "启动参数: ${args.joinToString(prefix = "[", postfix = "]")}" } log.debug { "启动参数: ${args.joinToString(prefix = "[", postfix = "]")}" }
if (initialFiles()) { initialFiles()
exitProcess(1)
}
val launcher = Launcher() val launcher = Launcher()
.registerShutdownHook() .registerShutdownHook()
startMetricsServer()?.registerShutdownHook() startMetricsServer()
if (!launcher.launch()) { if (!launcher.launch()) {
exitProcess(1) exitProcess(1)
} }
@ -62,28 +57,28 @@ internal fun startMetricsServer(config: MetricsConfig = Const.config.metrics): H
val httpServer = builder val httpServer = builder
.build() .build()
.registerShutdownHook()
log.info { "运行指标服务器已启动. (Port: ${httpServer.port})" } log.info { "运行指标服务器已启动. (Port: ${httpServer.port})" }
return httpServer return httpServer
} }
internal class Launcher( internal class Launcher(private val config: AppConfig = Const.config) : AutoCloseable {
private val config: AppConfig = Const.config,
private val configFile: File = AppPaths.CONFIG_APPLICATION.file,
) : AutoCloseable {
companion object { companion object {
@JvmStatic @JvmStatic
private val log = KotlinLogging.logger { } private val log = KotlinLogging.logger { }
} }
private val botApi = TelegramBotsLongPollingApplication() private val botApi = TelegramBotsApi(DefaultBotSession::class.java)
private val botSessionMap = mutableMapOf<ScalaBot, BotSession>() private val botSessionMap = mutableMapOf<ScalaBot, BotSession>()
private val mavenLocalRepository = getMavenLocalRepository() private val mavenLocalRepository = getMavenLocalRepository()
private fun getMavenLocalRepository(): LocalRepository { private fun getMavenLocalRepository(): LocalRepository {
val localPath = val localPath =
if (config.mavenLocalRepository != null && config.mavenLocalRepository!!.isNotEmpty()) { if (config.mavenLocalRepository != null && config.mavenLocalRepository!!.isNotEmpty()) {
val repoPath = configFile.toPath().resolve(config.mavenLocalRepository!!).apply { val repoPath = AppPaths.DATA_ROOT.file.toPath()
.resolve(config.mavenLocalRepository!!)
.apply {
if (!exists()) { if (!exists()) {
if (!parent.isWritable() || !parent.isReadable()) { if (!parent.isWritable() || !parent.isReadable()) {
throw IOException("Unable to read and write the directory where Maven repository is located.") throw IOException("Unable to read and write the directory where Maven repository is located.")
@ -116,47 +111,22 @@ internal class Launcher(
@Synchronized @Synchronized
fun launch(): Boolean { fun launch(): Boolean {
val botConfigs = loadBotConfigJson() ?: return false val botConfigs = loadBotConfig() ?: return false
if (botConfigs.isEmpty) { if (botConfigs.isEmpty()) {
log.warn { "尚未配置任何机器人, 请先配置机器人后再启动本程序." } log.warn { "尚未配置任何机器人, 请先配置机器人后再启动本程序." }
return false return false
} else if (botConfigs.none { it.enabled }) {
log.warn { "配置文件中没有已启用的机器人, 请至少启用一个机器人." }
return false
} }
var launchedCounts = 0 for (botConfig in botConfigs) {
for (botConfigJson in botConfigs) {
val botConfig = try {
GsonConst.botConfigGson.fromJson(botConfigJson, BotConfig::class.java)
} catch (e: JsonParseException) {
val botName = try {
botConfigJson.asJsonObject.get("account")?.asJsonObject?.get("name")?.asString ?: "Unknown"
} catch (e: Exception) {
"Unknown"
}
log.error(e) { "机器人 `$botName` 配置有误, 跳过该机器人的启动." }
continue
}
try { try {
launchBot(botConfig) launchBot(botConfig)
launchedCounts++
} catch (e: Exception) { } catch (e: Exception) {
if (e is TelegramApiRequestException && e.errorCode == 401) {
log.error { "机器人 `${botConfig.account.name}` 的 Bot Token 无效, 请检查配置: [${e.errorCode}] ${e.apiResponse}" }
} else {
log.error(e) { "机器人 `${botConfig.account.name}` 启动时发生错误." } log.error(e) { "机器人 `${botConfig.account.name}` 启动时发生错误." }
} }
} }
} return true
botApi.start()
botApi.registerShutdownHook()
return if (launchedCounts != 0) {
log.info { "已启动 $launchedCounts 个机器人." }
true
} else {
log.warn { "未启动任何机器人, 请检查配置并至少启用一个机器人." }
false
}
} }
private fun launchBot(botConfig: BotConfig) { private fun launchBot(botConfig: BotConfig) {
@ -165,38 +135,34 @@ internal class Launcher(
return return
} }
log.info { "正在启动机器人 `${botConfig.account.name}`..." } log.info { "正在启动机器人 `${botConfig.account.name}`..." }
val botOption = DefaultBotOptions().apply {
val proxyConfig = val proxyConfig =
if (botConfig.proxy.type != ProxyType.NO_PROXY) { if (botConfig.proxy.type != ProxyType.NO_PROXY) {
log.debug { "[Bot ${botConfig.account.name}] 使用独立代理: ${botConfig.proxy.type}" }
botConfig.proxy botConfig.proxy
} else if (config.proxy.type != ProxyType.NO_PROXY) { } else if (config.proxy.type != ProxyType.NO_PROXY) {
log.debug { "[Bot ${botConfig.account.name}] 使用全局代理: ${botConfig.proxy.type}" }
config.proxy config.proxy
} else { } else {
log.debug { "[Bot ${botConfig.account.name}] 不使用代理." } null
ProxyConfig(type = ProxyType.NO_PROXY) }
if (proxyConfig != null) {
proxyType = proxyConfig.type.toTelegramBotsType()
proxyHost = config.proxy.host
proxyPort = config.proxy.port
log.debug { "机器人 `${botConfig.account.name}` 已启用代理配置: $proxyConfig" }
} }
val okhttpClientBuilder = OkHttpClient.Builder() baseUrl = botConfig.baseApiUrl
if (proxyConfig.type != ProxyType.NO_PROXY) {
val proxyType = proxyConfig.type.toJavaProxyType()
val proxyAddress = InetSocketAddress.createUnresolved(proxyConfig.host, proxyConfig.port)
okhttpClientBuilder.proxy(Proxy(proxyType, proxyAddress))
} }
val account = botConfig.account val account = botConfig.account
val telegramClient =
OkHttpTelegramClient(okhttpClientBuilder.build(), account.token, botConfig.getBaseApiTelegramUrl())
val remoteRepositories = config.mavenRepositories val remoteRepositories = config.mavenRepositories
.map { it.toRemoteRepository(proxyConfig) } .map { it.toRemoteRepository(config.proxy) }
.toMutableList().apply { .toMutableList().apply {
if (this.none { if (this.none {
it.url == MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL it.url == MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL
|| it.url == MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL.trimEnd('/') || it.url == MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL.trimEnd('/')
}) { }) {
add(MavenRepositoryExtensionFinder.getMavenCentralRepository(proxy = proxyConfig.toAetherProxy())) add(MavenRepositoryExtensionFinder.getMavenCentralRepository(proxy = config.proxy.toAetherProxy()))
} }
}.toList() }.toList()
val extensionPackageFinders = setOf( val extensionPackageFinders = setOf(
@ -209,15 +175,11 @@ internal class Launcher(
val bot = ScalaBot( val bot = ScalaBot(
BotDBMaker.getBotDbInstance(account), BotDBMaker.getBotDbInstance(account),
telegramClient, botOption,
extensionPackageFinders, extensionPackageFinders,
botConfig botConfig
) )
botSessionMap[bot] = botApi.registerBot(bot)
val botUser = bot.telegramClient.execute(GetMe())
log.debug { "已验证 Bot Token 有效性, Bot Username: ${botUser.userName}" }
botSessionMap[bot] = botApi.registerBot(botConfig.account.token, bot)
log.info { "机器人 `${bot.botUsername}` 已启动." } log.info { "机器人 `${bot.botUsername}` 已启动." }
if (botConfig.autoUpdateCommandList) { if (botConfig.autoUpdateCommandList) {

View File

@ -7,8 +7,8 @@ import net.lamgc.scalabot.util.toHexString
import org.mapdb.DB import org.mapdb.DB
import org.mapdb.DBException import org.mapdb.DBException
import org.mapdb.DBMaker import org.mapdb.DBMaker
import org.telegram.telegrambots.abilitybots.api.db.DBContext import org.telegram.abilitybots.api.db.DBContext
import org.telegram.telegrambots.abilitybots.api.db.MapDBContext import org.telegram.abilitybots.api.db.MapDBContext
import java.io.File import java.io.File
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.security.MessageDigest import java.security.MessageDigest
@ -157,6 +157,7 @@ private abstract class FileDbAdapter(
val oldFile = oldDbAdapter.getBotDbFile(botAccount) val oldFile = oldDbAdapter.getBotDbFile(botAccount)
val newFile = getBotDbFile(botAccount) val newFile = getBotDbFile(botAccount)
try { try {
@Suppress("UnstableApiUsage")
Files.copy(oldFile, newFile) Files.copy(oldFile, newFile)
} catch (e: Exception) { } catch (e: Exception) {
if (newFile.exists()) { if (newFile.exists()) {

View File

@ -1,11 +1,10 @@
package net.lamgc.scalabot package net.lamgc.scalabot
import mu.KotlinLogging import mu.KotlinLogging
import net.lamgc.scalabot.extension.BotExtensionCreateOptions
import net.lamgc.scalabot.extension.BotExtensionFactory import net.lamgc.scalabot.extension.BotExtensionFactory
import net.lamgc.scalabot.util.getPriority import net.lamgc.scalabot.util.getPriority
import org.eclipse.aether.artifact.Artifact import org.eclipse.aether.artifact.Artifact
import org.telegram.telegrambots.abilitybots.api.util.AbilityExtension import org.telegram.abilitybots.api.util.AbilityExtension
import java.io.File import java.io.File
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.net.URL import java.net.URL
@ -15,18 +14,6 @@ import java.nio.charset.StandardCharsets
import java.util.* import java.util.*
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
/**
* 扩展加载器.
*
* 扩展加载器并非负责加载扩展的 Class, 而是委派搜索器发现并获取扩展, 然后加载扩展实例.
*
* 注意, 扩展加载器将内置两个 Finder: [FileNameFinder] [MavenMetaInformationFinder].
*
* @param bot 扩展加载器所负责的 ScalaBot 实例.
* @param extensionsDataFolder 提供给扩展用于数据存储的根目录(实际目录为 `{root}/{group...}/{artifact}`).
* @param extensionsPath 提供给 Finder 用于搜索扩展的本地扩展包存放路径.
* @param extensionFinders 加载器所使用的搜索器集合. 加载扩展时将使用所提供的的加载器.
*/
internal class ExtensionLoader( internal class ExtensionLoader(
private val bot: ScalaBot, private val bot: ScalaBot,
private val extensionsDataFolder: File = AppPaths.DATA_EXTENSIONS.file, private val extensionsDataFolder: File = AppPaths.DATA_EXTENSIONS.file,
@ -40,13 +27,6 @@ internal class ExtensionLoader(
MavenMetaInformationFinder MavenMetaInformationFinder
).apply { addAll(extensionFinders) }.toSet() ).apply { addAll(extensionFinders) }.toSet()
/**
* 加载扩展, 并返回扩展项.
*
* 调用本方法后, 将会指派提供的 Finder 搜索 ScalaBot 配置的扩展包.
*
* @return 返回存放了所有已加载扩展项的 Set. 可通过 [LoadedExtensionEntry] 获取扩展的有关信息.
*/
fun getExtensions(): Set<LoadedExtensionEntry> { fun getExtensions(): Set<LoadedExtensionEntry> {
val extensionEntries = mutableSetOf<LoadedExtensionEntry>() val extensionEntries = mutableSetOf<LoadedExtensionEntry>()
for (extensionArtifact in bot.extensions) { for (extensionArtifact in bot.extensions) {
@ -72,17 +52,6 @@ internal class ExtensionLoader(
/** /**
* 检查是否发生冲突. * 检查是否发生冲突.
*
* 扩展包冲突有两种情况:
* 1. 有多个同为最高优先级的搜索器搜索到了扩展包.
* 2. 唯一的最高优先级搜索器搜索到了多个扩展包.
*
* 扩展包冲突指的是**有多个具有相同构件坐标的扩展包被搜索到**,
* 如果不顾扩展包冲突直接加载的话, 将会出现安全隐患,
* 因此在加载器发现冲突的情况下将输出相关信息, 提示用户进行排查.
*
* @param foundResult 扩展包搜索结果.
*
* @return 如果出现冲突, 返回 `true`. * @return 如果出现冲突, 返回 `true`.
*/ */
private fun checkConflict(foundResult: Map<ExtensionPackageFinder, Set<FoundExtensionPackage>>): Boolean { private fun checkConflict(foundResult: Map<ExtensionPackageFinder, Set<FoundExtensionPackage>>): Boolean {
@ -99,9 +68,6 @@ internal class ExtensionLoader(
} }
} }
/**
* 从结果中过滤出由最高优先级的搜索器搜索到的扩展包.
*/
private fun filterHighPriorityResult(foundResult: Map<ExtensionPackageFinder, Set<FoundExtensionPackage>>) private fun filterHighPriorityResult(foundResult: Map<ExtensionPackageFinder, Set<FoundExtensionPackage>>)
: Map<ExtensionPackageFinder, Set<FoundExtensionPackage>> { : Map<ExtensionPackageFinder, Set<FoundExtensionPackage>> {
val finders: List<ExtensionPackageFinder> = foundResult.keys val finders: List<ExtensionPackageFinder> = foundResult.keys
@ -123,13 +89,7 @@ internal class ExtensionLoader(
for (factory in extClassLoader.serviceLoader) { for (factory in extClassLoader.serviceLoader) {
try { try {
val extension = val extension =
factory.createExtensionInstance( factory.createExtensionInstance(bot, getExtensionDataFolder(extensionArtifact))
bot, getExtensionDataFolder(extensionArtifact),
BotExtensionCreateOptions(
bot.accountId,
bot.botConfig.proxy.copy()
)
)
if (extension == null) { if (extension == null) {
log.debug { "Factory ${factory::class.java} 创建插件时返回了 null, 已跳过. (BotName: ${bot.botUsername})" } log.debug { "Factory ${factory::class.java} 创建插件时返回了 null, 已跳过. (BotName: ${bot.botUsername})" }
continue continue
@ -142,11 +102,6 @@ internal class ExtensionLoader(
return factories.toSet() return factories.toSet()
} }
/**
* 只是用来统计扩展包搜索结果的数量而已.
*
* @return 返回扩展包的数量.
*/
private fun allFoundedPackageNumber(filesMap: Map<ExtensionPackageFinder, Set<FoundExtensionPackage>>): Int { private fun allFoundedPackageNumber(filesMap: Map<ExtensionPackageFinder, Set<FoundExtensionPackage>>): Int {
var number = 0 var number = 0
for (files in filesMap.values) { for (files in filesMap.values) {
@ -155,14 +110,6 @@ internal class ExtensionLoader(
return number return number
} }
/**
* 搜索指定构件坐标的依赖包.
*
* 搜索扩展包将根据搜索器优先级从高到低依次搜索, 当某一个优先级的搜索器搜到扩展包后将停止搜索.
* 可以根据不同优先级的搜索器, 配置扩展包的主用和备用文件.
*
* @return 返回各个搜索器返回的搜索结果.
*/
private fun findExtensionPackage( private fun findExtensionPackage(
extensionArtifact: Artifact, extensionArtifact: Artifact,
): Map<ExtensionPackageFinder, Set<FoundExtensionPackage>> { ): Map<ExtensionPackageFinder, Set<FoundExtensionPackage>> {
@ -191,21 +138,13 @@ internal class ExtensionLoader(
return result return result
} }
/**
* 检查扩展包搜索器是否设置了 [FinderRules] 注解.
* @return 如果已设置注解, 则返回 `true`.
*/
private fun checkExtensionPackageFinder(finder: ExtensionPackageFinder): Boolean = private fun checkExtensionPackageFinder(finder: ExtensionPackageFinder): Boolean =
finder::class.java.getDeclaredAnnotation(FinderRules::class.java) != null finder::class.java.getDeclaredAnnotation(FinderRules::class.java) != null
/**
* 在日志中输出有关扩展包冲突的错误信息.
*/
private fun printExtensionFileConflictError( private fun printExtensionFileConflictError(
extensionArtifact: Artifact, extensionArtifact: Artifact,
foundResult: Map<ExtensionPackageFinder, Set<FoundExtensionPackage>> foundResult: Map<ExtensionPackageFinder, Set<FoundExtensionPackage>>
) { ) {
log.error {
val errMessage = StringBuilder( val errMessage = StringBuilder(
""" """
[Bot ${bot.botUsername}] 扩展包 $extensionArtifact 存在多个文件, 为防止安全问题, 已禁止加载该扩展包: [Bot ${bot.botUsername}] 扩展包 $extensionArtifact 存在多个文件, 为防止安全问题, 已禁止加载该扩展包:
@ -221,15 +160,9 @@ internal class ExtensionLoader(
.append(URLDecoder.decode(file.getRawUrl().toString(), StandardCharsets.UTF_8)).append('\n') .append(URLDecoder.decode(file.getRawUrl().toString(), StandardCharsets.UTF_8)).append('\n')
} }
} }
errMessage log.error { errMessage }
}
} }
/**
* 创建扩展数据目录, 并返回 [File] 对象.
* @param extensionArtifact 扩展包构件坐标.
* @return 返回对应的数据存储目录.
*/
private fun getExtensionDataFolder(extensionArtifact: Artifact): File { private fun getExtensionDataFolder(extensionArtifact: Artifact): File {
val dataFolder = val dataFolder =
File(extensionsDataFolder, "${extensionArtifact.groupId}/${extensionArtifact.artifactId}") File(extensionsDataFolder, "${extensionArtifact.groupId}/${extensionArtifact.artifactId}")
@ -239,12 +172,6 @@ internal class ExtensionLoader(
return dataFolder return dataFolder
} }
/**
* 已加载扩展项.
* @property extensionArtifact 扩展的构件坐标([Artifact]).
* @property factoryClass 扩展的工厂类.
* @property extension 扩展实例.
*/
data class LoadedExtensionEntry( data class LoadedExtensionEntry(
val extensionArtifact: Artifact, val extensionArtifact: Artifact,
val factoryClass: Class<out BotExtensionFactory>, val factoryClass: Class<out BotExtensionFactory>,
@ -254,10 +181,6 @@ internal class ExtensionLoader(
} }
/** /**
* 扩展的类加载器清除器.
*
* 原计划是用来通过关闭 ClassLoader 来卸载扩展的, 但似乎并没有这么做.
*
* 该类为保留措施, 尚未启用. * 该类为保留措施, 尚未启用.
*/ */
internal object ExtensionClassLoaderCleaner { internal object ExtensionClassLoaderCleaner {
@ -334,7 +257,7 @@ internal interface ExtensionPackageFinder {
/** /**
* 已找到的扩展包信息. * 已找到的扩展包信息.
* 通过实现该接口, 以寻找远端文件的 [ExtensionPackageFinder]; * 通过实现该接口, 以寻找远端文件的 [ExtensionPackageFinder]
* 可以在适当的时候将扩展包下载到本地, 而无需在搜索阶段下载扩展包. * 可以在适当的时候将扩展包下载到本地, 而无需在搜索阶段下载扩展包.
*/ */
internal interface FoundExtensionPackage { internal interface FoundExtensionPackage {
@ -373,7 +296,6 @@ private fun FoundExtensionPackage.createClassLoader(): ExtensionClassLoader =
* 已找到的扩展包文件. * 已找到的扩展包文件.
* @param artifact 扩展包构件坐标. * @param artifact 扩展包构件坐标.
* @param file 已找到的扩展包文件. * @param file 已找到的扩展包文件.
* @param finder 搜索到该扩展包的搜索器.
*/ */
internal class FileFoundExtensionPackage( internal class FileFoundExtensionPackage(
private val artifact: Artifact, private val artifact: Artifact,
@ -420,14 +342,14 @@ internal class ExtensionClassLoader(urls: Array<URL>, dependencyLoader: ClassLoa
// 以免使用了不来自扩展包的机器人扩展. // 以免使用了不来自扩展包的机器人扩展.
override fun getResources(name: String?): Enumeration<URL> { override fun getResources(name: String?): Enumeration<URL> {
if ("META-INF/services/${BotExtensionFactory::class.java.name}" == name) { if (BotExtensionFactory::class.java.equals(name)) {
return findResources(name) return findResources(name)
} }
return super.getResources(name) return super.getResources(name)
} }
override fun getResource(name: String?): URL? { override fun getResource(name: String?): URL? {
if ("META-INF/services/${BotExtensionFactory::class.java}" == name) { if (BotExtensionFactory::class.java.equals(name)) {
return findResource(name) return findResource(name)
} }
return super.getResource(name) return super.getResource(name)

View File

@ -26,7 +26,6 @@ import org.jdom2.input.SAXBuilder
import org.jdom2.xpath.XPathFactory import org.jdom2.xpath.XPathFactory
import java.io.File import java.io.File
import java.io.InputStream import java.io.InputStream
import java.net.URI
import java.net.URL import java.net.URL
import java.net.URLClassLoader import java.net.URLClassLoader
import java.util.* import java.util.*
@ -258,25 +257,23 @@ internal class MavenRepositoryExtensionFinder(
} }
override fun findByArtifact(extensionArtifact: Artifact, extensionsPath: File): Set<FoundExtensionPackage> { override fun findByArtifact(extensionArtifact: Artifact, extensionsPath: File): Set<FoundExtensionPackage> {
val repositories = repositorySystem.newResolutionRepositories(repoSystemSession, remoteRepositories).toList()
log.debug { log.debug {
StringBuilder().apply { StringBuilder().apply {
append("构件 $extensionArtifact 将在以下仓库拉取: \n") append("构件 $extensionArtifact 将在以下仓库拉取: \n")
repositories.forEach { remoteRepositories.forEach {
append("\t- $it\n") append("\t- ${it}\n")
} }
} }
} }
val extensionArtifactResult = repositorySystem.resolveArtifact( val extensionArtifactResult = repositorySystem.resolveArtifact(
repoSystemSession, repoSystemSession,
ArtifactRequest( ArtifactRequest(
extensionArtifact, extensionArtifact,
repositories, repositorySystem.newResolutionRepositories(repoSystemSession, remoteRepositories),
null null
) )
) )
val resolvedArtifact: Artifact? = extensionArtifactResult.artifact val extResolvedArtifact = extensionArtifactResult.artifact
if (!extensionArtifactResult.isResolved) { if (!extensionArtifactResult.isResolved) {
if (extensionArtifactResult.isMissing) { if (extensionArtifactResult.isMissing) {
log.warn { "在指定的仓库中找不到构件: ${extensionArtifactResult.artifact}" } log.warn { "在指定的仓库中找不到构件: ${extensionArtifactResult.artifact}" }
@ -284,26 +281,17 @@ internal class MavenRepositoryExtensionFinder(
printArtifactResultExceptions(extensionArtifactResult.exceptions) printArtifactResultExceptions(extensionArtifactResult.exceptions)
} }
return emptySet() return emptySet()
} else if (resolvedArtifact == null) {
log.warn { "无法在指定的仓库中解析构件: $extensionArtifact" }
return emptySet()
}
log.info {
"已从 Maven 仓库 `${extensionArtifactResult.repository.id}` 中找到" +
"扩展包 `${resolvedArtifact.groupId}:${resolvedArtifact.artifactId}` " +
"版本号 `${resolvedArtifact.version}`."
} }
val request = DependencyRequest( val request = DependencyRequest(
CollectRequest(Dependency(resolvedArtifact, null), repositories), CollectRequest(Dependency(extResolvedArtifact, null), remoteRepositories),
ScopeDependencyFilter(setOf("runtime", "compile", "provided"), null) ScopeDependencyFilter(setOf("runtime", "compile", "provided"), null)
) )
val dependencyResult = repositorySystem.resolveDependencies(repoSystemSession, request) val dependencyResult = repositorySystem.resolveDependencies(repoSystemSession, request)
val dependencies = checkAndCollectDependencyArtifacts(extensionArtifact, dependencyResult.artifactResults) val dependencies = checkAndCollectDependencyArtifacts(extensionArtifact, dependencyResult.artifactResults)
?: return emptySet() ?: return emptySet()
return setOf(MavenExtensionPackage(this, resolvedArtifact, extensionArtifactResult.repository, dependencies)) return setOf(MavenExtensionPackage(this, extResolvedArtifact, extensionArtifactResult.repository, dependencies))
} }
private fun checkAndCollectDependencyArtifacts( private fun checkAndCollectDependencyArtifacts(
@ -397,6 +385,7 @@ internal class MavenRepositoryExtensionFinder(
/** /**
* Maven 中央仓库 Url. * Maven 中央仓库 Url.
*/ */
@Suppress("MemberVisibilityCanBePrivate")
const val MAVEN_CENTRAL_URL = "https://repo1.maven.org/maven2/" const val MAVEN_CENTRAL_URL = "https://repo1.maven.org/maven2/"
/** /**
@ -466,19 +455,17 @@ internal class MavenRepositoryExtensionFinder(
throw IllegalArgumentException("Unsupported FoundExtensionPackage type: $foundExtensionPackage") throw IllegalArgumentException("Unsupported FoundExtensionPackage type: $foundExtensionPackage")
} }
val urls = mutableSetOf<URI>() val urls = mutableSetOf<URL>()
for (dependency in foundExtensionPackage.dependencies) { for (dependency in foundExtensionPackage.dependencies) {
val dependencyFile = dependency.file ?: continue val dependencyFile = dependency.file ?: continue
urls.add(dependencyFile.toURI()) urls.add(dependencyFile.toURI().toURL())
} }
// 将依赖的 ClassLoader 与 ExtensionPackage 的 ClassLoader 分开 // 将依赖的 ClassLoader 与 ExtensionPackage 的 ClassLoader 分开
// 这么做可以防范依赖中隐藏的 SPI 注册, 避免安全隐患. // 这么做可以防范依赖中隐藏的 SPI 注册, 避免安全隐患.
val dependenciesUrlArray = urls.toTypedArray() val dependenciesUrlArray = urls.toTypedArray()
val dependenciesClassLoader = URLClassLoader( val dependenciesClassLoader = URLClassLoader(dependenciesUrlArray)
dependenciesUrlArray.map { it.toURL() }.toTypedArray()
)
return ExtensionClassLoader( return ExtensionClassLoader(
arrayOf(foundExtensionPackage.getPackageFile().toURI().toURL()), arrayOf(foundExtensionPackage.getPackageFile().toURI().toURL()),

View File

@ -6,42 +6,43 @@ import io.prometheus.client.Summary
import mu.KotlinLogging import mu.KotlinLogging
import net.lamgc.scalabot.config.BotConfig import net.lamgc.scalabot.config.BotConfig
import org.eclipse.aether.artifact.Artifact import org.eclipse.aether.artifact.Artifact
import org.telegram.telegrambots.abilitybots.api.bot.AbilityBot import org.telegram.abilitybots.api.bot.AbilityBot
import org.telegram.telegrambots.abilitybots.api.db.DBContext import org.telegram.abilitybots.api.db.DBContext
import org.telegram.telegrambots.abilitybots.api.objects.Ability import org.telegram.abilitybots.api.objects.Ability
import org.telegram.telegrambots.abilitybots.api.toggle.BareboneToggle import org.telegram.abilitybots.api.toggle.BareboneToggle
import org.telegram.telegrambots.abilitybots.api.toggle.DefaultToggle import org.telegram.abilitybots.api.toggle.DefaultToggle
import org.telegram.telegrambots.bots.DefaultBotOptions
import org.telegram.telegrambots.meta.api.methods.commands.DeleteMyCommands import org.telegram.telegrambots.meta.api.methods.commands.DeleteMyCommands
import org.telegram.telegrambots.meta.api.methods.commands.SetMyCommands import org.telegram.telegrambots.meta.api.methods.commands.SetMyCommands
import org.telegram.telegrambots.meta.api.objects.Update import org.telegram.telegrambots.meta.api.objects.Update
import org.telegram.telegrambots.meta.api.objects.commands.BotCommand import org.telegram.telegrambots.meta.api.objects.commands.BotCommand
import org.telegram.telegrambots.meta.generics.TelegramClient
/** /**
* 可扩展 Bot. * 可扩展 Bot.
* @property creatorId 机器人所有人的 Telegram 用户 ID. 可通过联系部分机器人来获取该信息. * @property creatorId 机器人所有人的 Telegram 用户 Id. 可通过联系部分机器人来获取该信息.
* (e.g. [@userinfobot](http://t.me/userinfobot)) * (e.g. [@userinfobot](http://t.me/userinfobot))
* @param db 机器人数据库对象. 用于状态机等用途. * @param db 机器人数据库对象. 用于状态机等用途.
* @param options AbilityBot 设置对象.
* @property extensions 扩展坐标集合. * @property extensions 扩展坐标集合.
*/ */
@Suppress("CanBeParameter", "MemberVisibilityCanBePrivate")
internal class ScalaBot( internal class ScalaBot(
db: DBContext, db: DBContext,
client: TelegramClient, options: DefaultBotOptions,
extensionFinders: Set<ExtensionPackageFinder>, extensionFinders: Set<ExtensionPackageFinder>,
val botConfig: BotConfig, botConfig: BotConfig,
val accountId: Long = botConfig.account.id,
private val creatorId: Long = botConfig.account.creatorId, private val creatorId: Long = botConfig.account.creatorId,
val accountId: Long = botConfig.account.id,
val extensions: Set<Artifact> = botConfig.extensions val extensions: Set<Artifact> = botConfig.extensions
) : ) :
AbilityBot( AbilityBot(
client, botConfig.account.token,
botConfig.account.name, botConfig.account.name,
db, db,
if (botConfig.disableBuiltInAbility) if (botConfig.disableBuiltInAbility)
BareboneToggle() BareboneToggle()
else else
DefaultToggle() DefaultToggle(),
options
) { ) {
private val extensionLoader = ExtensionLoader( private val extensionLoader = ExtensionLoader(
@ -49,8 +50,6 @@ internal class ScalaBot(
extensionFinders = extensionFinders extensionFinders = extensionFinders
) )
private val accountIdString = accountId.toString()
init { init {
log.info { "[Bot $botUsername] 正在加载扩展..." } log.info { "[Bot $botUsername] 正在加载扩展..." }
val extensionEntries = extensionLoader.getExtensions() val extensionEntries = extensionLoader.getExtensions()
@ -66,19 +65,19 @@ internal class ScalaBot(
override fun creatorId(): Long = creatorId override fun creatorId(): Long = creatorId
override fun consume(update: Update?) { override fun onUpdateReceived(update: Update?) {
botUpdateCounter.labels(botUsername, accountIdString).inc() botUpdateCounter.labels(botUsername).inc()
botUpdateGauge.labels(botUsername, accountIdString).inc() botUpdateGauge.labels(botUsername).inc()
val timer = updateProcessTime.labels(botUsername, accountIdString).startTimer() val timer = updateProcessTime.labels(botUsername).startTimer()
try { try {
super.consume(update) super.onUpdateReceived(update)
} catch (e: Exception) { } catch (e: Exception) {
exceptionHandlingCounter.labels(botUsername, accountIdString).inc() exceptionHandlingCounter.labels(botUsername).inc()
throw e throw e
} finally { } finally {
timer.observeDuration() timer.observeDuration()
botUpdateGauge.labels(botUsername, accountIdString).dec() botUpdateGauge.labels(botUsername).dec()
} }
} }
@ -91,11 +90,11 @@ internal class ScalaBot(
* @return 更新成功返回 `true`. * @return 更新成功返回 `true`.
*/ */
fun updateCommandList(): Boolean { fun updateCommandList(): Boolean {
if (abilities == null) { if (abilities() == null) {
throw IllegalStateException("Abilities has not been initialized.") throw IllegalStateException("Abilities has not been initialized.")
} }
val botCommands = abilities.values.map { val botCommands = abilities().values.map {
val abilityInfo = if (it.info() == null || it.info().trim().isEmpty()) { val abilityInfo = if (it.info() == null || it.info().trim().isEmpty()) {
log.warn { "[Bot $botUsername] Ability `${it.name()}` 没有说明信息." } log.warn { "[Bot $botUsername] Ability `${it.name()}` 没有说明信息." }
"(The command has no description)" "(The command has no description)"
@ -111,10 +110,9 @@ internal class ScalaBot(
return true return true
} }
val setMyCommands = SetMyCommands.builder() val setMyCommands = SetMyCommands()
.commands(botCommands) setMyCommands.commands = botCommands
.build() return execute(DeleteMyCommands()) && execute(setMyCommands)
return telegramClient.execute(DeleteMyCommands()) && telegramClient.execute(setMyCommands)
} }
override fun onRegister() { override fun onRegister() {
@ -122,6 +120,10 @@ internal class ScalaBot(
onlineBotGauge.inc() onlineBotGauge.inc()
} }
override fun onClosing() {
onlineBotGauge.dec()
}
companion object { companion object {
@JvmStatic @JvmStatic
private val log = KotlinLogging.logger { } private val log = KotlinLogging.logger { }
@ -132,8 +134,7 @@ internal class ScalaBot(
private val botUpdateCounter = Counter.build() private val botUpdateCounter = Counter.build()
.name("updates_total") .name("updates_total")
.help("Total number of updates received by all bots.") .help("Total number of updates received by all bots.")
.labelNames("bot_name", "bot_id") .labelNames("bot_name")
.namespace(Const.METRICS_NAMESPACE)
.subsystem("telegrambots") .subsystem("telegrambots")
.register() .register()
@ -141,8 +142,7 @@ internal class ScalaBot(
private val botUpdateGauge = Gauge.build() private val botUpdateGauge = Gauge.build()
.name("updates_in_progress") .name("updates_in_progress")
.help("Number of updates in process by all bots.") .help("Number of updates in process by all bots.")
.labelNames("bot_name", "bot_id") .labelNames("bot_name")
.namespace(Const.METRICS_NAMESPACE)
.subsystem("telegrambots") .subsystem("telegrambots")
.register() .register()
@ -150,7 +150,6 @@ internal class ScalaBot(
private val onlineBotGauge = Gauge.build() private val onlineBotGauge = Gauge.build()
.name("bots_online") .name("bots_online")
.help("Number of bots Online.") .help("Number of bots Online.")
.namespace(Const.METRICS_NAMESPACE)
.subsystem("telegrambots") .subsystem("telegrambots")
.register() .register()
@ -162,8 +161,7 @@ internal class ScalaBot(
"so it may be different from the actual execution time of ability. " + "so it may be different from the actual execution time of ability. " +
"It is not recommended to use it as the accurate execution time of ability)" "It is not recommended to use it as the accurate execution time of ability)"
) )
.labelNames("bot_name", "bot_id") .labelNames("bot_name")
.namespace(Const.METRICS_NAMESPACE)
.subsystem("telegrambots") .subsystem("telegrambots")
.register() .register()
@ -171,8 +169,7 @@ internal class ScalaBot(
private val exceptionHandlingCounter = Counter.build() private val exceptionHandlingCounter = Counter.build()
.name("updates_exception_handling") .name("updates_exception_handling")
.help("Number of exceptions during processing.") .help("Number of exceptions during processing.")
.labelNames("bot_name", "bot_id") .labelNames("bot_name")
.namespace(Const.METRICS_NAMESPACE)
.subsystem("telegrambots") .subsystem("telegrambots")
.register() .register()
} }

View File

@ -1,29 +0,0 @@
package net.lamgc.scalabot.util
import java.util.regex.Matcher
import java.util.regex.Pattern
object TelegramBotAccounts {
private val botTokenPattern: Pattern = Pattern.compile("([1-9]\\d+):([A-Za-z\\d_-]{35,})")
/**
* 获取 AbilityBot 的账户 Id.
*
*
* 账户 Id 来自于 botToken , token 的格式为 "{AccountId}:{Secret}".
*
* 账户 Id 的真实性与 botToken 的有效性有关, 本方法并不会确保 botToken 的有效性, 一般情况下也无需考虑 Id 的有效性,
* 如果有需要, 可尝试通过调用 [org.telegram.telegrambots.meta.api.methods.GetMe] 来确保 botToken 的有效性.
*
* @param botToken 要获取账户 Id botToken 字符串.
* @return 返回 AbilityBot 的账户 Id.
* @throws IllegalArgumentException AbilityBot botToken 格式错误时抛出该异常.
*/
fun getBotAccountId(botToken: String): Long {
val matcher: Matcher = botTokenPattern.matcher(botToken)
require(matcher.matches()) { "Invalid token format." }
return matcher.group(1).toLong()
}
}

View File

@ -74,7 +74,7 @@ fun <T : AutoCloseable> T.registerShutdownHook(): T {
private object UtilsInternal { private object UtilsInternal {
val autoCloseableSet = mutableSetOf<AutoCloseable>() val autoCloseableSet = mutableSetOf<AutoCloseable>()
private val log = KotlinLogging.logger(UtilsInternal::class.java.name) private val log = KotlinLogging.logger { }
init { init {
Runtime.getRuntime().addShutdownHook(Thread(this::doCloseResources, "Shutdown-AutoCloseable")) Runtime.getRuntime().addShutdownHook(Thread(this::doCloseResources, "Shutdown-AutoCloseable"))

View File

@ -1,8 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<included> <included>
<define name="DATA_LOGS" class="net.lamgc.scalabot.LogDirectorySupplier"/> <define name="DATA_LOGS" class="net.lamgc.scalabot.LogDirectorySupplier"/>
<define name="LOG_LEVEL" class="net.lamgc.scalabot.LogLevelSupplier"/>
<define name="NETWORK_LOG_LEVEL" class="net.lamgc.scalabot.NetworkVerboseLogSupplier"/>
<appender name="STD_OUT" class="ch.qos.logback.core.ConsoleAppender"> <appender name="STD_OUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder> <encoder>
@ -26,7 +24,7 @@
<appender name="FILE_OUT" class="ch.qos.logback.core.rolling.RollingFileAppender"> <appender name="FILE_OUT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DATA_LOGS}/latest.log</file> <file>${DATA_LOGS}/latest.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${DATA_LOGS}/%d{yyyy-MM-dd}.log.gz</fileNamePattern> <fileNamePattern>data/logs/%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<maxHistory>30</maxHistory> <maxHistory>30</maxHistory>
</rollingPolicy> </rollingPolicy>
<encoder> <encoder>

View File

@ -0,0 +1,16 @@
<configuration scan="false" debug="false">
<include resource="base-logback.xml"/>
<logger name="org.apache.http" level="INFO"/>
<logger name="org.eclipse.aether.internal.impl.DefaultTransporterProvider" level="INFO"/>
<logger name="org.eclipse.aether.internal.impl.DefaultRepositoryConnectorProvider" level="INFO"/>
<logger name="org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManager" level="INFO"/>
<logger name="org.telegram.telegrambots.facilities.proxysocketfactorys" level="INFO"/>
<logger name="org.eclipse.aether.internal.impl.DefaultUpdateCheckManager" level="INFO"/>
<root level="DEBUG">
<appender-ref ref="FILE_OUT"/>
<appender-ref ref="STD_ERR"/>
<appender-ref ref="STD_OUT"/>
</root>
</configuration>

View File

@ -1,14 +1,7 @@
<configuration scan="false" debug="false"> <configuration scan="false" debug="false">
<include resource="base-logback.xml"/> <include resource="base-logback.xml"/>
<logger name="org.apache.http" level="${NETWORK_LOG_LEVEL}"/> <root level="INFO">
<logger name="org.eclipse.aether.internal.impl.DefaultTransporterProvider" level="${NETWORK_LOG_LEVEL}"/>
<logger name="org.eclipse.aether.internal.impl.DefaultRepositoryConnectorProvider" level="${NETWORK_LOG_LEVEL}"/>
<logger name="org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManager" level="${NETWORK_LOG_LEVEL}"/>
<logger name="org.telegram.telegrambots.facilities.proxysocketfactorys" level="${NETWORK_LOG_LEVEL}"/>
<logger name="org.eclipse.aether.internal.impl.DefaultUpdateCheckManager" level="${NETWORK_LOG_LEVEL}"/>
<root level="${LOG_LEVEL}">
<appender-ref ref="FILE_OUT"/> <appender-ref ref="FILE_OUT"/>
<appender-ref ref="STD_ERR"/> <appender-ref ref="STD_ERR"/>
<appender-ref ref="STD_OUT"/> <appender-ref ref="STD_OUT"/>

View File

@ -4,50 +4,31 @@ import com.github.stefanbirkner.systemlambda.SystemLambda
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.verify import io.mockk.verify
import mu.KotlinLogging
import net.lamgc.scalabot.config.MavenRepositoryConfig
import net.lamgc.scalabot.config.ProxyConfig
import net.lamgc.scalabot.config.ProxyType
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.io.TempDir import org.junit.jupiter.api.io.TempDir
import java.io.File import java.io.File
import java.io.IOException import kotlin.test.Test
import java.net.Proxy import kotlin.test.assertEquals
import java.net.URL import kotlin.test.assertNotNull
import java.nio.file.Files import kotlin.test.assertTrue
import java.nio.file.Path
import kotlin.io.path.deleteExisting
import kotlin.test.*
internal class AppPathsTest { internal class AppPathsTest {
@Test
fun `Consistency check`() {
for (path in AppPaths.entries) {
assertEquals(
File(path.path).canonicalPath,
path.file.canonicalPath,
"路径 File 与 Path 不一致: ${path.name}"
)
}
}
@Test @Test
fun `Data root path priority`() { fun `Data root path priority`() {
System.setProperty(AppPaths.PathConst.PROP_DATA_PATH, "fromSystemProperties") System.setProperty("bot.path.data", "fromSystemProperties")
assertEquals("fromSystemProperties", AppPaths.DATA_ROOT.file.path, "`DATA_ROOT`没有优先返回 Property 的值.") assertEquals("fromSystemProperties", AppPaths.DATA_ROOT.file.path, "`DATA_ROOT`没有优先返回 Property 的值.")
System.getProperties().remove(AppPaths.PathConst.PROP_DATA_PATH) System.getProperties().remove("bot.path.data")
val expectEnvValue = "fromEnvironmentVariable" val expectEnvValue = "fromEnvironmentVariable"
SystemLambda.withEnvironmentVariable(AppPaths.PathConst.ENV_DATA_PATH, expectEnvValue).execute { SystemLambda.withEnvironmentVariable("BOT_DATA_PATH", expectEnvValue).execute {
assertEquals( assertEquals(
expectEnvValue, AppPaths.DATA_ROOT.file.path, expectEnvValue, AppPaths.DATA_ROOT.file.path,
"`DATA_ROOT`没有优先返回 env 的值." "`DATA_ROOT`没有优先返回 env 的值."
) )
} }
SystemLambda.withEnvironmentVariable(AppPaths.PathConst.ENV_DATA_PATH, null).execute { SystemLambda.withEnvironmentVariable("BOT_DATA_PATH", null).execute {
assertEquals( assertEquals(
System.getProperty("user.dir"), AppPaths.DATA_ROOT.file.path, System.getProperty("user.dir"), AppPaths.DATA_ROOT.file.path,
"`DATA_ROOT`没有返回 System.properties `user.dir` 的值." "`DATA_ROOT`没有返回 System.properties `user.dir` 的值."
@ -135,282 +116,5 @@ internal class AppPathsTest {
defaultInitializerMethod.isAccessible = false defaultInitializerMethod.isAccessible = false
} }
@Test
fun `loadBotConfig test`(@TempDir testDir: File) {
assertNull(loadBotConfigJson(File("/NOT_EXISTS_FILE")), "加载 BotConfigs 失败时应该返回 null.")
SystemLambda.withEnvironmentVariable(AppPaths.PathConst.ENV_DATA_PATH, testDir.canonicalPath).execute {
assertNull(loadBotConfigJson(), "加载 BotConfigs 失败时应该返回 null.")
File(testDir, "bot.json").apply {
//language=JSON5
writeText(
"""
[
{
"enabled": false,
"account": {
"name": "TestBot",
"token": "123456789:AAHErDroUTznQsOd_oZPJ6cQEj4Z5mGHO10",
"creatorId": 123456789
},
"proxy": {
"host": "localhost",
"port": 8080,
"type": "HTTP"
},
"disableBuiltInAbility": false,
"autoUpdateCommandList": true,
"extensions": [
"org.example.test:test-extension:1.0.0"
],
"baseApiUrl": "http://localhost:8080"
}
]
""".trimIndent()
)
} }
val botConfigJsons = loadBotConfigJson()
assertNotNull(botConfigJsons)
assertEquals(1, botConfigJsons.size())
}
}
@Test
fun `loadAppConfig test`(@TempDir testDir: File) {
assertThrows<IOException>("加载失败时应该抛出 IOException.") {
loadAppConfig(File("/NOT_EXISTS_FILE"))
}
SystemLambda.withEnvironmentVariable(AppPaths.PathConst.ENV_DATA_PATH, testDir.canonicalPath).execute {
assertThrows<IOException>("加载失败时应该抛出 IOException.") {
loadAppConfig()
}
File(testDir, "config.json").apply {
//language=JSON5
writeText(
"""
{
"proxy": {
"type": "HTTP",
"host": "localhost",
"port": 8080
},
"metrics": {
"enable": true,
"port": 8800,
"bindAddress": "127.0.0.1",
"authenticator": {
"username": "username",
"password": "password"
}
},
"mavenRepositories": [
{
"url": "https://repository.maven.apache.org/maven2/"
}
],
"mavenLocalRepository": "file:///tmp/maven-local-repository"
}
""".trimIndent()
)
}
val appConfigs = loadAppConfig()
assertNotNull(appConfigs)
}
}
@Test
fun `ProxyType_toTelegramBotsType test`() {
val expectTypeMapping = mapOf(
ProxyType.NO_PROXY to null,
ProxyType.SOCKS5 to Proxy.Type.SOCKS,
ProxyType.SOCKS4 to Proxy.Type.SOCKS,
ProxyType.HTTP to Proxy.Type.HTTP,
ProxyType.HTTPS to Proxy.Type.HTTP
)
for (proxyType in ProxyType.entries) {
assertEquals(
expectTypeMapping[proxyType],
proxyType.toJavaProxyType(),
"ProxyType 转换失败."
)
}
}
@Test
fun `ProxyConfig_toAetherProxy test`() {
val host = "proxy.example.org"
val port = 1080
val expectNotNullProxyType = setOf(
ProxyType.HTTP,
ProxyType.HTTPS
)
for (proxyType in ProxyType.entries) {
val proxyConfig = ProxyConfig(proxyType, host, port)
val aetherProxy = proxyConfig.toAetherProxy()
if (expectNotNullProxyType.contains(proxyType)) {
assertNotNull(aetherProxy, "支持的代理类型应该不为 null.")
assertEquals(host, aetherProxy.host)
assertEquals(port, aetherProxy.port)
} else {
assertNull(aetherProxy, "不支持的代理类型应该返回 null.")
}
}
}
@Test
fun `MavenRepositoryConfig_toRemoteRepository test`() {
val defaultMavenRepositoryConfig = MavenRepositoryConfig(
url = URL(MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL),
enableReleases = true,
enableSnapshots = false
)
val remoteRepositoryWithoutId = defaultMavenRepositoryConfig.toRemoteRepository(
ProxyConfig(ProxyType.NO_PROXY, "", 0)
)
assertEquals(MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL, remoteRepositoryWithoutId.url.toString())
assertNotNull(remoteRepositoryWithoutId.id)
assertTrue(remoteRepositoryWithoutId.getPolicy(false).isEnabled)
assertFalse(remoteRepositoryWithoutId.getPolicy(true).isEnabled)
val remoteRepositoryWithId = defaultMavenRepositoryConfig.copy(id = "test-repo").toRemoteRepository(
ProxyConfig(ProxyType.HTTP, "127.0.0.1", 1080)
)
assertEquals("test-repo", remoteRepositoryWithId.id)
assertEquals(MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL, remoteRepositoryWithId.url.toString())
assertEquals("http", remoteRepositoryWithId.proxy.type)
assertEquals("127.0.0.1", remoteRepositoryWithId.proxy.host)
assertEquals(1080, remoteRepositoryWithId.proxy.port)
assertEquals(remoteRepositoryWithId.id, remoteRepositoryWithId.id)
val remoteRepositoryWithProxy = defaultMavenRepositoryConfig.copy(
id = "test-repo",
proxy = ProxyConfig(ProxyType.HTTP, "example.org", 1080).toAetherProxy()
).toRemoteRepository(ProxyConfig(ProxyType.HTTP, "localhost", 8080))
assertEquals("http", remoteRepositoryWithProxy.proxy.type)
assertEquals("example.org", remoteRepositoryWithProxy.proxy.host, "未优先使用 MavenRepositoryConfig 中的 proxy 属性.")
assertEquals(1080, remoteRepositoryWithProxy.proxy.port, "未优先使用 MavenRepositoryConfig 中的 proxy 属性.")
}
@Test
fun `checkRepositoryLayout test`() {
val noProxyConfig = ProxyConfig(ProxyType.NO_PROXY, "", 0)
assertEquals(
"default", MavenRepositoryConfig(url = URL("https://repo.example.org"))
.toRemoteRepository(noProxyConfig).contentType
)
assertEquals(
"legacy", MavenRepositoryConfig(url = URL("https://repo.example.org"), layout = "LEgaCY")
.toRemoteRepository(noProxyConfig).contentType
)
assertThrows<IllegalArgumentException> {
MavenRepositoryConfig(
url = URL("https://repo.example.org"),
layout = "NOT_EXISTS_LAYOUT"
).toRemoteRepository(noProxyConfig)
}
}
@Test
fun `initialFiles test`(@TempDir testDir: Path) {
// 这么做是为了让日志文件创建在其他地方, 由于日志文件在运行时会持续占用, 在 windows 中文件会被锁定,
// 导致测试框架无法正常清除测试所使用的临时文件夹.
val logsDir = Files.createTempDirectory("ammmmmm-logs-")
System.setProperty(AppPaths.PathConst.PROP_DATA_PATH, logsDir.toString())
assertEquals(logsDir.toString(), AppPaths.DATA_ROOT.path, "日志目录设定失败.")
KotlinLogging.logger("TEST").error { "日志占用.(无需理会), 日志目录: $logsDir" }
AppPaths.DATA_LOGS.file.listFiles { _, name -> name.endsWith(".log") }?.forEach {
it.deleteOnExit()
}
val fullInitializeDir = Files.createTempDirectory(testDir, "fullInitialize")
fullInitializeDir.deleteExisting()
System.setProperty(AppPaths.PathConst.PROP_DATA_PATH, fullInitializeDir.toString())
assertEquals(fullInitializeDir.toString(), AppPaths.DATA_ROOT.path, "测试路径设定失败.")
assertTrue(initialFiles(), "方法未能提醒用户编辑初始配置文件.")
for (path in AppPaths.entries) {
assertTrue(path.file.exists(), "文件未初始化成功: ${path.path}")
if (path.file.isFile) {
assertNotEquals(0, path.file.length(), "文件未初始化成功(大小为 0): ${path.path}")
}
path.reset()
}
assertFalse(initialFiles(), "方法试图在配置已初始化的情况下提醒用户编辑初始配置文件.")
for (path in AppPaths.entries) {
assertTrue(path.file.exists(), "文件未初始化成功: ${path.path}")
if (path.file.isFile) {
assertNotEquals(0, path.file.length(), "文件未初始化成功(大小为 0): ${path.path}")
}
path.reset()
}
assertTrue(AppPaths.CONFIG_APPLICATION.file.delete(), "config.json 删除失败.")
assertFalse(initialFiles(), "方法试图在部分配置已初始化的情况下提醒用户编辑初始配置文件.")
for (path in AppPaths.entries) {
assertTrue(path.file.exists(), "文件未初始化成功: ${path.path}")
if (path.file.isFile) {
assertNotEquals(0, path.file.length(), "文件未初始化成功(大小为 0): ${path.path}")
}
path.reset()
}
assertTrue(AppPaths.CONFIG_BOT.file.delete(), "bot.json 删除失败.")
assertFalse(initialFiles(), "方法试图在部分配置已初始化的情况下提醒用户编辑初始配置文件.")
for (path in AppPaths.entries) {
assertTrue(path.file.exists(), "文件未初始化成功: ${path.path}")
if (path.file.isFile) {
assertNotEquals(0, path.file.length(), "文件未初始化成功(大小为 0): ${path.path}")
}
path.reset()
}
assertTrue(AppPaths.CONFIG_APPLICATION.file.delete(), "config.json 删除失败.")
assertTrue(AppPaths.CONFIG_BOT.file.delete(), "bot.json 删除失败.")
assertTrue(
initialFiles(),
"在主要配置文件(config.json 和 bot.json)不存在的情况下初始化文件后, 方法未能提醒用户编辑初始配置文件."
)
for (path in AppPaths.entries) {
assertTrue(path.file.exists(), "文件未初始化成功: ${path.path}")
if (path.file.isFile) {
assertNotEquals(0, path.file.length(), "文件未初始化成功(大小为 0): ${path.path}")
}
}
AppPaths.CONFIG_APPLICATION.file.writeText("Test-APPLICATION")
AppPaths.CONFIG_BOT.file.writeText("Test-BOT")
assertFalse(initialFiles(), "方法试图在部分配置已初始化的情况下提醒用户编辑初始配置文件.")
assertEquals(
"Test-APPLICATION", AppPaths.CONFIG_APPLICATION.file.readText(),
"config.json 被覆盖. initialized 并未阻止重复初始化."
)
assertEquals(
"Test-BOT", AppPaths.CONFIG_BOT.file.readText(),
"bot.json 被覆盖. initialized 并未阻止重复初始化."
)
System.getProperties().remove(AppPaths.PathConst.PROP_DATA_PATH)
}
private fun AppPaths.reset() {
val method = AppPaths::class.java.getDeclaredMethod("reset")
method.isAccessible = true
method.invoke(this)
method.isAccessible = false
}
}

View File

@ -1,27 +0,0 @@
package util
import net.lamgc.scalabot.util.TelegramBotAccounts
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test
class TelegramBotAccountsTest {
@Test
fun getBotAccountIdTest() {
val expectToken = "1234567890:AAHXcNDBRZTKfyPED5Gi3PZDIKPOM6xhxwo"
val actual: Long = TelegramBotAccounts.getBotAccountId(expectToken)
assertEquals(1234567890, actual)
val badTokenA = "12c34d56a7890:AAHXcNDBRZTKfyPED5Gi3PZDIKPOM6xhxwo"
assertThrows(
IllegalArgumentException::class.java
) { TelegramBotAccounts.getBotAccountId(badTokenA) }
val badTokenB = "12c34d56a7890AAHXcNDBRZTKfyPED5Gi3PZDIKPOM6xhxwo"
assertThrows(
IllegalArgumentException::class.java
) { TelegramBotAccounts.getBotAccountId(badTokenB) }
}
}

View File

@ -5,7 +5,7 @@ plugins {
dependencies { dependencies {
compileOnly(project(":scalabot-extension")) compileOnly(project(":scalabot-extension"))
testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.1") testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
} }

View File

@ -1,8 +1,8 @@
package net.lamgc.scalabot.simple; package net.lamgc.scalabot.simple;
import org.telegram.telegrambots.abilitybots.api.bot.BaseAbilityBot; import org.telegram.abilitybots.api.bot.BaseAbilityBot;
import org.telegram.telegrambots.abilitybots.api.objects.*; import org.telegram.abilitybots.api.objects.*;
import org.telegram.telegrambots.abilitybots.api.util.AbilityExtension; import org.telegram.abilitybots.api.util.AbilityExtension;
public class SayHelloExtension implements AbilityExtension { public class SayHelloExtension implements AbilityExtension {
@ -27,7 +27,7 @@ public class SayHelloExtension implements AbilityExtension {
String msg = "Hello! " + ctx.user().getUserName() + String msg = "Hello! " + ctx.user().getUserName() +
" ( " + ctx.user().getId() + " ) [ " + ctx.user().getLanguageCode() + " ]" + "\n" + " ( " + ctx.user().getId() + " ) [ " + ctx.user().getLanguageCode() + " ]" + "\n" +
"Current Chat ID: " + ctx.chatId(); "Current Chat ID: " + ctx.chatId();
ctx.bot().getSilent().send(msg, ctx.chatId()); ctx.bot().silent().send(msg, ctx.chatId());
}) })
.build(); .build();
} }
@ -36,13 +36,13 @@ public class SayHelloExtension implements AbilityExtension {
* 更具特色的 `Say hello`. * 更具特色的 `Say hello`.
*/ */
public Ability test() { public Ability test() {
ReplyFlow botHello = ReplyFlow.builder(bot.getDb()) ReplyFlow botHello = ReplyFlow.builder(bot.db())
.enableStats("say_hello") .enableStats("say_hello")
.action((bot, upd) -> bot.getSilent().send("What is u name?", upd.getMessage().getChatId())) .action((bot, upd) -> bot.silent().send("What is u name?", upd.getMessage().getChatId()))
.onlyIf(update -> update.hasMessage() .onlyIf(update -> update.hasMessage()
&& update.getMessage().hasText() && update.getMessage().hasText()
&& "hello".equalsIgnoreCase(update.getMessage().getText())) && "hello".equalsIgnoreCase(update.getMessage().getText()))
.next(Reply.of((bot, upd) -> bot.getSilent() .next(Reply.of((bot, upd) -> bot.silent()
.send("OK! You name is " + upd.getMessage().getText().substring("my name is ".length()), upd.getMessage().getChatId()), .send("OK! You name is " + upd.getMessage().getText().substring("my name is ".length()), upd.getMessage().getChatId()),
upd -> upd.hasMessage() upd -> upd.hasMessage()
&& upd.getMessage().hasText() && upd.getMessage().hasText()
@ -55,7 +55,7 @@ public class SayHelloExtension implements AbilityExtension {
.locality(Locality.ALL) .locality(Locality.ALL)
.privacy(Privacy.PUBLIC) .privacy(Privacy.PUBLIC)
.enableStats() .enableStats()
.action(ctx -> ctx.bot().getSilent().send("Hello!", ctx.chatId())) .action(ctx -> ctx.bot().silent().send("Hello!", ctx.chatId()))
.reply(botHello) .reply(botHello)
.build(); .build();
} }

View File

@ -1,16 +1,15 @@
package net.lamgc.scalabot.simple; package net.lamgc.scalabot.simple;
import net.lamgc.scalabot.extension.BotExtensionCreateOptions;
import net.lamgc.scalabot.extension.BotExtensionFactory; import net.lamgc.scalabot.extension.BotExtensionFactory;
import org.telegram.telegrambots.abilitybots.api.bot.BaseAbilityBot; import org.telegram.abilitybots.api.bot.BaseAbilityBot;
import org.telegram.telegrambots.abilitybots.api.util.AbilityExtension; import org.telegram.abilitybots.api.util.AbilityExtension;
import java.io.File; import java.io.File;
public class SimpleExtensionFactory implements BotExtensionFactory { public class SimpleExtensionFactory implements BotExtensionFactory {
@Override @Override
public AbilityExtension createExtensionInstance(BaseAbilityBot bot, File shareDataFolder, BotExtensionCreateOptions options) { public AbilityExtension createExtensionInstance(BaseAbilityBot bot, File shareDataFolder) {
return new SayHelloExtension(bot); return new SayHelloExtension(bot);
} }

View File

@ -1,18 +1,16 @@
plugins { plugins {
`java-library` kotlin("jvm")
jacoco java
`maven-publish` `maven-publish`
signing signing
} }
dependencies { dependencies {
implementation("commons-codec:commons-codec:1.16.1") api("org.telegram:telegrambots-abilities:6.0.1")
api("org.telegram:telegrambots-abilities:8.0.0") api("org.slf4j:slf4j-api:1.7.36")
api(project(":scalabot-meta"))
testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.1") testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
testImplementation("org.mockito:mockito-core:5.11.0") testImplementation("org.mockito:mockito-core:4.6.1")
testImplementation("org.telegram:telegrambots-client:8.0.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
} }
@ -25,30 +23,29 @@ tasks.withType<Javadoc> {
java { java {
withJavadocJar() withJavadocJar()
withSourcesJar() withSourcesJar()
sourceCompatibility = JavaVersion.VERSION_17 sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_11
} }
tasks.test { tasks.test {
useJUnitPlatform() useJUnitPlatform()
finalizedBy(tasks.jacocoTestReport)
}
tasks.jacocoTestReport {
dependsOn(tasks.test)
}
tasks.withType<AbstractArchiveTask>().configureEach {
isPreserveFileTimestamps = false
isReproducibleFileOrder = true
} }
publishing { publishing {
repositories { repositories {
maven("https://git.lamgc.me/api/packages/LamGC/maven") { if (project.version.toString().endsWith("-SNAPSHOT")) {
maven("https://nexus.kuku.me/repository/maven-snapshots/") {
credentials { credentials {
username = project.properties["repo.credentials.self-git.username"].toString() username = project.properties["repo.credentials.private.username"].toString()
password = project.properties["repo.credentials.self-git.password"].toString() password = project.properties["repo.credentials.private.password"].toString()
}
}
} else {
maven("https://nexus.kuku.me/repository/maven-releases/") {
credentials {
username = project.properties["repo.credentials.private.username"].toString()
password = project.properties["repo.credentials.private.password"].toString()
}
} }
} }
} }

View File

@ -1,44 +0,0 @@
package net.lamgc.scalabot.extension;
import net.lamgc.scalabot.config.ProxyConfig;
/**
* BotExtension 创建参数.
* <p>
* 通过该类可向 {@link BotExtensionFactory} 提供更多创建 BotExtension 时可用的参数.
*/
@SuppressWarnings("unused")
public class BotExtensionCreateOptions {
private final long botAccountId;
private final ProxyConfig proxy;
/**
* 构造新的 BotExtensionCreateOptions.
*
* @param botAccountId 创建扩展的 Bot 账户 Id.
* @param proxy Bot 所使用的代理配置.
*/
public BotExtensionCreateOptions(long botAccountId, ProxyConfig proxy) {
this.botAccountId = botAccountId;
this.proxy = proxy;
}
/**
* 获取 Bot 使用的代理信息.
*
* @return 返回 Bot TelegramClient 所使用的代理配置.
*/
public ProxyConfig getProxy() {
return proxy;
}
/**
* 获取 Bot 的账户 Id.
*
* @return 返回 Bot 的账户 Id.
*/
public long getBotAccountId() {
return botAccountId;
}
}

View File

@ -1,8 +1,7 @@
package net.lamgc.scalabot.extension; package net.lamgc.scalabot.extension;
import org.telegram.telegrambots.abilitybots.api.bot.BaseAbilityBot; import org.telegram.abilitybots.api.bot.BaseAbilityBot;
import org.telegram.telegrambots.abilitybots.api.db.DBContext; import org.telegram.abilitybots.api.util.AbilityExtension;
import org.telegram.telegrambots.abilitybots.api.util.AbilityExtension;
import java.io.File; import java.io.File;
@ -10,7 +9,7 @@ import java.io.File;
* 该接口用于为指定的 {@link BaseAbilityBot} 创建扩展. * 该接口用于为指定的 {@link BaseAbilityBot} 创建扩展.
* *
* <p> 由于 AbilityExtension 无法直接获取 {@link BaseAbilityBot} * <p> 由于 AbilityExtension 无法直接获取 {@link BaseAbilityBot}
* 数据库对象 {@link DBContext}, * 数据库对象 {@link org.telegram.abilitybots.api.db.DBContext},
* 所以将通过该接口工厂来创建扩展对象. * 所以将通过该接口工厂来创建扩展对象.
* *
* @author LamGC * @author LamGC
@ -21,7 +20,7 @@ public interface BotExtensionFactory {
/** /**
* 为给定的 {@link BaseAbilityBot} 对象创建扩展. * 为给定的 {@link BaseAbilityBot} 对象创建扩展.
* *
* <p> 如扩展无使用 {@link DBContext} 的话, * <p> 如扩展无使用 {@link org.telegram.abilitybots.api.db.DBContext} 的话,
* 也可以返回扩展单例, 因为 AbilityBot 本身并不禁止多个机器人共用一个扩展对象 * 也可以返回扩展单例, 因为 AbilityBot 本身并不禁止多个机器人共用一个扩展对象
* (AbilityBot 只是调用了扩展中的方法来创建 Ability 对象). * (AbilityBot 只是调用了扩展中的方法来创建 Ability 对象).
* *
@ -31,10 +30,8 @@ public interface BotExtensionFactory {
* <pre> $DATA_ROOT/data/extensions/{GroupId}/{ArtifactId}</pre> * <pre> $DATA_ROOT/data/extensions/{GroupId}/{ArtifactId}</pre>
* <b>同一个扩展包的 Factory</b> 接收到的共享数据目录<b>都是一样的</b>, * <b>同一个扩展包的 Factory</b> 接收到的共享数据目录<b>都是一样的</b>,
* 建议将数据存储在数据目录中, 便于数据的存储管理. * 建议将数据存储在数据目录中, 便于数据的存储管理.
* @param options 创建扩展时可用的参数.
* @return 返回为该 Bot 对象创建的扩展对象, 如果不希望为该机器人提供扩展, 可返回 {@code null}. * @return 返回为该 Bot 对象创建的扩展对象, 如果不希望为该机器人提供扩展, 可返回 {@code null}.
* @since 0.7.0
*/ */
AbilityExtension createExtensionInstance(BaseAbilityBot bot, File shareDataFolder, BotExtensionCreateOptions options); AbilityExtension createExtensionInstance(BaseAbilityBot bot, File shareDataFolder);
} }

View File

@ -1,17 +1,38 @@
package net.lamgc.scalabot.extension.util; package net.lamgc.scalabot.extension.util;
import org.telegram.telegrambots.abilitybots.api.bot.BaseAbilityBot; import org.telegram.abilitybots.api.bot.BaseAbilityBot;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** public class AbilityBots {
* 一些开发扩展中可以用到的工具类.
*/ private final static Pattern botTokenPattern = Pattern.compile("([1-9]\\d+):([A-Za-z\\d_-]{35,})");
public final class AbilityBots {
private AbilityBots() { private AbilityBots() {
} }
/**
* 获取 AbilityBot 的账户 Id.
*
* <p> 账户 Id 来自于 botToken , token 的格式为 "[AccountId]:[Secret]".
* <p> 账户 Id 的真实性与 botToken 的有效性有关, 本方法并不会确保 botToken 的有效性, 一般情况下也无需考虑 Id 的有效性,
* 如果有需要, 可尝试通过调用 {@link org.telegram.telegrambots.meta.api.methods.GetMe} 来确保 botToken 的有效性.
*
* @param bot 要获取账户 Id AbilityBot 对象.
* @return 返回 AbilityBot 的账户 Id.
* @throws IllegalArgumentException AbilityBot botToken 格式错误时抛出该异常.
*/
public static long getBotAccountId(BaseAbilityBot bot) {
String botToken = bot.getBotToken();
Matcher matcher = botTokenPattern.matcher(botToken);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid token format.");
}
return Long.parseLong(matcher.group(1));
}
/** /**
* 取消某一对话的状态机. * 取消某一对话的状态机.
* *
@ -20,7 +41,7 @@ public final class AbilityBots {
* @return 如果状态机存在, 则删除后返回 true, 不存在(未开启任何状态机, 即没有触发任何 Reply)则返回 false. * @return 如果状态机存在, 则删除后返回 true, 不存在(未开启任何状态机, 即没有触发任何 Reply)则返回 false.
*/ */
public static boolean cancelReplyState(BaseAbilityBot bot, long chatId) { public static boolean cancelReplyState(BaseAbilityBot bot, long chatId) {
Map<Long, Integer> stateMap = bot.getDb().getMap("user_state_replies"); Map<Long, Integer> stateMap = bot.db().getMap("user_state_replies");
if (!stateMap.containsKey(chatId)) { if (!stateMap.containsKey(chatId)) {
return false; return false;
} }

View File

@ -2,35 +2,22 @@ package net.lamgc.scalabot.extension.util;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mapdb.DBMaker; import org.mapdb.DBMaker;
import org.telegram.telegrambots.abilitybots.api.bot.AbilityBot; import org.telegram.abilitybots.api.bot.AbilityBot;
import org.telegram.telegrambots.abilitybots.api.bot.BaseAbilityBot; import org.telegram.abilitybots.api.bot.BaseAbilityBot;
import org.telegram.telegrambots.abilitybots.api.db.MapDBContext; import org.telegram.abilitybots.api.db.MapDBContext;
import org.telegram.telegrambots.abilitybots.api.objects.*; import org.telegram.abilitybots.api.objects.*;
import org.telegram.telegrambots.abilitybots.api.sender.SilentSender; import org.telegram.abilitybots.api.sender.SilentSender;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.User; import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.api.objects.message.Message;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
public class AbilityBotsTest { public class AbilityBotsTest {
public static final User USER = User.builder() public static final User USER = new User(1L, "first", false, "last", "username", null, false, false, false);
.userName("username") public static final User CREATOR = new User(1337L, "creatorFirst", false, "creatorLast", "creatorUsername", null, false, false, false);
.id(1L)
.firstName("first")
.lastName("last")
.isBot(false)
.build();
public static final User CREATOR = User.builder()
.userName("creatorUsername")
.id(1337L)
.firstName("creatorFirst")
.lastName("creatorLast")
.isBot(false)
.build();
static Update mockFullUpdate(BaseAbilityBot bot, User user, String args) { static Update mockFullUpdate(BaseAbilityBot bot, User user, String args) {
bot.users().put(USER.getId(), USER); bot.users().put(USER.getId(), USER);
@ -52,67 +39,73 @@ public class AbilityBotsTest {
return update; return update;
} }
@Test
void getBotAccountIdTest() {
String expectToken = "1234567890:AAHXcNDBRZTKfyPED5Gi3PZDIKPOM6xhxwo";
long actual = AbilityBots.getBotAccountId(new TestingAbilityBot(expectToken, "test"));
assertEquals(1234567890, actual);
String badTokenA = "12c34d56a7890:AAHXcNDBRZTKfyPED5Gi3PZDIKPOM6xhxwo";
assertThrows(IllegalArgumentException.class, () ->
AbilityBots.getBotAccountId(new TestingAbilityBot(badTokenA, "test")));
String badTokenB = "12c34d56a7890AAHXcNDBRZTKfyPED5Gi3PZDIKPOM6xhxwo";
assertThrows(IllegalArgumentException.class, () ->
AbilityBots.getBotAccountId(new TestingAbilityBot(badTokenB, "test")));
}
@Test @Test
void cancelReplyStateTest() { void cancelReplyStateTest() {
User userA = User.builder() User userA = new User(10001L, "first", false, "last", "username", null, false, false, false);
.id(10001L) User userB = new User(10101L, "first", false, "last", "username", null, false, false, false);
.firstName("first")
.lastName("last")
.userName("username")
.isBot(false)
.build();
User userB = User.builder()
.id(10101L)
.firstName("first")
.lastName("last")
.userName("username")
.isBot(false)
.build();
SilentSender silent = mock(SilentSender.class); SilentSender silent = mock(SilentSender.class);
BaseAbilityBot bot = new TestingAbilityBot("", silent); BaseAbilityBot bot = new TestingAbilityBot("", "", silent);
bot.onRegister(); bot.onRegister();
bot.consume(mockFullUpdate(bot, userA, "/set_reply")); bot.onUpdateReceived(mockFullUpdate(bot, userA, "/set_reply"));
verify(silent, times(1)).send("Reply set!", userA.getId()); verify(silent, times(1)).send("Reply set!", userA.getId());
bot.consume(mockFullUpdate(bot, userA, "reply_01")); bot.onUpdateReceived(mockFullUpdate(bot, userA, "reply_01"));
verify(silent, times(1)).send("Reply 01", userA.getId()); verify(silent, times(1)).send("Reply 01", userA.getId());
assertTrue(AbilityBots.cancelReplyState(bot, userA.getId())); assertTrue(AbilityBots.cancelReplyState(bot, userA.getId()));
bot.consume(mockFullUpdate(bot, userA, "reply_02")); bot.onUpdateReceived(mockFullUpdate(bot, userA, "reply_02"));
verify(silent, never()).send("Reply 02", userA.getId()); verify(silent, never()).send("Reply 02", userA.getId());
assertFalse(AbilityBots.cancelReplyState(bot, userB.getId())); assertFalse(AbilityBots.cancelReplyState(bot, userB.getId()));
silent = mock(SilentSender.class); silent = mock(SilentSender.class);
bot = new TestingAbilityBot("", silent); bot = new TestingAbilityBot("", "", silent);
bot.onRegister(); bot.onRegister();
bot.consume(mockFullUpdate(bot, userA, "/set_reply")); bot.onUpdateReceived(mockFullUpdate(bot, userA, "/set_reply"));
verify(silent, times(1)).send("Reply set!", userA.getId()); verify(silent, times(1)).send("Reply set!", userA.getId());
bot.consume(mockFullUpdate(bot, userA, "reply_01")); bot.onUpdateReceived(mockFullUpdate(bot, userA, "reply_01"));
verify(silent, times(1)).send("Reply 01", userA.getId()); verify(silent, times(1)).send("Reply 01", userA.getId());
bot.consume(mockFullUpdate(bot, userA, "reply_02")); bot.onUpdateReceived(mockFullUpdate(bot, userA, "reply_02"));
verify(silent, times(1)).send("Reply 02", userA.getId()); verify(silent, times(1)).send("Reply 02", userA.getId());
} }
public static class TestingAbilityBot extends AbilityBot { public static class TestingAbilityBot extends AbilityBot {
public TestingAbilityBot(String botUsername, SilentSender silentSender) { public TestingAbilityBot(String botToken, String botUsername) {
super(new NoOpTelegramClient(), botUsername, new MapDBContext(DBMaker.heapDB().make())); super(botToken, botUsername, new MapDBContext(DBMaker.heapDB().make()));
}
public TestingAbilityBot(String botToken, String botUsername, SilentSender silentSender) {
super(botToken, botUsername, new MapDBContext(DBMaker.heapDB().make()));
this.silent = silentSender; this.silent = silentSender;
} }
@SuppressWarnings("unused")
public Ability setReply() { public Ability setReply() {
return Ability.builder() return Ability.builder()
.name("set_reply") .name("set_reply")
.enableStats() .enableStats()
.locality(Locality.ALL) .locality(Locality.ALL)
.privacy(Privacy.PUBLIC) .privacy(Privacy.PUBLIC)
.action(ctx -> ctx.bot().getSilent().send("Reply set!", ctx.chatId())) .action(ctx -> ctx.bot().silent().send("Reply set!", ctx.chatId()))
.reply(ReplyFlow.builder(getDb()) .reply(ReplyFlow.builder(db())
.action((bot, upd) -> bot.getSilent().send("Reply 01", upd.getMessage().getChatId())) .action((bot, upd) -> bot.silent().send("Reply 01", upd.getMessage().getChatId()))
.onlyIf(upd -> upd.hasMessage() && upd.getMessage().getText().equals("reply_01")) .onlyIf(upd -> upd.hasMessage() && upd.getMessage().getText().equals("reply_01"))
.next(Reply.of((bot, upd) -> .next(Reply.of((bot, upd) ->
bot.getSilent().send("Reply 02", upd.getMessage().getChatId()), bot.silent().send("Reply 02", upd.getMessage().getChatId()),
upd -> upd.hasMessage() && upd.getMessage().getText().equals("reply_02"))) upd -> upd.hasMessage() && upd.getMessage().getText().equals("reply_02")))
.build() .build()
) )

View File

@ -1,229 +0,0 @@
package net.lamgc.scalabot.extension.util;
import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.groupadministration.SetChatPhoto;
import org.telegram.telegrambots.meta.api.methods.send.*;
import org.telegram.telegrambots.meta.api.methods.stickers.*;
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageMedia;
import org.telegram.telegrambots.meta.api.objects.File;
import org.telegram.telegrambots.meta.api.objects.message.Message;
import org.telegram.telegrambots.meta.generics.TelegramClient;
import java.io.InputStream;
import java.io.Serializable;
import java.util.List;
import java.util.concurrent.CompletableFuture;
class NoOpTelegramClient implements TelegramClient {
@Override
public <T extends Serializable, Method extends BotApiMethod<T>> CompletableFuture<T> executeAsync(Method method) {
return null;
}
@Override
public <T extends Serializable, Method extends BotApiMethod<T>> T execute(Method method) {
return null;
}
@Override
public Message execute(SendDocument sendDocument) {
return null;
}
@Override
public Message execute(SendPhoto sendPhoto) {
return null;
}
@Override
public Boolean execute(SetWebhook setWebhook) {
return null;
}
@Override
public Message execute(SendVideo sendVideo) {
return null;
}
@Override
public Message execute(SendVideoNote sendVideoNote) {
return null;
}
@Override
public Message execute(SendSticker sendSticker) {
return null;
}
@Override
public Message execute(SendAudio sendAudio) {
return null;
}
@Override
public Message execute(SendVoice sendVoice) {
return null;
}
@Override
public List<Message> execute(SendMediaGroup sendMediaGroup) {
return List.of();
}
@Override
public List<Message> execute(SendPaidMedia sendPaidMedia) {
return List.of();
}
@Override
public Boolean execute(SetChatPhoto setChatPhoto) {
return null;
}
@Override
public Boolean execute(AddStickerToSet addStickerToSet) {
return null;
}
@Override
public Boolean execute(ReplaceStickerInSet replaceStickerInSet) {
return null;
}
@Override
public Boolean execute(SetStickerSetThumbnail setStickerSetThumbnail) {
return null;
}
@Override
public Boolean execute(CreateNewStickerSet createNewStickerSet) {
return null;
}
@Override
public File execute(UploadStickerFile uploadStickerFile) {
return null;
}
@Override
public Serializable execute(EditMessageMedia editMessageMedia) {
return null;
}
@Override
public java.io.File downloadFile(File file) {
return null;
}
@Override
public InputStream downloadFileAsStream(File file) {
return null;
}
@Override
public Message execute(SendAnimation sendAnimation) {
return null;
}
@Override
public CompletableFuture<Message> executeAsync(SendDocument sendDocument) {
return null;
}
@Override
public CompletableFuture<Message> executeAsync(SendPhoto sendPhoto) {
return null;
}
@Override
public CompletableFuture<Boolean> executeAsync(SetWebhook setWebhook) {
return null;
}
@Override
public CompletableFuture<Message> executeAsync(SendVideo sendVideo) {
return null;
}
@Override
public CompletableFuture<Message> executeAsync(SendVideoNote sendVideoNote) {
return null;
}
@Override
public CompletableFuture<Message> executeAsync(SendSticker sendSticker) {
return null;
}
@Override
public CompletableFuture<Message> executeAsync(SendAudio sendAudio) {
return null;
}
@Override
public CompletableFuture<Message> executeAsync(SendVoice sendVoice) {
return null;
}
@Override
public CompletableFuture<List<Message>> executeAsync(SendMediaGroup sendMediaGroup) {
return null;
}
@Override
public CompletableFuture<List<Message>> executeAsync(SendPaidMedia sendPaidMedia) {
return null;
}
@Override
public CompletableFuture<Boolean> executeAsync(SetChatPhoto setChatPhoto) {
return null;
}
@Override
public CompletableFuture<Boolean> executeAsync(AddStickerToSet addStickerToSet) {
return null;
}
@Override
public CompletableFuture<Boolean> executeAsync(ReplaceStickerInSet replaceStickerInSet) {
return null;
}
@Override
public CompletableFuture<Boolean> executeAsync(SetStickerSetThumbnail setStickerSetThumbnail) {
return null;
}
@Override
public CompletableFuture<Boolean> executeAsync(CreateNewStickerSet createNewStickerSet) {
return null;
}
@Override
public CompletableFuture<File> executeAsync(UploadStickerFile uploadStickerFile) {
return null;
}
@Override
public CompletableFuture<Serializable> executeAsync(EditMessageMedia editMessageMedia) {
return null;
}
@Override
public CompletableFuture<Message> executeAsync(SendAnimation sendAnimation) {
return null;
}
@Override
public CompletableFuture<java.io.File> downloadFileAsync(File file) {
return null;
}
@Override
public CompletableFuture<InputStream> downloadFileAsStreamAsync(File file) {
return null;
}
}

View File

@ -1,13 +0,0 @@
# scalabot-meta
本模块用于将 ScalaBot 的一些配置相关内容发布出去,以便于其他项目使用。
主要是配置类和相应的 Gson 序列化器(如果有,或者必要)。
## 关于序列化器
强烈建议使用序列化器!由于 Kotlin 与 Gson 之间的一些兼容性问题
(参见[本提交](https://github.com/LamGC/ScalaBot/commit/084280564af58d1af22db5b57c67577d93bd820e)
如果直接让 Gson 解析 Kotlin Data 类,将会出现一些潜在的问题(比如无法使用默认值)。
部分序列化器也可以帮助检查字段值是否合法,以防止因字段值不正确导致出现更多的问题
(例如 BotAccount 中,如果 `token` 的格式有误,那么获取 `id` 时将引发 `NumberFormatException` 异常)。

View File

@ -1,201 +0,0 @@
public final class net/lamgc/scalabot/config/AppConfig {
public fun <init> ()V
public fun <init> (Lnet/lamgc/scalabot/config/ProxyConfig;Lnet/lamgc/scalabot/config/MetricsConfig;Ljava/util/List;Ljava/lang/String;)V
public synthetic fun <init> (Lnet/lamgc/scalabot/config/ProxyConfig;Lnet/lamgc/scalabot/config/MetricsConfig;Ljava/util/List;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Lnet/lamgc/scalabot/config/ProxyConfig;
public final fun component2 ()Lnet/lamgc/scalabot/config/MetricsConfig;
public final fun component3 ()Ljava/util/List;
public final fun component4 ()Ljava/lang/String;
public final fun copy (Lnet/lamgc/scalabot/config/ProxyConfig;Lnet/lamgc/scalabot/config/MetricsConfig;Ljava/util/List;Ljava/lang/String;)Lnet/lamgc/scalabot/config/AppConfig;
public static synthetic fun copy$default (Lnet/lamgc/scalabot/config/AppConfig;Lnet/lamgc/scalabot/config/ProxyConfig;Lnet/lamgc/scalabot/config/MetricsConfig;Ljava/util/List;Ljava/lang/String;ILjava/lang/Object;)Lnet/lamgc/scalabot/config/AppConfig;
public fun equals (Ljava/lang/Object;)Z
public final fun getMavenLocalRepository ()Ljava/lang/String;
public final fun getMavenRepositories ()Ljava/util/List;
public final fun getMetrics ()Lnet/lamgc/scalabot/config/MetricsConfig;
public final fun getProxy ()Lnet/lamgc/scalabot/config/ProxyConfig;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
public final class net/lamgc/scalabot/config/BotAccount {
public fun <init> (Ljava/lang/String;Ljava/lang/String;J)V
public final fun component1 ()Ljava/lang/String;
public final fun component2 ()Ljava/lang/String;
public final fun component3 ()J
public final fun copy (Ljava/lang/String;Ljava/lang/String;J)Lnet/lamgc/scalabot/config/BotAccount;
public static synthetic fun copy$default (Lnet/lamgc/scalabot/config/BotAccount;Ljava/lang/String;Ljava/lang/String;JILjava/lang/Object;)Lnet/lamgc/scalabot/config/BotAccount;
public fun equals (Ljava/lang/Object;)Z
public final fun getCreatorId ()J
public final fun getId ()J
public final fun getName ()Ljava/lang/String;
public final fun getToken ()Ljava/lang/String;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
public final class net/lamgc/scalabot/config/BotConfig {
public fun <init> (ZLnet/lamgc/scalabot/config/BotAccount;ZZLjava/util/Set;Lnet/lamgc/scalabot/config/ProxyConfig;Ljava/lang/String;)V
public synthetic fun <init> (ZLnet/lamgc/scalabot/config/BotAccount;ZZLjava/util/Set;Lnet/lamgc/scalabot/config/ProxyConfig;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Z
public final fun component2 ()Lnet/lamgc/scalabot/config/BotAccount;
public final fun component3 ()Z
public final fun component4 ()Z
public final fun component5 ()Ljava/util/Set;
public final fun component6 ()Lnet/lamgc/scalabot/config/ProxyConfig;
public final fun component7 ()Ljava/lang/String;
public final fun copy (ZLnet/lamgc/scalabot/config/BotAccount;ZZLjava/util/Set;Lnet/lamgc/scalabot/config/ProxyConfig;Ljava/lang/String;)Lnet/lamgc/scalabot/config/BotConfig;
public static synthetic fun copy$default (Lnet/lamgc/scalabot/config/BotConfig;ZLnet/lamgc/scalabot/config/BotAccount;ZZLjava/util/Set;Lnet/lamgc/scalabot/config/ProxyConfig;Ljava/lang/String;ILjava/lang/Object;)Lnet/lamgc/scalabot/config/BotConfig;
public fun equals (Ljava/lang/Object;)Z
public final fun getAccount ()Lnet/lamgc/scalabot/config/BotAccount;
public final fun getAutoUpdateCommandList ()Z
public final fun getBaseApiTelegramUrl ()Lorg/telegram/telegrambots/meta/TelegramUrl;
public final fun getBaseApiUrl ()Ljava/lang/String;
public final fun getDisableBuiltInAbility ()Z
public final fun getEnabled ()Z
public final fun getExtensions ()Ljava/util/Set;
public final fun getProxy ()Lnet/lamgc/scalabot/config/ProxyConfig;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
public final class net/lamgc/scalabot/config/ConfigsKt {
public static final fun getDefaultTelegramApiUrl ()Ljava/lang/String;
}
public final class net/lamgc/scalabot/config/MavenRepositoryConfig {
public fun <init> (Ljava/lang/String;Ljava/net/URL;Lorg/eclipse/aether/repository/Proxy;Ljava/lang/String;ZZLorg/eclipse/aether/repository/Authentication;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/net/URL;Lorg/eclipse/aether/repository/Proxy;Ljava/lang/String;ZZLorg/eclipse/aether/repository/Authentication;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Ljava/lang/String;
public final fun component2 ()Ljava/net/URL;
public final fun component3 ()Lorg/eclipse/aether/repository/Proxy;
public final fun component4 ()Ljava/lang/String;
public final fun component5 ()Z
public final fun component6 ()Z
public final fun component7 ()Lorg/eclipse/aether/repository/Authentication;
public final fun copy (Ljava/lang/String;Ljava/net/URL;Lorg/eclipse/aether/repository/Proxy;Ljava/lang/String;ZZLorg/eclipse/aether/repository/Authentication;)Lnet/lamgc/scalabot/config/MavenRepositoryConfig;
public static synthetic fun copy$default (Lnet/lamgc/scalabot/config/MavenRepositoryConfig;Ljava/lang/String;Ljava/net/URL;Lorg/eclipse/aether/repository/Proxy;Ljava/lang/String;ZZLorg/eclipse/aether/repository/Authentication;ILjava/lang/Object;)Lnet/lamgc/scalabot/config/MavenRepositoryConfig;
public fun equals (Ljava/lang/Object;)Z
public final fun getAuthentication ()Lorg/eclipse/aether/repository/Authentication;
public final fun getEnableReleases ()Z
public final fun getEnableSnapshots ()Z
public final fun getId ()Ljava/lang/String;
public final fun getLayout ()Ljava/lang/String;
public final fun getProxy ()Lorg/eclipse/aether/repository/Proxy;
public final fun getUrl ()Ljava/net/URL;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
public final class net/lamgc/scalabot/config/MetricsConfig {
public fun <init> ()V
public fun <init> (ZILjava/lang/String;Lnet/lamgc/scalabot/config/UsernameAuthenticator;)V
public synthetic fun <init> (ZILjava/lang/String;Lnet/lamgc/scalabot/config/UsernameAuthenticator;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Z
public final fun component2 ()I
public final fun component3 ()Ljava/lang/String;
public final fun component4 ()Lnet/lamgc/scalabot/config/UsernameAuthenticator;
public final fun copy (ZILjava/lang/String;Lnet/lamgc/scalabot/config/UsernameAuthenticator;)Lnet/lamgc/scalabot/config/MetricsConfig;
public static synthetic fun copy$default (Lnet/lamgc/scalabot/config/MetricsConfig;ZILjava/lang/String;Lnet/lamgc/scalabot/config/UsernameAuthenticator;ILjava/lang/Object;)Lnet/lamgc/scalabot/config/MetricsConfig;
public fun equals (Ljava/lang/Object;)Z
public final fun getAuthenticator ()Lnet/lamgc/scalabot/config/UsernameAuthenticator;
public final fun getBindAddress ()Ljava/lang/String;
public final fun getEnable ()Z
public final fun getPort ()I
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
public final class net/lamgc/scalabot/config/ProxyConfig {
public fun <init> ()V
public fun <init> (Lnet/lamgc/scalabot/config/ProxyType;Ljava/lang/String;I)V
public synthetic fun <init> (Lnet/lamgc/scalabot/config/ProxyType;Ljava/lang/String;IILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Lnet/lamgc/scalabot/config/ProxyType;
public final fun component2 ()Ljava/lang/String;
public final fun component3 ()I
public final fun copy (Lnet/lamgc/scalabot/config/ProxyType;Ljava/lang/String;I)Lnet/lamgc/scalabot/config/ProxyConfig;
public static synthetic fun copy$default (Lnet/lamgc/scalabot/config/ProxyConfig;Lnet/lamgc/scalabot/config/ProxyType;Ljava/lang/String;IILjava/lang/Object;)Lnet/lamgc/scalabot/config/ProxyConfig;
public fun equals (Ljava/lang/Object;)Z
public final fun getHost ()Ljava/lang/String;
public final fun getPort ()I
public final fun getType ()Lnet/lamgc/scalabot/config/ProxyType;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}
public final class net/lamgc/scalabot/config/ProxyType : java/lang/Enum {
public static final field HTTP Lnet/lamgc/scalabot/config/ProxyType;
public static final field HTTPS Lnet/lamgc/scalabot/config/ProxyType;
public static final field NO_PROXY Lnet/lamgc/scalabot/config/ProxyType;
public static final field SOCKS4 Lnet/lamgc/scalabot/config/ProxyType;
public static final field SOCKS5 Lnet/lamgc/scalabot/config/ProxyType;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lnet/lamgc/scalabot/config/ProxyType;
public static fun values ()[Lnet/lamgc/scalabot/config/ProxyType;
}
public final class net/lamgc/scalabot/config/UsernameAuthenticator : com/sun/net/httpserver/BasicAuthenticator {
public fun <init> (Ljava/lang/String;Ljava/lang/String;)V
public fun checkCredentials (Ljava/lang/String;Ljava/lang/String;)Z
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public final fun toJsonObject ()Lcom/google/gson/JsonObject;
}
public final class net/lamgc/scalabot/config/serializer/ArtifactSerializer : com/google/gson/JsonDeserializer, com/google/gson/JsonSerializer {
public static final field INSTANCE Lnet/lamgc/scalabot/config/serializer/ArtifactSerializer;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lorg/eclipse/aether/artifact/Artifact;
public synthetic fun serialize (Ljava/lang/Object;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
public fun serialize (Lorg/eclipse/aether/artifact/Artifact;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
}
public final class net/lamgc/scalabot/config/serializer/AuthenticationSerializer : com/google/gson/JsonDeserializer {
public static final field INSTANCE Lnet/lamgc/scalabot/config/serializer/AuthenticationSerializer;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lorg/eclipse/aether/repository/Authentication;
}
public final class net/lamgc/scalabot/config/serializer/BotAccountSerializer : com/google/gson/JsonDeserializer {
public static final field INSTANCE Lnet/lamgc/scalabot/config/serializer/BotAccountSerializer;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/lamgc/scalabot/config/BotAccount;
}
public final class net/lamgc/scalabot/config/serializer/BotConfigSerializer : com/google/gson/JsonDeserializer, com/google/gson/JsonSerializer {
public static final field INSTANCE Lnet/lamgc/scalabot/config/serializer/BotConfigSerializer;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/lamgc/scalabot/config/BotConfig;
public synthetic fun serialize (Ljava/lang/Object;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
public fun serialize (Lnet/lamgc/scalabot/config/BotConfig;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
}
public final class net/lamgc/scalabot/config/serializer/MavenRepositoryConfigSerializer : com/google/gson/JsonDeserializer {
public static final field INSTANCE Lnet/lamgc/scalabot/config/serializer/MavenRepositoryConfigSerializer;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/lamgc/scalabot/config/MavenRepositoryConfig;
}
public final class net/lamgc/scalabot/config/serializer/ProxyConfigSerializer : com/google/gson/JsonDeserializer, com/google/gson/JsonSerializer {
public static final field INSTANCE Lnet/lamgc/scalabot/config/serializer/ProxyConfigSerializer;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/lamgc/scalabot/config/ProxyConfig;
public synthetic fun serialize (Ljava/lang/Object;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
public fun serialize (Lnet/lamgc/scalabot/config/ProxyConfig;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
}
public final class net/lamgc/scalabot/config/serializer/ProxyTypeSerializer : com/google/gson/JsonDeserializer, com/google/gson/JsonSerializer {
public static final field INSTANCE Lnet/lamgc/scalabot/config/serializer/ProxyTypeSerializer;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/lamgc/scalabot/config/ProxyType;
public synthetic fun serialize (Ljava/lang/Object;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
public fun serialize (Lnet/lamgc/scalabot/config/ProxyType;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
}
public final class net/lamgc/scalabot/config/serializer/UsernameAuthenticatorSerializer : com/google/gson/JsonDeserializer, com/google/gson/JsonSerializer {
public static final field INSTANCE Lnet/lamgc/scalabot/config/serializer/UsernameAuthenticatorSerializer;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/lamgc/scalabot/config/UsernameAuthenticator;
public synthetic fun serialize (Ljava/lang/Object;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
public fun serialize (Lnet/lamgc/scalabot/config/UsernameAuthenticator;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
}

View File

@ -1,47 +1,39 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins { plugins {
kotlin("jvm") kotlin("jvm")
id("org.jetbrains.kotlinx.kover") id("org.jetbrains.kotlinx.kover")
id("org.jetbrains.dokka") version "1.9.20" id("org.jetbrains.dokka") version "1.7.0"
`maven-publish` `maven-publish`
signing signing
id("org.jetbrains.kotlinx.binary-compatibility-validator")
} }
dependencies { dependencies {
val aetherVersion = "1.1.0" val aetherVersion = "1.1.0"
api("org.eclipse.aether:aether-api:$aetherVersion") implementation("org.eclipse.aether:aether-api:$aetherVersion")
implementation("org.eclipse.aether:aether-util:$aetherVersion") implementation("org.eclipse.aether:aether-util:$aetherVersion")
implementation("org.telegram:telegrambots-meta:8.0.0") implementation("org.telegram:telegrambots-meta:6.0.1")
api("com.google.code.gson:gson:2.11.0") implementation("com.google.code.gson:gson:2.9.0")
testImplementation(kotlin("test")) testImplementation(kotlin("test"))
testImplementation("io.mockk:mockk:1.13.13") testImplementation("io.mockk:mockk:1.12.4")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.1") testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.10.1") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.2")
dokkaHtmlPlugin("org.jetbrains.dokka:javadoc-plugin:2.0.0") dokkaHtmlPlugin("org.jetbrains.dokka:javadoc-plugin:1.7.0")
} }
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach { tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
compilerOptions { kotlinOptions {
jvmTarget = JvmTarget.JVM_17 jvmTarget = "11"
} }
} }
java { java {
withJavadocJar() withJavadocJar()
withSourcesJar() withSourcesJar()
sourceCompatibility = JavaVersion.VERSION_17 sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_11
}
tasks.withType<AbstractArchiveTask>().configureEach {
isPreserveFileTimestamps = false
isReproducibleFileOrder = true
} }
tasks.getByName<Test>("test") { tasks.getByName<Test>("test") {
@ -54,10 +46,19 @@ val javadocJar = tasks.named<Jar>("javadocJar") {
publishing { publishing {
repositories { repositories {
maven("https://git.lamgc.me/api/packages/LamGC/maven") { if (project.version.toString().endsWith("-SNAPSHOT", ignoreCase = true)) {
maven("https://nexus.kuku.me/repository/maven-snapshots/") {
credentials { credentials {
username = project.properties["repo.credentials.self-git.username"].toString() username = project.properties["repo.credentials.private.username"].toString()
password = project.properties["repo.credentials.self-git.password"].toString() password = project.properties["repo.credentials.private.password"].toString()
}
}
} else {
maven("https://nexus.kuku.me/repository/maven-releases/") {
credentials {
username = project.properties["repo.credentials.private.username"].toString()
password = project.properties["repo.credentials.private.password"].toString()
}
} }
} }
} }

View File

@ -3,8 +3,7 @@ package net.lamgc.scalabot.config
import org.eclipse.aether.artifact.Artifact import org.eclipse.aether.artifact.Artifact
import org.eclipse.aether.repository.Authentication import org.eclipse.aether.repository.Authentication
import org.eclipse.aether.repository.Proxy import org.eclipse.aether.repository.Proxy
import org.telegram.telegrambots.meta.TelegramUrl import org.telegram.telegrambots.meta.ApiConstants
import java.net.URI
import java.net.URL import java.net.URL
/** /**
@ -27,22 +26,8 @@ data class BotAccount(
get() = token.substringBefore(":").toLong() get() = token.substringBefore(":").toLong()
} }
val defaultTelegramApiUrl: String = URL(
TelegramUrl.DEFAULT_URL.schema,
TelegramUrl.DEFAULT_URL.host,
TelegramUrl.DEFAULT_URL.port,
"/"
).toExternalForm()
/** /**
* 机器人配置. * 机器人配置.
*
* 使用 Gson 解析时, 请添加以下类型适配器:
* - [net.lamgc.scalabot.config.serializer.ProxyTypeSerializer]
* - [net.lamgc.scalabot.config.serializer.BotConfigSerializer]
* - [net.lamgc.scalabot.config.serializer.BotAccountSerializer]
* - [net.lamgc.scalabot.config.serializer.ArtifactSerializer]
*
* @property enabled 是否启用机器人. * @property enabled 是否启用机器人.
* @property account 机器人帐号信息, 用于访问 API. * @property account 机器人帐号信息, 用于访问 API.
* @property disableBuiltInAbility 是否禁用 AbilityBot 自带命令. * @property disableBuiltInAbility 是否禁用 AbilityBot 自带命令.
@ -64,22 +49,8 @@ data class BotConfig(
*/ */
val extensions: Set<Artifact> = emptySet(), val extensions: Set<Artifact> = emptySet(),
val proxy: ProxyConfig = ProxyConfig(type = ProxyType.NO_PROXY), val proxy: ProxyConfig = ProxyConfig(type = ProxyType.NO_PROXY),
val baseApiUrl: String = defaultTelegramApiUrl val baseApiUrl: String = ApiConstants.BASE_URL
) { )
fun getBaseApiTelegramUrl(): TelegramUrl {
if (this.baseApiUrl == defaultTelegramApiUrl) {
return TelegramUrl.DEFAULT_URL
} else {
URI.create(baseApiUrl).let {
return TelegramUrl.builder()
.host(it.host)
.port(it.port)
.schema(it.scheme)
.build()
}
}
}
}
/** /**
* 代理类型. * 代理类型.
@ -101,16 +72,8 @@ enum class ProxyType {
data class ProxyConfig( data class ProxyConfig(
val type: ProxyType = ProxyType.NO_PROXY, val type: ProxyType = ProxyType.NO_PROXY,
val host: String = "127.0.0.1", val host: String = "127.0.0.1",
val port: Int = 1080, val port: Int = 1080
) { )
override fun toString(): String {
return if (type != ProxyType.NO_PROXY) {
"$type://$host:$port"
} else {
"NO_PROXY"
}
}
}
/** /**
* ScalaBot 的运行指标公开配置. * ScalaBot 的运行指标公开配置.
@ -155,13 +118,6 @@ data class MavenRepositoryConfig(
* ScalaBot App 配置. * ScalaBot App 配置.
* *
* App 配置信息与 BotConfig 分开, 分别存储在各自单独的文件中. * App 配置信息与 BotConfig 分开, 分别存储在各自单独的文件中.
*
* 使用 Gson 解析时, 请添加以下类型适配器:
* - [net.lamgc.scalabot.config.serializer.ProxyTypeSerializer]
* - [net.lamgc.scalabot.config.serializer.MavenRepositoryConfigSerializer]
* - [net.lamgc.scalabot.config.serializer.AuthenticationSerializer]
* - [net.lamgc.scalabot.config.serializer.UsernameAuthenticatorSerializer]
*
* @property proxy Telegram API 代理配置. * @property proxy Telegram API 代理配置.
* @property metrics 运行指标数据配置. 可通过时序数据库记录运行数据. * @property metrics 运行指标数据配置. 可通过时序数据库记录运行数据.
* @property mavenRepositories Maven 远端仓库配置. * @property mavenRepositories Maven 远端仓库配置.

View File

@ -3,7 +3,6 @@ package net.lamgc.scalabot.config.serializer
import com.google.gson.* import com.google.gson.*
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import net.lamgc.scalabot.config.* import net.lamgc.scalabot.config.*
import net.lamgc.scalabot.config.serializer.SerializeUtils.getPrimitiveValueOrThrow
import org.eclipse.aether.artifact.AbstractArtifact import org.eclipse.aether.artifact.AbstractArtifact
import org.eclipse.aether.artifact.Artifact import org.eclipse.aether.artifact.Artifact
import org.eclipse.aether.artifact.DefaultArtifact import org.eclipse.aether.artifact.DefaultArtifact
@ -13,7 +12,6 @@ import org.eclipse.aether.util.repository.AuthenticationBuilder
import java.lang.reflect.Type import java.lang.reflect.Type
import java.net.MalformedURLException import java.net.MalformedURLException
import java.net.URL import java.net.URL
import java.util.regex.Pattern
object ProxyTypeSerializer : JsonDeserializer<ProxyType>, object ProxyTypeSerializer : JsonDeserializer<ProxyType>,
JsonSerializer<ProxyType> { JsonSerializer<ProxyType> {
@ -83,8 +81,8 @@ object AuthenticationSerializer : JsonDeserializer<Authentication> {
if (json !is JsonObject) { if (json !is JsonObject) {
throw JsonParseException("Unsupported JSON type.") throw JsonParseException("Unsupported JSON type.")
} }
val username = json.getPrimitiveValueOrThrow("username").asString val username = SerializerUtils.checkJsonKey(json, "username")
val password = json.getPrimitiveValueOrThrow("password").asString val password = SerializerUtils.checkJsonKey(json, "password")
val builder = AuthenticationBuilder() val builder = AuthenticationBuilder()
builder.addUsername(username) builder.addUsername(username)
builder.addPassword(password) builder.addPassword(password)
@ -93,14 +91,14 @@ object AuthenticationSerializer : JsonDeserializer<Authentication> {
} }
internal object SerializeUtils { private object SerializerUtils {
fun checkJsonKey(json: JsonObject, key: String): String {
fun JsonObject.getPrimitiveValueOrThrow(fieldName: String): JsonPrimitive { if (!json.has(key)) {
val value = get(fieldName) ?: throw JsonParseException("Missing `$fieldName` field.") throw JsonParseException("Required field does not exist: $key")
if (value !is JsonPrimitive) { } else if (!json.get(key).isJsonPrimitive) {
throw JsonParseException("Invalid `account` field type.") throw JsonParseException("Wrong field `$key` type: ${json.get(key)::class.java}")
} }
return value return json.get(key).asString
} }
} }
@ -116,7 +114,7 @@ object MavenRepositoryConfigSerializer
is JsonObject -> { is JsonObject -> {
MavenRepositoryConfig( MavenRepositoryConfig(
id = json.get("id")?.asString, id = json.get("id")?.asString,
url = URL(json.getPrimitiveValueOrThrow("url").asString), url = URL(SerializerUtils.checkJsonKey(json, "url")),
proxy = if (json.has("proxy")) proxy = if (json.has("proxy"))
context.deserialize<Proxy>( context.deserialize<Proxy>(
json.get("proxy"), Proxy::class.java json.get("proxy"), Proxy::class.java
@ -262,39 +260,3 @@ object BotConfigSerializer : JsonSerializer<BotConfig>, JsonDeserializer<BotConf
} }
} }
object BotAccountSerializer : JsonDeserializer<BotAccount> {
private val tokenCheckRegex = Pattern.compile("\\d+:[a-zA-Z\\d_-]{35}")
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): BotAccount {
if (json == null || json.isJsonNull) {
throw JsonParseException("Missing `account` field.")
} else if (!json.isJsonObject) {
throw JsonParseException("Invalid `account` field type.")
}
val jsonObj = json.asJsonObject
val name = jsonObj.getPrimitiveValueOrThrow("name").asString
val token = jsonObj.getPrimitiveValueOrThrow("token").asString.let {
if (it.isEmpty()) {
throw JsonParseException("`token` cannot be empty.")
} else if (!tokenCheckRegex.matcher(it).matches()) {
throw JsonParseException("`token` is invalid.")
} else {
it
}
}
val creatorId = try {
jsonObj.getPrimitiveValueOrThrow("creatorId").asLong
} catch (e: NumberFormatException) {
throw JsonParseException("`creatorId` must be a number.")
}.apply {
if (this < 0) {
throw JsonParseException("`creatorId` must be a positive number.")
}
}
return BotAccount(name, token, creatorId)
}
}

View File

@ -219,12 +219,6 @@ internal class ProxyConfigTest {
assertEquals(8080, actualConfig.port) assertEquals(8080, actualConfig.port)
assertEquals(ProxyType.HTTP, actualConfig.type) assertEquals(ProxyType.HTTP, actualConfig.type)
} }
@Test
fun `toString test`() {
assertEquals("NO_PROXY", ProxyConfig(ProxyType.NO_PROXY).toString())
assertEquals("HTTP://example.org:1008", ProxyConfig(ProxyType.HTTP, "example.org", 1008).toString())
}
} }
internal class MetricsConfigTest { internal class MetricsConfigTest {

View File

@ -5,7 +5,6 @@ import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.verify import io.mockk.verify
import net.lamgc.scalabot.config.* import net.lamgc.scalabot.config.*
import net.lamgc.scalabot.config.serializer.SerializeUtils.getPrimitiveValueOrThrow
import org.eclipse.aether.artifact.Artifact import org.eclipse.aether.artifact.Artifact
import org.eclipse.aether.artifact.DefaultArtifact import org.eclipse.aether.artifact.DefaultArtifact
import org.eclipse.aether.repository.Authentication import org.eclipse.aether.repository.Authentication
@ -13,39 +12,54 @@ import org.eclipse.aether.repository.AuthenticationContext
import org.eclipse.aether.repository.Proxy import org.eclipse.aether.repository.Proxy
import org.intellij.lang.annotations.Language import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Assertions.assertThrows
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.lang.reflect.Type import java.lang.reflect.Type
import java.net.URL import java.net.URL
import kotlin.test.* import kotlin.test.*
internal class SerializeUtilsTest { internal class SerializersKtTest {
private val instance: Any
private val method: Method
init {
val clazz = Class.forName("net.lamgc.scalabot.config.serializer.SerializerUtils")
method = clazz.getDeclaredMethod("checkJsonKey", JsonObject::class.java, String::class.java)
method.isAccessible = true
instance = clazz.getDeclaredField("INSTANCE").apply {
isAccessible = true
}.get(null)
}
private fun invoke(json: JsonObject, key: String): String {
try {
return method.invoke(instance, json, key) as String
} catch (e: InvocationTargetException) {
throw e.targetException
}
}
@Test @Test
fun `getPrimitiveValueOrThrow test`() { fun `Json key checker test`() {
assertThrows(JsonParseException::class.java) { assertThrows(JsonParseException::class.java) {
JsonObject().getPrimitiveValueOrThrow("NOT_EXIST_KEY") invoke(JsonObject(), "NOT_EXIST_KEY")
}
assertThrows(JsonParseException::class.java) {
invoke(JsonObject().apply { add("NULL_KEY", JsonNull.INSTANCE) }, "NULL_KEY")
}
assertThrows(JsonParseException::class.java) {
invoke(JsonObject().apply { add("ARRAY_KEY", JsonArray()) }, "ARRAY_KEY")
}
assertThrows(JsonParseException::class.java) {
invoke(JsonObject().apply { add("OBJECT_KEY", JsonObject()) }, "OBJECT_KEY")
} }
assertThrows(JsonParseException::class.java) { val expectKey = "TEST"
JsonObject().apply { val expectString = "testString"
add("testKey", JsonArray()) val json = JsonObject().apply { addProperty(expectKey, expectString) }
}.getPrimitiveValueOrThrow("testKey")
}
assertThrows(JsonParseException::class.java) {
JsonObject().apply {
add("testKey", JsonObject())
}.getPrimitiveValueOrThrow("testKey")
}
assertThrows(JsonParseException::class.java) {
JsonObject().apply {
add("testKey", JsonNull.INSTANCE)
}.getPrimitiveValueOrThrow("testKey")
}
val expectKey = "STRING_KEY" assertEquals(expectString, invoke(json, expectKey))
val expectValue = JsonPrimitive("A STRING")
assertEquals(expectValue, JsonObject()
.apply { add(expectKey, expectValue) }
.getPrimitiveValueOrThrow(expectKey))
} }
} }
@ -53,7 +67,7 @@ internal class ProxyTypeSerializerTest {
@Test @Test
fun `serialize test`() { fun `serialize test`() {
for (type in ProxyType.entries) { for (type in ProxyType.values()) {
assertEquals( assertEquals(
JsonPrimitive(type.name), ProxyTypeSerializer.serialize(type, null, null), JsonPrimitive(type.name), ProxyTypeSerializer.serialize(type, null, null),
"ProxyType 序列化结果与预期不符." "ProxyType 序列化结果与预期不符."
@ -79,7 +93,7 @@ internal class ProxyTypeSerializerTest {
ProxyTypeSerializer.deserialize(JsonNull.INSTANCE, null, null) ProxyTypeSerializer.deserialize(JsonNull.INSTANCE, null, null)
) )
for (type in ProxyType.entries) { for (type in ProxyType.values()) {
assertEquals( assertEquals(
type, ProxyTypeSerializer.deserialize(JsonPrimitive(type.name), null, null), type, ProxyTypeSerializer.deserialize(JsonPrimitive(type.name), null, null),
"ProxyType 反序列化结果与预期不符." "ProxyType 反序列化结果与预期不符."
@ -692,161 +706,3 @@ internal class UsernameAuthenticatorSerializerTest {
} }
} }
internal class BotAccountSerializerTest {
private val expectToken = "123456789:AAHErDroUTznQsOd_oZPJ6cQEj4Z5mGHO10"
private val gson = GsonBuilder()
.registerTypeAdapter(BotAccount::class.java, BotAccountSerializer)
.create()
@Test
fun `Invalid json type check test`() {
assertThrows(JsonParseException::class.java) {
BotAccountSerializer.deserialize(null, null, null)
}
assertThrows(JsonParseException::class.java) {
BotAccountSerializer.deserialize(JsonNull.INSTANCE, null, null)
}
assertThrows(JsonParseException::class.java) {
BotAccountSerializer.deserialize(JsonPrimitive("A STRING"), null, null)
}
assertThrows(JsonParseException::class.java) {
BotAccountSerializer.deserialize(JsonArray(), null, null)
}
}
@Test
fun `Field missing test`() {
assertThrows(JsonParseException::class.java) {
BotAccountSerializer.deserialize(JsonObject(), null, null)
}
assertThrows(JsonParseException::class.java) {
BotAccountSerializer.deserialize(JsonObject().apply {
addProperty("token", expectToken)
addProperty("creatorId", 1)
}, null, null)
}
assertThrows(JsonParseException::class.java) {
BotAccountSerializer.deserialize(JsonObject().apply {
addProperty("name", "testUser")
addProperty("creatorId", 1)
}, null, null)
}
assertThrows(JsonParseException::class.java) {
BotAccountSerializer.deserialize(JsonObject().apply {
addProperty("name", "testUser")
addProperty("token", expectToken)
}, null, null)
}
val account = BotAccountSerializer.deserialize(JsonObject().apply {
addProperty("name", "testUser")
addProperty("token", expectToken)
addProperty("creatorId", 1)
}, null, null)
assertNotNull(account)
assertEquals("testUser", account.name)
assertEquals(expectToken, account.token)
assertEquals(1, account.creatorId)
}
@Test
fun `'token' check test`() {
val jsonObject = JsonObject().apply {
addProperty("name", "testUser")
addProperty("token", expectToken)
addProperty("creatorId", 123456789123456789)
}
val looksGoodAccount = BotAccountSerializer.deserialize(jsonObject, null, null)
assertNotNull(looksGoodAccount)
assertEquals("testUser", looksGoodAccount.name)
assertEquals(expectToken, looksGoodAccount.token)
assertEquals(123456789123456789, looksGoodAccount.creatorId)
try {
BotAccountSerializer.deserialize(jsonObject.deepCopy().apply {
addProperty("token", "")
}, null, null)
fail("Token 为空,但是没有抛出异常。")
} catch (e: JsonParseException) {
assertEquals("`token` cannot be empty.", e.message)
}
try {
BotAccountSerializer.deserialize(jsonObject.deepCopy().apply {
addProperty("token", "abcdefghijklmnopqrstuvwxyz")
}, null, null)
fail("Token 格式错误(基本格式错误),但是没有抛出异常。")
} catch (e: JsonParseException) {
assertEquals("`token` is invalid.", e.message)
}
try {
BotAccountSerializer.deserialize(jsonObject.deepCopy().apply {
addProperty("token", "abcdefgh:ijklmnopqrstuvwxyz-1234567890_abcde")
}, null, null)
fail("Token 格式错误ID 不为数字),但是没有抛出异常。")
} catch (e: JsonParseException) {
assertEquals("`token` is invalid.", e.message)
}
try {
BotAccountSerializer.deserialize(jsonObject.deepCopy().apply {
addProperty("token", "0123456789:ijklmnopqrstu-vwxyz_123456")
}, null, null)
fail("Token 格式错误(授权令牌长度错误),但是没有抛出异常。")
} catch (e: JsonParseException) {
assertEquals("`token` is invalid.", e.message)
}
}
@Test
fun `'creatorId' check test`() {
val jsonObject = JsonObject().apply {
addProperty("name", "testUser")
addProperty("token", expectToken)
addProperty("creatorId", 1)
}
val looksGoodAccount = gson.fromJson(jsonObject, BotAccount::class.java)
assertEquals(1, looksGoodAccount.creatorId)
try {
BotAccountSerializer.deserialize(jsonObject.deepCopy().apply {
addProperty("creatorId", "A STRING")
}, null, null)
fail("creatorId 不是一个数字,但是没有抛出异常。")
} catch (e: JsonParseException) {
assertEquals("`creatorId` must be a number.", e.message)
}
try {
BotAccountSerializer.deserialize(jsonObject.deepCopy().apply {
addProperty("creatorId", -1)
}, null, null)
fail("creatorId 不能为负数,但是没有抛出异常。")
} catch (e: JsonParseException) {
assertEquals("`creatorId` must be a positive number.", e.message)
}
}
@Test
fun `json deserialize test`() {
val jsonObject = JsonObject().apply {
addProperty("name", "testUser")
addProperty("token", expectToken)
addProperty("creatorId", 1)
}
val looksGoodAccount = gson.fromJson(jsonObject, BotAccount::class.java)
assertNotNull(looksGoodAccount)
assertEquals("testUser", looksGoodAccount.name)
assertEquals(expectToken, looksGoodAccount.token)
assertEquals(1, looksGoodAccount.creatorId)
}
}