feat: add send mail command

This commit is contained in:
2024-09-17 22:37:42 +08:00
parent b6d6578436
commit 069fed22ca
9 files changed
+104 -10

No files matched your search

+1
View File
@@ -36,6 +36,7 @@ dependencies {
implementation(libs.sqliteJDBC) implementation(libs.sqliteJDBC)
implementation(libs.motdPinger) implementation(libs.motdPinger)
implementation(libs.animated.gif.lib) implementation(libs.animated.gif.lib)
implementation(libs.simple.java.mail)
} }
tasks.build { tasks.build {
+2
View File
@@ -7,6 +7,7 @@ exposedVersion = "0.53.0"
sqliteJDBCVersion = "3.46.1.0" sqliteJDBCVersion = "3.46.1.0"
motdPingerVersion = "1.0.1" motdPingerVersion = "1.0.1"
animatedGifLibVersion = "1.4" animatedGifLibVersion = "1.4"
simpleJavaMailVersion = "8.11.3"
[libraries] [libraries]
rOneBot = { group = "cn.rtast", name = "ROneBot", version.ref = "ronebotVersion" } rOneBot = { group = "cn.rtast", name = "ROneBot", version.ref = "ronebotVersion" }
@@ -16,6 +17,7 @@ exposedCore = { group = "org.jetbrains.exposed", name = "exposed-core", version.
exposedJDBC = { group = "org.jetbrains.exposed", name = "exposed-jdbc", version.ref = "exposedVersion" } exposedJDBC = { group = "org.jetbrains.exposed", name = "exposed-jdbc", version.ref = "exposedVersion" }
sqliteJDBC = { group = "org.xerial", name = "sqlite-jdbc", version.ref = "sqliteJDBCVersion" } sqliteJDBC = { group = "org.xerial", name = "sqlite-jdbc", version.ref = "sqliteJDBCVersion" }
animated-gif-lib = { group = "com.madgag", name = "animated-gif-lib", version.ref = "animatedGifLibVersion" } animated-gif-lib = { group = "com.madgag", name = "animated-gif-lib", version.ref = "animatedGifLibVersion" }
simple-java-mail = {group="org.simplejavamail", name="simple-java-mail",version.ref="simpleJavaMailVersion"}
[bundles] [bundles]
+7 -5
View File
@@ -31,9 +31,11 @@ val DEFAULT_CONFIG = Config(
githubKey = "1919810", githubKey = "1919810",
imageType = ImageType.PNG, imageType = ImageType.PNG,
openAIAPIHost = "https://api.moonshot.cn", openAIAPIHost = "https://api.moonshot.cn",
openAIAPIKey = "114514" openAIAPIKey = "114514",
) smtpHost = "127.0.0.1",
smtpPort = 25,
val ADMINS = listOf( smtpUser = "114514",
3458671395L smtpPassword = "1919810",
smtpFromAddress = "fancybot@repo.rtast.cn",
admins = listOf(3458671395L)
) )
@@ -152,7 +152,8 @@ val commands = listOf(
JiJianCommand(), LikeMeCommand(), JiJianCommand(), LikeMeCommand(),
CompilerCommand(), GPTCommand(), CompilerCommand(), GPTCommand(),
GithubUserCommand(), AboutCommand(), GithubUserCommand(), AboutCommand(),
MusicPlayUrlCommand(), DomainPriceCommand() MusicPlayUrlCommand(), DomainPriceCommand(),
SendMailCommand()
) )
suspend fun main() { suspend fun main() {
@@ -0,0 +1,68 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/17
*/
package cn.rtast.fancybot.commands
import cn.rtast.fancybot.configManager
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.ob.MessageChain
import cn.rtast.rob.util.ob.OBMessage
import org.simplejavamail.api.mailer.config.TransportStrategy
import org.simplejavamail.email.EmailBuilder
import org.simplejavamail.mailer.MailerBuilder
class SendMailCommand : BaseCommand() {
override val commandNames = listOf("/mail")
private val smtpHost = configManager.smtpHost
private val smtpPort = configManager.smtpPort
private val smtpUser = configManager.smtpUser
private val smtpPassword = configManager.smtpPassword
private val smtpFromAddress = configManager.smtpFromAddress
private val mailer = MailerBuilder.withSMTPServer(smtpHost, smtpPort, smtpUser, smtpPassword)
.withTransportStrategy(TransportStrategy.SMTP_TLS)
.clearEmailValidator()
.async()
.buildMailer()
private fun sendMail(target: String) {
val email = EmailBuilder.startingBlank()
.from(smtpFromAddress)
.to(target)
.withSubject("Test email from FancyBot")
.withPlainText("Test email~")
.buildEmail()
this.mailer.sendMail(email)
}
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
if (args.isEmpty()) {
val msg = MessageChain.Builder()
.addAt(message.sender.userId)
.addText("发送`/mail <收件人地址>`即可发送测试邮件~")
.build()
listener.sendGroupMessage(message.groupId, msg)
return
}
if (message.sender.userId !in configManager.admins) {
val msg = MessageChain.Builder()
.addReply(message.messageId)
.addText("你不许发邮件")
.build()
listener.sendGroupMessage(message.groupId, msg)
return
}
val target = args.first()
this.sendMail(target)
val msg = MessageChain.Builder()
.addAt(message.sender.userId)
.addText("正在发送邮件, 请查收~(可能会出现在垃圾桶中~)")
.build()
listener.sendGroupMessage(message.groupId, msg)
}
}
@@ -7,7 +7,7 @@
package cn.rtast.fancybot.commands.record package cn.rtast.fancybot.commands.record
import cn.rtast.fancybot.ADMINS import cn.rtast.fancybot.configManager
import cn.rtast.fancybot.signManager import cn.rtast.fancybot.signManager
import cn.rtast.rob.entity.GroupMessage import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.enums.UserRole import cn.rtast.rob.enums.UserRole
@@ -20,7 +20,7 @@ class MyPointCommand : BaseCommand() {
override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) { override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List<String>) {
// query target's point // query target's point
if (args.isNotEmpty() && (message.sender.role == UserRole.admin || message.sender.userId in ADMINS)) { if (args.isNotEmpty() && (message.sender.role == UserRole.admin || message.sender.userId in configManager.admins)) {
val targetId = args.first().toLong() val targetId = args.first().toLong()
val targetData = signManager.getStatus(targetId) val targetData = signManager.getStatus(targetId)
val msg = MessageChain.Builder().addAt(message.sender.userId) val msg = MessageChain.Builder().addAt(message.sender.userId)
@@ -21,5 +21,11 @@ data class Config(
val githubKey: String, val githubKey: String,
val imageType: ImageType, val imageType: ImageType,
val openAIAPIHost: String, val openAIAPIHost: String,
val openAIAPIKey: String val openAIAPIKey: String,
val smtpHost: String,
val smtpPort: Int,
val smtpUser: String,
val smtpPassword: String,
val smtpFromAddress: String,
val admins: List<Long>
) )
@@ -22,4 +22,10 @@ class ConfigManager : JsonFileHandler<Config>("config.json") {
val imageType get() = this.read<Config>().imageType val imageType get() = this.read<Config>().imageType
val openAIAPIHost get() = this.read<Config>().openAIAPIHost val openAIAPIHost get() = this.read<Config>().openAIAPIHost
val openAIAPIKey get() = this.read<Config>().openAIAPIKey val openAIAPIKey get() = this.read<Config>().openAIAPIKey
val smtpHost get() = this.read<Config>().smtpHost
val smtpPort get() = this.read<Config>().smtpPort
val smtpUser get() = this.read<Config>().smtpUser
val smtpPassword get() = this.read<Config>().smtpPassword
val smtpFromAddress get() = this.read<Config>().smtpFromAddress
val admins get() = this.read<Config>().admins
} }
+9 -1
View File
@@ -12,5 +12,13 @@
"githubKey": "1919810", "githubKey": "1919810",
"imageType": "PNG", "imageType": "PNG",
"openAIAPIHost": "https://api.moonshot.cn", "openAIAPIHost": "https://api.moonshot.cn",
"openAIAPIKey": "114514" "openAIAPIKey": "114514",
"smtpHost": "127.0.0.1",
"smtpPort": 25,
"smtpUser": "114514",
"smtpPassword": "1919810",
"smtpFromAddress": "fancybot@repo.rtast.cn",
"admins": [
3458671395
]
} }