mirror of
https://github.com/LamGC/oracle-manager.git
synced 2025-04-29 14:17:33 +00:00
feat: InlineKeyboardCallback 支持记录多个数据项.
通过支持多个数据项, 可以在内联键盘回调中传递多个数据.
This commit is contained in:
parent
7f3fa2ea0f
commit
f1eef0ad94
@ -68,7 +68,7 @@ class OracleAccountManagerExtension(private val bot: BaseAbilityBot) : AbilityEx
|
||||
|
||||
if (!OracleAccessKeyManager.accessKeyContains(profile.keyFingerprint)) {
|
||||
bot.db().getVar<String>("oc_account_add::${upd.message.chatId}::profile").set(
|
||||
profile.toJson()
|
||||
profile.toJsonString()
|
||||
)
|
||||
bot.silent().send(
|
||||
"OK,配置文件检查通过,现在需要发送相应的私钥(机器人的所有人将对密钥的安全性负责)," +
|
||||
@ -174,7 +174,12 @@ class OracleAccountManagerExtension(private val bot: BaseAbilityBot) : AbilityEx
|
||||
.newRow()
|
||||
.addButton {
|
||||
text(changeNameMsg)
|
||||
callbackData(action = "oc_account_change_name", profile.toJson())
|
||||
callbackData(
|
||||
action = "oc_account_change_name",
|
||||
extraData = jsonObjectOf {
|
||||
JSON_FIELD_PROFILE += profile
|
||||
}
|
||||
)
|
||||
}
|
||||
.build()
|
||||
return SendMessage.builder()
|
||||
@ -216,7 +221,9 @@ class OracleAccountManagerExtension(private val bot: BaseAbilityBot) : AbilityEx
|
||||
.text(text)
|
||||
.callbackData(
|
||||
action = "oc_account_manager",
|
||||
extraData = account.toJson()
|
||||
extraData = jsonObjectOf {
|
||||
JSON_FIELD_PROFILE += account
|
||||
}
|
||||
)
|
||||
.build()
|
||||
)
|
||||
@ -245,7 +252,7 @@ class OracleAccountManagerExtension(private val bot: BaseAbilityBot) : AbilityEx
|
||||
|
||||
fun manageOracleAccount(): Reply = Reply.of({ bot, upd ->
|
||||
val keyboardCallback = upd.callbackQuery.callbackData
|
||||
val profile = OracleAccountProfile.fromJson(keyboardCallback.extraData!!)
|
||||
val profile = getProfileByCallback(upd.callbackQuery.callbackData)
|
||||
val identityClient = IdentityClient(profile.getAuthenticationDetailsProvider())
|
||||
val user = try {
|
||||
identityClient.getUser(profile.userId)
|
||||
@ -257,12 +264,12 @@ class OracleAccountManagerExtension(private val bot: BaseAbilityBot) : AbilityEx
|
||||
.newRow()
|
||||
.addButton {
|
||||
text("服务器列表")
|
||||
callbackData(action = "oc_server_list", keyboardCallback.extraData)
|
||||
callbackData(keyboardCallback.next("oc_server_list"))
|
||||
}
|
||||
.newRow()
|
||||
.addButton {
|
||||
text("账号管理")
|
||||
callbackData(action = "oc_account_edit", keyboardCallback.extraData)
|
||||
callbackData(keyboardCallback.next("oc_account_edit"))
|
||||
}
|
||||
.newRow().addButton {
|
||||
text("<<< 返回上一级")
|
||||
@ -317,7 +324,7 @@ class OracleAccountManagerExtension(private val bot: BaseAbilityBot) : AbilityEx
|
||||
|
||||
fun removeOracleAccount(): Reply = ReplyFlow.builder(bot.db())
|
||||
.action { bot, upd ->
|
||||
val profile = OracleAccountProfile.fromJson(upd.callbackQuery.callbackData.extraData!!)
|
||||
val profile = getProfileByCallback(upd.callbackQuery.callbackData)
|
||||
val keyboardCallback = upd.callbackQuery.callbackData
|
||||
EditMessageText.builder()
|
||||
.chatId(upd.callbackQuery.message.chatId.toString())
|
||||
@ -339,8 +346,7 @@ class OracleAccountManagerExtension(private val bot: BaseAbilityBot) : AbilityEx
|
||||
}
|
||||
.onlyIf(callbackQueryAt("oc_account_remove"))
|
||||
.next(Reply.of({ bot, upd ->
|
||||
val keyboardCallback = upd.callbackQuery.callbackData
|
||||
val profile = OracleAccountProfile.fromJson(keyboardCallback.extraData!!)
|
||||
val profile = getProfileByCallback(upd.callbackQuery.callbackData)
|
||||
val result =
|
||||
OracleAccountManage.removeOracleAccountByOracleUserId(profile.userId, upd.callbackQuery.from.id)
|
||||
val msg = if (result) {
|
||||
@ -366,12 +372,12 @@ class OracleAccountManagerExtension(private val bot: BaseAbilityBot) : AbilityEx
|
||||
bot.silent().send("出现未知错误,请联系机器人管理员。", upd.callbackQuery.message.chatId)
|
||||
return@action
|
||||
}
|
||||
val profile = OracleAccountProfile.fromJson(upd.callbackQuery.callbackData.extraData!!)
|
||||
val profile = getProfileByCallback(upd.callbackQuery.callbackData)
|
||||
val entryName = "oc_account_change_name::cache::" +
|
||||
"chat_${upd.callbackQuery.message.chatId}::user_${upd.callbackQuery.from.id}::profile"
|
||||
logger.debug { "询问名称 - Profile 键名称:$entryName" }
|
||||
|
||||
bot.db().getVar<String>(entryName).set(upd.callbackQuery.callbackData.extraData)
|
||||
bot.db().getVar<String>(entryName).set(getProfileByCallback(upd.callbackQuery.callbackData).toJsonString())
|
||||
bot.silent().send(
|
||||
"当前机器人的名称为:\n${profile.name}\n请发送机器人的新名称。",
|
||||
upd.callbackQuery.message.chatId
|
||||
|
@ -1,6 +1,7 @@
|
||||
package net.lamgc.scext.oraclemanager
|
||||
|
||||
import com.google.common.base.Supplier
|
||||
import com.google.gson.JsonObject
|
||||
import com.oracle.bmc.OCID
|
||||
import com.oracle.bmc.Region
|
||||
import com.oracle.bmc.auth.AuthenticationDetailsProvider
|
||||
@ -37,7 +38,9 @@ data class OracleAccountProfile(
|
||||
var name: String,
|
||||
) : java.io.Serializable {
|
||||
|
||||
fun toJson(): String = gson.toJson(this)
|
||||
fun toJsonString(): String = gson.toJson(this)
|
||||
|
||||
fun toJsonObject(): JsonObject = gson.toJsonTree(this, JsonObject::class.java).asJsonObject
|
||||
|
||||
fun getAuthenticationDetailsProvider(accessKeyProvider: Supplier<InputStream>? = null): AuthenticationDetailsProvider {
|
||||
val accessKey = accessKeyProvider
|
||||
@ -56,6 +59,10 @@ data class OracleAccountProfile(
|
||||
@JvmStatic
|
||||
fun fromJson(json: String): OracleAccountProfile =
|
||||
gson.fromJson(json, OracleAccountProfile::class.java)
|
||||
|
||||
@JvmStatic
|
||||
fun fromJson(json: JsonObject): OracleAccountProfile =
|
||||
gson.fromJson(json, OracleAccountProfile::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ import com.google.common.base.Strings
|
||||
import com.google.common.cache.Cache
|
||||
import com.google.common.cache.CacheBuilder
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.JsonElement
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import com.oracle.bmc.ConfigFileReader.ConfigFile
|
||||
@ -17,6 +18,7 @@ import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter
|
||||
import org.bouncycastle.openssl.jcajce.JcaPKCS8Generator
|
||||
import org.bouncycastle.util.io.pem.PemWriter
|
||||
import org.telegram.abilitybots.api.bot.BaseAbilityBot
|
||||
import org.telegram.abilitybots.api.objects.Reply
|
||||
import org.telegram.abilitybots.api.sender.MessageSender
|
||||
import org.telegram.abilitybots.api.sender.SilentSender
|
||||
import org.telegram.telegrambots.meta.api.methods.BotApiMethod
|
||||
@ -249,8 +251,11 @@ fun InlineKeyboardButtonBuilder.callbackData(callback: InlineKeyboardCallback):
|
||||
return this
|
||||
}
|
||||
|
||||
fun InlineKeyboardButtonBuilder.callbackData(action: String, extraData: String? = null): InlineKeyboardButtonBuilder {
|
||||
callbackData(InlineKeyboardCallback(action, extraData))
|
||||
fun InlineKeyboardButtonBuilder.callbackData(
|
||||
action: String,
|
||||
extraData: JsonObject? = null
|
||||
): InlineKeyboardButtonBuilder {
|
||||
callbackData(InlineKeyboardCallback(action, extraData ?: JsonObject()))
|
||||
return this
|
||||
}
|
||||
|
||||
@ -291,14 +296,14 @@ val gson = Gson()
|
||||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
data class InlineKeyboardCallback(
|
||||
@SerializedName("a") val action: String,
|
||||
@SerializedName("d") val extraData: String? = null
|
||||
@SerializedName("d") val extraData: JsonObject = JsonObject()
|
||||
) {
|
||||
|
||||
fun toJson(): String {
|
||||
return gson.toJson(this)
|
||||
}
|
||||
|
||||
fun next(newAction: String, newExtraData: String? = null): InlineKeyboardCallback {
|
||||
fun next(newAction: String, newExtraData: JsonObject? = null): InlineKeyboardCallback {
|
||||
return InlineKeyboardCallback(newAction, newExtraData ?: this.extraData)
|
||||
}
|
||||
|
||||
@ -328,3 +333,45 @@ fun Random.randomString(length: Int): String {
|
||||
return builder.toString()
|
||||
}
|
||||
|
||||
fun callbackQueryOf(action: String, block: (BaseAbilityBot, Update) -> Unit): Reply =
|
||||
Reply.of(block, callbackQueryAt(action))
|
||||
|
||||
const val JSON_FIELD_PROFILE = "profile"
|
||||
|
||||
fun getProfileByCallback(callback: InlineKeyboardCallback): OracleAccountProfile {
|
||||
return OracleAccountProfile.fromJson(callback.extraData[JSON_FIELD_PROFILE].asJsonObject)
|
||||
}
|
||||
|
||||
class JsonObjectBuilder(private val jsonObject: JsonObject) {
|
||||
|
||||
operator fun String.plusAssign(json: JsonElement) {
|
||||
jsonObject.add(this, json)
|
||||
}
|
||||
|
||||
operator fun String.plusAssign(value: String) {
|
||||
jsonObject.addProperty(this, value)
|
||||
}
|
||||
|
||||
operator fun String.plusAssign(value: Boolean) {
|
||||
jsonObject.addProperty(this, value)
|
||||
}
|
||||
|
||||
operator fun String.plusAssign(value: Char) {
|
||||
jsonObject.addProperty(this, value)
|
||||
}
|
||||
|
||||
operator fun String.plusAssign(value: Number) {
|
||||
jsonObject.addProperty(this, value)
|
||||
}
|
||||
|
||||
operator fun String.plusAssign(value: Any) {
|
||||
jsonObject.add(this, gson.toJsonTree(value))
|
||||
}
|
||||
}
|
||||
|
||||
fun jsonObjectOf(block: JsonObjectBuilder.() -> Unit): JsonObject {
|
||||
val jsonObject = JsonObject()
|
||||
JsonObjectBuilder(jsonObject).block()
|
||||
return jsonObject
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user