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